CDerived* pd;
pd->print();
The pointer is not initialized -> undefined.
You need
CDerived* pd = new CDerived;
pd->print();
In addition, it does not work normally. Rather, you are not lucky that everything is in order. Virtual submission requires a virtual table, and since the pointer is not initialized, the virtual table pointer does not exist, so it crashes when the functions are virtual.
, - undefined , , .
, :
class CBase {
public:
int y;
void print()
{
cout<<"In base print func\n" << y;
};
};
class CDerived: public CBase {
public:
int x;
void print()
{
cout<<"In derived print func\n" << x;
};
};
, virtual.