# cpp **Repository Path**: rouchie/cpp ## Basic Information - **Project Name**: cpp - **Description**: 记录平时遇到的CPP知识点 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-13 - **Last Updated**: 2023-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README - [cpp](#cpp) - [子类虚函数私有化](#子类虚函数私有化) # cpp ## 子类虚函数私有化 >privateVirtual.cpp ``` /* 子类虚函数私有化,则子类无法直接调用虚函数,必须通过父类来调用 */ #include class A { public: virtual void hello() = 0; }; class B : public A { private: virtual void hello() { std::cout << "B hello" << std::endl; } } class C : public B { private: virtual void hello() { std::cout << "C hello" << std::endl; } } class D : public B { public: virtual void hello() { std::cout << "C hello" << std::endl; } } int main() { A * p0 = new B(); A * p1 = new C(); A * p2 = new D(); p0->hello(); p1->hello(); p2->hello(); return 0; } ```