0
点赞
收藏
分享

微信扫一扫

Problem A: 还会用继承吗?


Problem A: 还会用继承吗?


Description

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

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


Input

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

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


Output

见样例。



#include <iostream>

using namespace std;

class Base{
private:
int x;
public:
Base(int a) : x(a) {
cout << "Base = " << x << " is created." << endl;
}
Base(const Base &b) {
x = b.x;
cout << "Base = " << x << " is copied." << endl;
}
~Base() {
cout << "Base = " << x << " is erased." << endl;
}
};

class Derived : public Base {
private:
int x;
public:
Derived(int a, int b) : Base(a), x(b) {
cout << "Derived = " << x << " is created." << endl;
}
Derived(const Derived &d) : Base(d) {
x = d.x;
cout << "Derived = " << x << " is copied." << endl;
}
~Derived() {
cout << "Derived = " << x << " is erased." << 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);
}
}



举报

相关推荐

0 条评论