您的位置:首页 > 编程语言 > C语言/C++

c++多重继承

2016-06-29 20:14 567 查看
#include<iostream>
#include<stdlib.h>
using namespace std;
class father
{
public:
//father(){ cout << "创建父亲" << endl; }
father(int height);
//father(){}
virtual ~father(){ cout << "析构父亲" << endl; }
virtual void smart()const{ cout << "父亲很聪明" << endl; }
virtual int getheight()const{ return itsheight; }
protected:
int itsheight;
};
father::father(int height) //:itsheight(height)
{
itsheight = height;
cout << "创建父亲" << endl;
}

class mother
{
public:
//mother(){ cout << "创建母亲" << endl; }
mother(bool sex);
//mother(){}
virtual ~mother(){ cout << "析构母亲" << endl; }
virtual void beautiful()const{ cout << "母亲很漂亮" << endl; }
virtual bool getsex()const{ return itssex; }
protected:
bool itssex;
};
mother::mother(bool sex) //:itssex(sex)
{
itssex = sex;
cout << "创建母亲" << endl;
}

class son :public father, public mother
{
public:
//son(){ cout << "创建儿子" << endl; }
son(int, bool, long);
~son(){ cout << "析构儿子" << endl; }
//void smart()const{ cout << "儿子很聪明" << endl; }
//void beautiful()const{ cout << "儿子很帅" << endl; }
virtual long getnum()const{ return num; }
private:
long num;
};
son::son(int height, bool sex, long number):father(height), mother(sex), num(number)
{
//itsheight = height;//使用这种初始化方式时,调用的是基类不带参数的构造函数,若基类不存在不带参数的构造函数,则报错
//itssex = sex;
//num = number;
//father(height);

cout << "创建儿子" << endl;
}

int main()
{
//father*p = new father;
//p->smart();
//delete p;

//father*q = new son;
//q->smart();
//delete q;

//mother*e = new mother;
//e->beautiful();
//delete e;

//mother*r = new son;
//r->beautiful();
//delete r;

son*ps = new son(5, true, 3);
ps->beautiful();
ps->smart();
cout << "小儿子有" << ps->getheight();
cout << "五英尺高" << endl;
delete ps;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: