c++ 基类、派生类都使用了动态内存分配的实例

#include <iostream>

using namespace std;
/* 基类,派生类都使用了动态内存分配,如使用了 new 关键字
* 需要显式析构函数,复制构造函数和赋值构造函数
*/
class baseDMA
{
    private:
        char * data1;
    public:
       baseDMA(const char *d = "default_base"); // 构造函数
       baseDMA(const baseDMA & rs); // 复制构造函数
       ~baseDMA(); // 析构函数
       baseDMA & operator=(const baseDMA & rs);// 赋值构造函数
       virtual void showdata() const;
};

class derivedDMA:public baseDMA
{
    private:
        char * data2;
    public:
       derivedDMA(const char *d1 = "base",const char *d2 = "derived"); // 构造函数
       derivedDMA(const derivedDMA & rs); // 复制构造函数
       ~derivedDMA(); // 析构函数
       derivedDMA & operator=(const derivedDMA & rs);// 赋值构造函数
       void showdata() const;
};


int main()
{
    derivedDMA test1("test1Base","test1Derived");
    test1.showdata(); // 显示 test1 数据
    derivedDMA test2(test1);// 调用复制构造函数
    test2.showdata(); // 显示 test2 数据
    derivedDMA test3;
    test3.showdata(); // 显示 test3 调用赋值构造前的数据
    test3 = test1; // 调用赋值构造函数
    test3.showdata(); // 显示 test3 调用赋值构造后的数据
    return 0;
}

baseDMA::baseDMA(const char * d){
    cout << this << "->构造函数"<< endl;
    int len = strlen(d);
    data1 = new char[len + 1];
    strncpy(data1, d, len);
    data1[len] = '/0';
}

baseDMA::baseDMA(const baseDMA & rs){
    cout << this << "->复制构造函数"<< endl;
    int len = strlen(rs.data1);
    data1 = new char[len + 1];
    strncpy(data1, rs.data1, len);
    data1[len] = '/0';
}

baseDMA::~baseDMA(){
    cout << this << "->析构函数" << endl;
    delete [] data1;
}

baseDMA & baseDMA::operator=(const baseDMA & rs){
    if (this == &rs)
        return *this;
    cout << this << "->赋值构造函数" << endl;
    delete [] data1; // 先清理以前申请的内存
    int len = strlen(rs.data1);
    data1 = new char[len + 1];
    strncpy(data1, rs.data1, len);
    data1[len] = '/0';
    return *this;
}

void baseDMA::showdata() const{
    cout << this << " data1 = " << data1 << endl;
}

derivedDMA::derivedDMA(const char *d1,const char *d2)
:baseDMA(d1) // 初始化列表当中先调用基类构造函数
{
    cout << this << "->构造函数"<< endl;
    int len = strlen(d2);
    data2 = new char[len + 1];
    strncpy(data2, d2, len);
    data2[len] = '/0';
}

derivedDMA::derivedDMA(const derivedDMA & rs)
:baseDMA(rs) // 初始化列当中先调用基类的复制构造函数
{
    cout << this << "->复制构造函数"<< endl;
    int len = strlen(rs.data2);
    data2 = new char[len + 1];
    strncpy(data2, rs.data2, len);
    data2[len] = '/0';
}

derivedDMA::~derivedDMA(){
    cout << this << "->析构函数" << endl;
    delete [] data2;
}

derivedDMA & derivedDMA::operator=(const derivedDMA & rs){
    if (this == &rs)
        return *this;
    cout << this << "->赋值构造函数" << endl;
    baseDMA::operator=(rs);// 先调用基类的赋值构造函数
    delete [] data2; // 先清理以前申请的内存
    int len = strlen(rs.data2);
    data2 = new char[len + 1];
    strncpy(data2, rs.data2, len);
    data2[len] = '/0';
    return *this;
}

void derivedDMA::showdata() const{
    baseDMA::showdata();
    cout << this << " data2 = " << data2 << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值