C++类继承new分配空间联系

睡觉希望大家见谅  这是我瞎写的,关于new分配指针空间以及和virtual的练习而已

主函数:

#include <iostream>
#include "CD_pointer.h"
using namespace std;


void Bravo( CD & disk);


int main()
{
    CD c1("Beatles","Capitol",14,35.5);
    classic c2 = classic("Piano Sonata in B flat, Fantasia in C",
                         "Alfred Brendel","Philips",2,57.17);
    CD * pcd = &c1;
    cout << "Using object directly:\n";
    c1.Report();        //Use CD method
    c2.Report();        //Use classic method


    cout << "Using type cd * pointer to objects:\n";
    pcd->Report();
    pcd = &c2;
    pcd->Report();


    cout << "Calling a function with a CD reference argument:\n";
    Bravo(c1);
    Bravo(c2);


    cout << "Testing assignment: ";
    classic copy;
    copy = c2;
    copy.Report();


    return 0;
}
void Bravo( CD & disk)
{
    disk.Report();
}

//创建一个CD_pointer.h头文件

class CD
{               //represent CD disk
private:
    char * performers;
    char * label;
    int selections;     //number of selections
    double playtime;    //playing time in minutes
public:
    CD(const char * s1,const char * s2, int n,double x);
    CD(const CD & cd);
    CD();
    virtual ~CD();
    virtual void Report();   //report all CD data
    CD & operator=(const CD & d);


};


class classic :public CD
{
private:
    char * classic_information;
public:
    classic(const char * c,const char * s1,const char * s2,int n,
                    double x);
    classic(const char * c,const CD & s);
    classic();
    virtual ~classic();
    virtual void Report();
    classic & operator=(const classic & c);


};

//创建一个CD_pointer.cpp

#include <iostream>
#include <cstring>
#include "CD_pointer.h"


CD & CD::operator=(const CD & d)
{
    if(this == &d)
        return *this;
    delete [] performers;
    delete [] label;
    performers = new char[std::strlen(d.performers) + 1];
    label = new char[std::strlen(d.label) + 1];
    selections = d.selections;
    playtime = d.playtime;
    return *this;
}


CD::CD(const CD & cd)
{
     performers = new char[std::strlen(cd.performers) + 1];
     label = new char[std::strlen(cd.label) + 1];
     selections = cd.selections;
     playtime = cd.playtime;
}


void CD::Report()
{
    std::cout << "Performers: " << performers << std::endl;
    std::cout << "Label:      " << label << std::endl;
    std::cout << "Selections: " << selections << std::endl;
    std::cout << "Playtime:   " << playtime << std::endl;
}


CD::~CD()
{
    delete [] performers;
    delete [] label;
}


CD::CD(const char * s1,const char * s2,int n,double x)
{
    performers = new char[std::strlen(s1) + 1];
    std::strcpy(performers, s1);
    label = new char[std::strlen(s2) + 1];
    std::strcpy(label,s2);
    selections = n;
    playtime = x;
}


CD::CD()
{
    performers = new char[std::strlen("Mr.L") + 1];
    std::strcpy(performers,"Mr.L");
}


classic & classic::operator=(const classic & c)
{
    if(this == &c)
        return *this;
    delete [] classic_information;
    CD::operator=(c);
    classic_information = new char[std::strlen(c.classic_information) + 1];
    std::strcpy(classic_information,c.classic_information);
    return *this;
}


void classic::Report()
{
    CD::Report();
    std::cout << "classic_information: " << classic_information << std::endl;
}


classic::~classic()
{
    delete [] classic_information;
}


classic::classic(const char * c,const char * s1,
                 const char * s2,int n,double x) : CD(s1,s2,n,x)
{
    classic_information = new char[std::strlen(c) + 1];
    std::strcpy(classic_information,c);
}


classic::classic(const char * c,const CD & s)
                    : CD(s)
{
    classic_information = new char[std::strlen(c) + 1];
    std::strcpy(classic_information,c);
}


classic::classic() : CD()
{
    classic_information = new char[std::strlen("single") + 1];
    std::strcpy(classic_information,"single");
}

总结:

对于virtual以及运用new分配内存,1:如果一开始没有先实现定义virtual函数,程序可能会报错,所以你要不用担心,自需实现了virtual函数就行,具体导致

原因我也不清楚,毕竟我只是一个小白;2:使用new函数为pointer类型分配空间,一定要进行深度复制重定义赋值函数,否则以后的代码会出现致命的错误。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值