#include <iostream>
using namespace std;
class Person
{
public:
void P(){ cout << "Person P" << endl; }
virtual void Print(){ cout << "Person Print" << endl; }
Person(){ cout << "Constructor Person" << endl; }
virtual ~Person(){ cout << "Dectructor Person" << endl; }
};
class Developer : public Person
{
public:
void Pi() { cout << "Developer Pi" << endl; }
void Print() override
{
cout << "Developer Print" << endl;
}
Developer(){ cout << "Constructor Develoeper" << endl; }
~Developer(){ cout << "Dectructor Develoer" << endl; }
};
int main()
{
Person *p = new Person();
Developer* d = dynamic_cast<Developer*>(p);
d->Pi();
delete p;
delete d;
return 0;
}
Output:
Constructor Person
Developer Pi
Dectructor Person
Why can I invoke Developer
's function Pi
?
How can invoke Pi
without Developer
's Constructor?
Note that Pi
is only declared in class Developer
.