在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
示例代码对以下两种情况进行了说明: 1. 继承时改变虚函数的权限; 2. 私有继承; class A { public: virtual void test1() { cout << "public test1 of A" << endl; } private: virtual void test2() { cout << "private test2 of A" << endl; } protected: int p1; private: int p2; }; class B: public A { private: void test1() { cout << "privte test1 of B" << endl; } public: void test2() { cout << "public test2 of B" << endl; } }; class C: private A { public: void testProtected() { cout << p1 << endl; } }; class D: public C { public: void testProtected() { // cout << p1 << endl;//因为c私有继承A,c不能访问a的protedted成员 } }; int main() { B b; A* a = &b; a->test1();//可以访问, 权限以A为准 // a->test2();//无法访问私有成员 C c; }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论