希望大家见谅 这是我瞎写的,关于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类型分配空间,一定要进行深度复制和重定义赋值函数,否则以后的代码会出现致命的错误。