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);
}
}