#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
class Animal
{
public:
virtual void speak() = 0;
Animal()
{
cout << "Animal的构造函数" << endl;
}
//虚析构,这样才可以将cat中堆区数据进行释放
//virtual ~Animal()
//{
// cout << "Animal的析构函数" << endl;
//}
//纯虚析构,类外要声明,这个类变成抽象类
virtual ~Animal() = 0;
};
//纯虚析构类外声明
Animal::~Animal()
{
}
class Cat : public Animal
{
public:
void speak()
{
cout << *name<< "小猫说话" << endl;
}
string* name;
Cat(string _name)
{
cout << "Cat构造函数" << endl;
name = new string(_name);
}
~Cat()
{
cout << "Cat的析构函数" << endl;
if (name != NULL)
{
delete name;
name = NULL;
}
}
};
void test01()
{
Animal* animal = new Cat("Tom");
animal->speak();
delete animal;
}
int main()
{
test01();
return 0;
}