My Code looks like this -
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {};
};
class Derived: public Base {
private:
int secret;
void print() {
cout<<"the secret is "<<this->secret<<endl;
}
public:
explicit Derived(int a): secret(a) { }
};
int main() {
Base* b1;
Derived d1(1022);
d1.print(); // Wont Work - and should not work
b1 = &d1;
b1->print(); // Works and prints the secret
return 0;
}
I don't understand how can a derived class private function be called using a base class pointer? I understand that after declaring the base class function as virtual we essentially use late binding and check the contents of the pointer before associating it with a function call but does it ignore the access specifiers in the derived classes if the base class implementation of the same function is virtual and public?