• C7-2 多继承 (100/100 分数)


    题目描述

    下面的代码声明了三个基类Base1、Base2和Base3,然后从这三个基类按照公有方式派生出类Derived。在每个类中分别定义带一个整型参数的构造函数和析构函数输出提示信息,构造函数的提示信息中需要包含整型参数的数值。请将下面的代码补充完整,使得输出结果与样例输出相同,注意:测试数据有多组。

        #include <iostream>
    using namespace std;

    class Base1
    {
    public:
    Base1(int x);
    ~Base1();
    };

    class Base2
    {
    public:
    Base2(int x);
    ~Base2();
    };
    class Base3
    {
    public:
    Base3(int x);
    ~Base3();
    };

    class Derived://继承上面3个类
    {
    public:
    Derived(int x1, int x2, int x3, int x4);
    ~Derived();
    };

    Base1::Base1(int x)
    {
    cout<<"Base1 constructor called "<<x<<endl;
    }

    Base1::~Base1()
    {
    cout<<"Base1 destructor called"<<endl;
    }
    //依照Base1类中的代码实现其它类的构造函数和析构函数


    int main()
    {
    int x[4];
    for (int i = 0; i < 4; ++i)
    cin >> x[i];
    Derived d(x[0], x[1], x[2], x[3]);
    return 0;
    }



    输入描述

    每组输入为 4 个整数用空格隔开



    输出描述

    根据构造和析构函数的调用顺序输出



    样例输入

    1 2 3 4


    样例输出

    Base2 constructor called 3
    Base1 constructor called 2
    Base3 constructor called 4
    Derived constructor called 1
    Derived destructor called
    Base3 destructor called
    Base1 destructor called
    Base2 destructor called
    /*students please write your program here*/
        #include <iostream>
        using namespace std;
       
        class Base1
        {
        public:
            Base1(int x);
            ~Base1();
        };
       
        class Base2
        {
        public:
            Base2(int x);
            ~Base2();
        };
        class Base3
        {
        public:
            Base3(int x);
            ~Base3();
        };
        
        class Derived: public Base2,public Base1,public Base3 //继承上面3个类
        {
        public:
            Derived(int x1, int x2, int x3, int x4);
            ~Derived();
        };
    
        Base1::Base1(int x)
        {
            cout<<"Base1 constructor called "<<x<<endl;
        }
       
        Base1::~Base1()
        {
            cout<<"Base1 destructor called"<<endl;
        }
        //依照Base1类中的代码实现其它类的构造函数和析构函数 
    
        Base2::Base2(int x)
        {
            cout<<"Base2 constructor called "<<x<<endl;
        }
       
        Base2::~Base2()
        {
            cout<<"Base2 destructor called"<<endl;
        }
    
        Base3::Base3(int x)
        {
            cout<<"Base3 constructor called "<<x<<endl;
        }
       
        Base3::~Base3()
        {
            cout<<"Base3 destructor called"<<endl;
        }
    
        Derived::Derived(int x1, int x2, int x3, int x4):Base1( x2),Base2( x3),Base3( x4)
        {
            cout<<"Derived constructor called "<<x1<<endl;
        }
    
        Derived::~Derived()
        {
            cout<<"Derived destructor called"<<endl;
        }
       
        int main()
        {
            int x[4];
            for (int i = 0; i < 4; ++i)
                cin >> x[i];
            Derived d(x[0], x[1], x[2], x[3]);
            return 0;
        }
  • 相关阅读:
    jquery 获取 input type radio checked的元素
    各种js验证规则
    centos7 vsftp xftp 解决无法显示远程文件夹,可登陆
    js浮点运算精度丢失的解决办法
    ThinkPHP 改装后的分页类
    亚马逊AWS开启之路
    上慕课从这里开始 (www-mooc.com)
    iptables httpd.conf详解
    微信带链接的文本消息推送
    IE8不支持响应式设计解决方法
  • 原文地址:https://www.cnblogs.com/qianxuejin/p/9050379.html
Copyright © 2020-2023  润新知