#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
Animal();
Animal(string name);
string mouth;
void eat();
void sleep();
void drool();
string name;
};
class Pig : public Animal
{
public:
Pig();
Pig(string name);
void climb();
};
class Turtle : public Animal
{
public:
Turtle();
Turtle(string name);
void swim();
};
Animal::Animal()
{
cout << "父类的构造函数先调用" << endl;
this->name = "NULL ";
}
Animal::Animal(string name){
this->name = name;
}
void Animal::eat()
{
cout << name << "I am eating " << endl;
}
void Animal::sleep()
{
cout << name << "I am sleeping , don't disturb me " << endl;
}
void Animal::drool()
{
cout << name << " i am female , when i look male , i will flow water" << endl;
}
Pig::Pig(){
cout << name << "子类Pig的构造器后被调用" << endl;
}
Pig::Pig(string name) : Animal(name) //继承了子类的构造器 :
{
cout << name << "子类Pig的构造器后被调用" << endl;
}
void Pig::climb()
{
cout << name << " 我是一个漂亮的母猪, 我会上树, 我正在上树" << endl;
}
Turtle::Turtle(){
cout << "子类Turtle的构造器后被调用" << endl;
}
Turtle::Turtle(string name) : Animal(name) //继承了子类的构造函数 :
{
cout << "子类Turtle的构造器后被调用" << endl;
}
void Turtle::swim()
{
cout << "我是一只小甲鱼,当母猪要来抓我的时候,我就游到海里。。 哈哈" << endl;
}
int main()
{
Pig pig("亨利");
Turtle turtle("龟仙人");
pig.eat();
turtle.eat();
pig.climb();
turtle.swim();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class BaseClass
{
public:
BaseClass();
~BaseClass();
void dosth();
};
class SubClass : public BaseClass
{
public:
SubClass();
~SubClass();
};
BaseClass::BaseClass()
{
cout << "进入基类构造器" << endl;
cout << "我在基类构造器干了些事" << endl;
}
BaseClass::~BaseClass()
{
cout << "进入基类析构器" << endl;
cout << "我在基类的析构器干了些事" << endl;
}
void BaseClass::dosth()
{
cout << "我干了些事" << endl;
}
SubClass::SubClass()
{
cout << "进入子类构造器" << endl;
cout << "我在子类构造器干了些事" << endl;
}
SubClass::~SubClass()
{
cout << "进入子类析构器" << endl;
cout << "我在子类的析构器干了些事" << endl;
}
int main()
{
SubClass sub;
sub.dosth();
cout << "程序运行结束" << endl;
return 0;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */