Обеспечение более строгого доступа к производному классу В C++
Hi everyone i have one question is my mind This program worked ----------------------------------------- #include<iostream> using namespace std; //declaring base class class Base { public: virtual int fun(int i) { cout << "Base::fun(int i) called"; } }; //Declaring Derived class class Derived: public Base { private: int fun(int x) { cout << "Derived::fun(int x) called"; } }; int main() { Base *ptr = new Derived; ptr->fun(10); return 0; } //It worked and gave output as : Derived::fun(int x) called But the below program didn't worked, don't know why ------------------------------------------------- #include<iostream> using namespace std; //Declaring base class class Base { private: virtual int fun(int i) { cout << "Base::fun(int i) called"; } }; //Declaring Derived class class Derived: public Base { public: int fun(int x) { cout << "Derived::fun(int x) called"; } }; int main() { Base *ptr = new Derived; ptr->fun(10); return 0; }
Что я уже пробовал:
Я изучил виртуальные функции, но запутался в работе двух вышеперечисленных программ