c++多重继承

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值