这样验证private类型成员无法被继承(public,private,protected)注:private,protected略去不做证明
下面只要验证 testProtected 能被第三层继承类继承,但是无法被第三层类直接调用就说明是public继承后继承类型为protected,而基类为Public类型成员则即可被继承又可以直接调用。
1 #include
2 #include
3 4 class base 5 { 6 public: 7 std::string testPublic() 8 { 9 return std::string("this is public base"); 10 } 11 protected: 12 std::string testProtected() 13 { 14 return std::string("this is protected base"); 15 } 16 private: 17 std::string testPrivate() 18 { 19 return std::string("this is private base"); 20 } 21 }; 22 23 class derivedPublic:public base 24 { 25 public: 26 std::string testPubPublic() 27 { 28 return testPublic()+= "in derived"; 29 } 30 31 std::string testProPublic() 32 { 33 return testProtected()+= "in derived"; 34 } 35 36 // std::string testPriPublic() 37 // { 38 // return testPrivate()+= "in derived"; 39 // } 40 }; 41 42 class deepDerived:public derivedPublic 43 { 44 public: 45 std::string deepProtected() 46 { 47 return testProtected() +="in deep"; 48 } 49 50 std::string deepPublic() 51 { 52 return testPublic() +="indeep"; 53 } 54 }; 55 56 int main(int argc,char* argv[]) 57 { 58 derivedPublic dpub; 59 std::cout << dpub.testProtected() << std::endl; 60 deepDerived deepdpub; 61 std::cout<
这里服务器报错
derived12.cpp:13: error: `std::string base::testProtected()' is protected
derived12.cpp:62: error: within this context
这样就验证了一个是public,一个是protected,protected是不能直接调用的,但是被继承后是可以被public成员调用的。 下面的已经证明,详细步骤就略去如果对该部分验证感兴趣,可以看下面代码。
1 #include
2 #include
3 class base 4 { 5 public: 6 std::string testPublic() 7 { 8 return std::string("this is public base"); 9 } 10 protected: 11 std::string testProtected() 12 { 13 return std::string("this is protected base"); 14 } 15 private: 16 std::string testPrivate() 17 { 18 return std::string("this is private base"); 19 } 20 }; 21 22 class derivedPublic:public base 23 { 24 public: 25 std::string testPubPublic() 26 { 27 return testPublic()+= "in derived"; 28 } 29 30 std::string testProPublic() 31 { 32 return testProtected()+= "in derived"; 33 } 34 35 // std::string testPriPublic() //私有成员并没有被继承下来 36 // { 37 // return testPrivate()+= "in derived"; 38 // } 39 }; 40 41 class deepDerived:public derivedPublic 42 { 43 public: 44 std::string test() 45 { 46 return testPublic() +="in 3"; 47 } 48 }; 49 50 class derivedProtected:protected base 51 { 52 public: 53 std::string testPubProtected() 54 { 55 return testPublic()+= "in derived"; 56 } 57 58 std::string testProProtected() 59 { 60 return testProtected()+= "in derived"; 61 } 62 }; 63 64 class deepDerived2:public derivedProtected 65 { 66 public: 67 std::string test() 68 { 69 return testPublic() +="in 3"; 70 } 71 }; 72 73 class derivedPrivate:private base 74 { 75 public: 76 std::string testPubPirvate() 77 { 78 return testPublic()+= "in derived"; 79 } 80 81 std::string testProPrivate() 82 { 83 return testProtected()+= "in derived"; 84 } 85 86 }; 87 88 //class deepDerived3:public derivedPrivate 89 //{ 90 // public: 91 // std::string test() 92 // { 93 // return testPublic() +="in 3"; 94 // } 95 //}; 96 97 int main(int argc,char* argv[]) 98 { 99 derivedPublic dpub; 100 //derivedProtected dpro; 101 //derivedPrivate dpri; 102 std::cout<