zoukankan      html  css  js  c++  java
  • YTU 2946: 填空:间接基类就是A

    2946: 填空:间接基类就是A

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 132  解决: 96

    题目描述

    如下程序所示,D继承自B和C,而B和C均继承自A。根据继承的机制,D的对象将拥有其所有“祖先”允许其继承的成员,以及该类新增的成员。main函数也很明确,用输入的整数初始化对象,然后按格式输出对象的成员值。请在begin到end之间补足空白处该填写的成份后,提交begin到end之间的代码。
    #include <iostream>
    using namespace std;
    class A
    {
    protected:
        int x;
    public:
        A(int a)
        {
            x=a;
        }
    };

    //***************begin*****************
    class B: ___(1)_____ A
    {
    protected:
        int y;
    public:
        B(int a,int b):A(a)
        {
            y=b;
        }
    };

    class C:___(2)_____A
    {
    protected:
        int z;
    public:
        C(int a,int b):A(a)
        {
            z=b;
        }
    };

    class D: _________(3)__________
    {
    private:
        int t;
    public:
        D(int a, int b, int c, int d):_________(4)___________{}
        void output()________(5)_________
    };
    //***************end*****************
        void D::output()
        {
            cout<<"x from A: "<<x<<endl;
            cout<<"y from B: "<<y<<endl;
            cout<<"z from C: "<<z<<endl;
            cout<<"t from D: "<<t<<endl;
        }

    int main( )
    {
        int a, b, c, d;
        cin>>a>>b>>c>>d;
        D x(a,b,c,d);
        x.output();
        return 0;
    }

    输入

    4个整数,将在D类对象的构造函数中,分别初始化来自几个类中的数据成员

    输出

    各个类中的数据成员的值

    样例输入

    1 2 3 4

    样例输出

    x from A: 1
    y from B: 2
    z from C: 3
    t from D: 4

    你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

    #include <iostream>
    using namespace std;
    class A
    {
    protected:
        int x;
    public:
        A(int a)
        {
            x=a;
        }
    };
    class B: public A
    {
    protected:
        int y;
    public:
        B(int a,int b):A(a)
        {
            y=b;
        }
    };
    class C:public A
    {
    protected:
        int z;
    public:
        C(int a,int b):A(a)
        {
            z=b;
        }
    };
    class D
    {
    private:
        int x,y,z,t;
    public:
        D(int a, int b, int c, int d):x(a),y(b),z(c),t(d) {}
        void output();
    };
    void D::output()
    {
        cout<<"x from A: "<<x<<endl;
        cout<<"y from B: "<<y<<endl;
        cout<<"z from C: "<<z<<endl;
        cout<<"t from D: "<<t<<endl;
    }
    int main( )
    {
        int a, b, c, d;
        cin>>a>>b>>c>>d;
        D x(a,b,c,d);
        x.output();
        return 0;
    }
    

  • 相关阅读:
    shentou mianshiti
    PHP
    XSS分类&危害&防御
    SQL注入原理&分类&危害&防御
    绕WAF&安全狗新姿势
    IO 模型
    SPC 判异
    [VBA]关于查找方法(Find方法)的应用(一)
    python学习第二十三天 并发编程(线程,进程,协程)
    excel 空单元格在图表中显示的方式 空 0 或者线
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989648.html
Copyright © 2011-2022 走看看