I don't know the sequence of the code running,Please teach me When I create a point A *p=new C, what happen?I even can't understand this equation,why their class are different and it still can be compiled?
#include <iostream>
using namespace std;
class A
{
public:
A( ){cout << "A Constructor" << endl;}
virtual ~A( ){cout << "A Destructor" << endl;}
virtual void f( ){cout << "A::f( )" << endl;}
void g( ){ f( ); }
};
class B:public A
{
public:
B( ){f();cout <<"B Constructor" << endl;}
~B( ){cout << "B Destructor" << endl;}
};
class C:public B
{
public:
C( ){f( ); cout << "C Constructor" << endl;}
~C( ){cout << "C Destructor" << endl;}
void f( ){cout << "C::f( )" << endl;}
};
int main()
{
A *p=new C;
p->g( );
delete p;
}
output is
A Constructor
A::f( )
B Constructor
C::f( )
C Constructor
C::f( )
C Destructor
B Destructor
A Destructor