// 通過動物類實例化狗類
Animal *p = new Dog("dog");
// 調(diào)用成員函數(shù)
p->eat();
p->move();
// 釋放內(nèi)存
delete p;
p = NULL;
return 0;
Animal *p = new Dog("dog");
// 調(diào)用成員函數(shù)
p->eat();
p->move();
// 釋放內(nèi)存
delete p;
p = NULL;
return 0;
2017-05-10
// 虛析構(gòu)函數(shù)
virtual ~Animal(){cout << "~Animal" << endl;}
// 虛成員函數(shù)
void eat(){cout << "Animal--" << m_strName << "-- eat" << endl;}
// 純虛函數(shù)
virtual void move() {cout<<"move()"<<endl;}
public:
// 數(shù)據(jù)成員
string m_strName;
};
virtual ~Animal(){cout << "~Animal" << endl;}
// 虛成員函數(shù)
void eat(){cout << "Animal--" << m_strName << "-- eat" << endl;}
// 純虛函數(shù)
virtual void move() {cout<<"move()"<<endl;}
public:
// 數(shù)據(jù)成員
string m_strName;
};
2017-05-10
class Animal
{
public:
// 默認(rèn)構(gòu)造函數(shù)
Animal()
{};
// 含參構(gòu)造函數(shù)
Animal(string name){m_strName = name; cout << "Animal" << endl;}
{
public:
// 默認(rèn)構(gòu)造函數(shù)
Animal()
{};
// 含參構(gòu)造函數(shù)
Animal(string name){m_strName = name; cout << "Animal" << endl;}
2017-05-10