Problem A: 还会用继承吗?
Problem A: 还会用继承吗?
Description
定义一个Base类,包括1个int类型的属性,以及满足输出格式要求的构造函数、拷贝构造函数和析构函数。
定义Base类的子类Derived,包括1个int类型的属性, 以及满足输出格式要求的构造函数、拷贝构造函数和析构函数。
Input
第1行N>0表示测试用例个数。
每个测试包括2个int类型的整数。
Output
见样例。
#include 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); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~