C++类继承CD练习

来源:互联网 发布:ipadmini2怎么下载软件 编辑:程序博客网 时间:2024/06/18 07:41

这个只是我C++ primer plus类继承练习

编写一个CD程序

#include <iostream>
#include "CD.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.h

class CD
{               //represent CD disk
private:
    char performers[50];
    char label[20];
    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:
    int size;
    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.cpp

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


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


CD::CD(const CD & cd)
{
   std::strcpy(performers,cd.performers);
   std::strcpy(label,cd.label);
   selections = cd.selections;
   playtime = cd.playtime;
}
CD::CD()
{
    std::strcpy(performers,"Alice");
    std::strcpy(label,"love");
    selections = 1;
    playtime = 3;
}
void CD::Report()
{
    std::cout << "Performer: " << performers << std::endl;
    std::cout << "Label:     " << label << std::endl;
    std::cout << "selections: " << selections << std::endl;
    std::cout << "playtime:  " << playtime << std::endl;
}
CD::~CD()
{
    std::cout << "Goodbye!\n";
}
CD & CD::operator=(const CD & d)
{
   std::strcpy(performers,d.performers);
   std::strcpy(label,d.label);
   selections = d.selections;
   playtime = d.playtime;
   return *this;
}


classic::classic(const char * c,const char * s1,const char * s2,int n,
        double x) : CD(s1,s2,n,x)
{
    size = std::strlen(c);
    classic_information = new char[size + 1];
    std::strcpy(classic_information, c);
}
classic::classic(const char * c,const CD & s) : CD(s)
{
    size = std::strlen(c);
    classic_information = new char[size + 1];
    std::strcpy(classic_information, c);
}
classic::classic() : CD()
{
    size = std::strlen("I came form US");
    classic_information = new char[size + 1];
    std::strcpy(classic_information,"I came form US");
}
classic::~classic()
{
    delete [] classic_information;
    std::cout << "Goodbye!" << std::endl;
}
void classic::Report()
{
    CD::Report();
    std::cout << classic_information << std::endl;
}
classic & classic::operator=(const classic & c)
{
    if(this == &c)
        return *this;
    CD::operator=(c);
    delete [] classic_information;
    size = std::strlen(c.classic_information);
    classic_information = new char[size + 1];
    std::strcpy(classic_information, c.classic_information);
    return *this;
}

总结:对于类继承的虚函数,如果一开始没有先编写虚函数会系统会显示出一个错误,所以请先编写虚函数,

派生类初始化基类,编写初始化,定义时不需初始化,否则会出错,对于使用了指针new分配内存的实例变量

应使用深度复制就是复制他的变量参数而不是传递地址,还要重定义赋值=函数,不然就会出现致命的错误。

应该

这只是一个简单的练习内容

原创粉丝点击