#include<iostream>
using namespace std;
class Animal
{
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
virtual void Speak() = 0;
~Animal()
{
cout << "Animal析构函数调用!" << endl;
}
};
class Cat : public Animal
{
public:
Cat(string name)
{
cout << "Cat构造函数调用!" << endl;
m_Name = new string(name);
}
virtual void Speak()
{
cout << *m_Name << "小猫在说话!" << endl;
}
~Cat()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
public:
string* m_Name;
};
void test06()
{
Animal* animal = new Cat("Tom");
animal->Speak();
delete animal;
}
int main()
{
test06();
system("pause");
return 0;
}
using namespace std;
class Animal
{
public:
Animal()
{
cout << "Animal 构造函数调用!" << endl;
}
virtual void Speak() = 0;
~Animal()
{
cout << "Animal析构函数调用!" << endl;
}
};
class Cat : public Animal
{
public:
Cat(string name)
{
cout << "Cat构造函数调用!" << endl;
m_Name = new string(name);
}
virtual void Speak()
{
cout << *m_Name << "小猫在说话!" << endl;
}
~Cat()
{
cout << "Cat析构函数调用!" << endl;
if (this->m_Name != NULL) {
delete m_Name;
m_Name = NULL;
}
}
public:
string* m_Name;
};
void test06()
{
Animal* animal = new Cat("Tom");
animal->Speak();
delete animal;
}
int main()
{
test06();
system("pause");
return 0;
}