OJ 2074 Problem D Base与Derived

本文详细解析了C++中基类和派生类的构造函数与析构函数的实现过程,通过具体示例展示了如何定义带有int类型属性的Base和Derived类,并在构造和析构过程中输出相应的信息。

Description

定义Base和Derived类,Derived类是Base类的子类,两个类都只有1个int类型的属性。定义它们的构造函数和析构函数,输出信息如样例所示。

Input

输入2个整数。

Output

见样例。

Sample Input

100
200

Sample Output

Base 100 is created.
Base 100 is created.
Derived 200 is created.
Derived 200 is created.
Base 100 is created.
Base 100 is created.

HINT

为什么还是在析构函数里面是created!

Append Code

int main()
{
    int a, b;
    cin>>a>>b;
    Base base(a);
    Derived derived(a, b);
    return 0;
}

Accepted Code

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

class Base {
private:
    int num1;
public:
    Base(int n = 0) : num1(n) {
        cout << "Base " << num1 << " is created.\n";
    }
    ~Base() {
        cout << "Base " << num1 << " is created.\n";
    }
};

class Derived : public Base {
private:
    int num2;
public:
    Derived(int n1 = 0, int n2 = 0) : Base(n1), num2(n2) {
        cout << "Derived " << num2 << " is created.\n";
    }
    ~Derived() {
        cout << "Derived " << num2 << " is created.\n";
    }
};
int main()
{
    int a, b;
    cin>>a>>b;
    Base base(a);
    Derived derived(a, b);
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值