带参数的继承类构造方法1.0


#include<iostream>
using namespace std;
class Father
{
    public:
    
        Father(string n)
        {   name=n;
            cout<<"构造Father"<<endl;}
        ~Father()
        {cout<<"析构Father"<<endl;}
        void show()
        {cout<<"father's name-------------"<<name<<endl;}
    private:
        string name;
};
class Mother
{
    public:
    
        Mother(string n)
        {
            name=n;cout<<"构造Mother"<<endl;}
        ~Mother()
        {cout<<"析构Mother"<<endl;}
        void show()
        {cout<<"mother's name-------------"<<name<<endl;}
    private:
        string name;
};

class Son:public Mother,public Father
{
    public:
    
        Son(string n,string m,string f):Mother(m),Father(f)//============先构造的是父类,所以要先初始化父类的参数。
        {
            name=n;
            cout<<"构造son"<<endl;}
        ~Son()
        {cout<<"析构son"<<endl;}
    void show()
        {cout<<"son's name------------"<<name<<endl;}
    private:
        string name;


};
int main()
{
    Son s("son","mother","father");
    Son *p=&s;     p->show();
    Mother *q=&s;  q->show();
    Father *w=&s;  w->show();
}

======================================================================


#include <iostream>

using namespace std;


class grand
{
        double money;
    public:
        grand(double m)
        {
            money = m;
            cout << "create grand" << endl;
        }

        void look()
        {
            cout << "money=" << money << endl;
        }

};

class father:virtual public grand
{
    string first_name;
    public:
        father(string fn,double q):grand(q)
        {
            first_name = fn;
            cout << "create father" << endl;
        }
        void play_piano()
        {
            cout << "弹钢琴" << endl;
        }
};

class mother:virtual public grand
{
    string last_name;
    public:
        mother(string ln,double d):grand(d)
        {
            last_name = ln;
            cout << "create mother" << endl;
        }
        void dance()
        {
            cout << "会跳舞" << endl;
        }
};

class uncle
{
    public:
        void play_card()
        {
            cout << "打牌" << endl;
        }
};
class you:public father,public mother
{
    public:
        you(string s1,string s2,double d):father(s1,d),mother(s2,d),grand(d)
        {
            cout << "you" << endl;
        }
};

int main()
{
    you u("Job","Smith",1000);
    u.play_piano();
    u.dance();

    //u.father::look();
    u.look();}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值