实验12:Problem E: 还会用继承吗?

Home Web Board ProblemSet Standing Status Statistics
 
Problem E: 还会用继承吗?

Problem E: 还会用继承吗?

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 540  Solved: 383
[Submit][Status][Web Board]

Description

定义一个Base类,包括1个int类型的属性,以及满足输出格式要求的构造函数、拷贝构造函数和析构函数。

定义Base类的子类Derived,包括1个int类型的属性, 以及满足输出格式要求的构造函数、拷贝构造函数和析构函数。

 

Input

第1行N>0表示测试用例个数。

每个测试包括2个int类型的整数。

 

Output

见样例。

 

Sample Input

1 10 20

Sample Output

Base = 10 is created. Base = 10 is copied. Base = 10 is created. Derived = 20 is created. Base = 10 is copied. Derived = 20 is copied. Derived = 20 is erased. Base = 10 is erased. Derived = 20 is erased. Base = 10 is erased. Base = 10 is erased. Base = 10 is erased.

HINT

 

Append Code

[Submit][Status][Web Board]
#include<iostream>
using namespace std;
class Base{
public:
    int a;
    Base(int t):a(t){cout<<"Base = "<<a<<" is created."<<endl;}
    ~Base(){cout<<"Base = "<<a<<" is erased."<<endl;}
    Base(const Base& b){a=b.a;cout<<"Base = "<<a<<" is copied."<<endl;}
};
class Derived: public Base{
public:
    int b;
    Derived(int t):Base(t){cout<<"Derived = "<<b<<" is created."<<endl;}
    ~Derived(){cout<<"Derived = "<<b<<" is erased."<<endl;}
    Derived(const Derived& d):Base(d),b(d.b){cout<<"Derived = "<<b<<" is copied."<<endl;}
    Derived(int x,int y):Base(x),b(y){cout<<"Derived = "<<b<<" is created."<<endl;}

};
int main()
{
    int cases, data1, data2;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        cin>>data1>>data2;
        Base base1(data1), base2(base1);

        Derived derived1(data1, data2), derived2(derived1);
    }
}

 

posted @ 2016-05-22 12:03  一一一匹马赛克儿  阅读(758)  评论(0编辑  收藏  举报