#include<iostream>
#include<cstring>
class Base
{
char* base;
public:
Base()
{
base = new char[2];
strcpy(base, "1");
cout << "2";
}
~Base()
{
delete[]base;
cout << "3";
}
void Print()
{
cout << base;
}
};
class Derived :public Base
{
char* derived;
public:
Derived()
{
derived = new char[2];
strcpy(derived, "4");
cout <<"5";
}
~Derived()
{
delete[]derived;
}
};
int main()
{
Base* ptr = new Derived;
ptr->Print();
delete ptr;
return 0;
}
答案是2513
Base* ptr = new Derived; 是什么意思呀?Derived不是类的名称吗 为什么会被new创建出来呢?
还有ptr->Print();调用的为什么是基类中的函数呢?(输出的是1)
最后delete ptr; 又为什么调用的是基类的析构函数呢?析构不是派生类先调用吗?
麻烦各位看看解答一下疑惑,谢谢!!感激不尽!!