13.11-1

可能发生中断:
char m_name[100];


class Cd
{
public:
	Cd(char * s1, char * s2, int n, double x);
	Cd(const Cd & d);
	Cd();
	virtual~ Cd();
	virtual void Report()const;
	virtual Cd & operator=(const Cd & d);
protected:
private:
	char m_performer[50];
	char m_label[20];
	int m_selections;
	double m_playtime;
};
class Classic:public Cd
{
public:
	Classic(char *name, char * s1, char * s2, int n, double x) ;
	Classic::Classic(char *s, Cd&c) ;

	Classic(Classic&c) ;
	virtual void Report()const;
	virtual Classic & operator=(const Classic & d);
	Classic();
	~Classic();
protected:
private:
	char m_name[100];
};

可能发生中断:长度+1处。
strcpy_s(m_label, strlen(d.m_label)+1,d.m_label);

#include 
#include "Classic.h"
using namespace std;
//cd method
Cd::Cd(char * s1, char * s2, int n, double x)
{
	int len1 = strlen(s1);
	strcpy_s(m_label, len1 + 1, s1);
	int len2 = strlen(s2);
	strcpy_s(m_performer, len2 + 1, s2);
	m_selections = n;
	m_playtime = x;
}
Cd::Cd(const Cd & d)
{
	strcpy_s(m_label,strlen(d.m_label)+1 ,d.m_label);
	strcpy_s(m_performer,strlen(d.m_performer)+1, d.m_performer);
	this->m_playtime = d.m_playtime;
	this->m_selections = d.m_selections;
}
Cd::Cd()
{
	 m_performer[50] = '\0';
	 m_label[20]='\0';
	 m_selections = 0;
	 m_playtime = 0;
}
Cd::~Cd()
{
	
}
void Cd::Report()const
{
	std::cout << m_performer << " " << m_label << " " << m_selections << " " << m_playtime << std::endl;;
}
Cd & Cd::operator=(const Cd & d)
{
	if (this==&d)
	{
		return *this;
	}
	strcpy_s(m_label, strlen(d.m_label)+1,d.m_label);
	strcpy_s(m_performer,strlen(d.m_performer)+1, d.m_performer);
	m_playtime = d.m_playtime;
	m_selections = d.m_selections;
	return*this;
}

//classic method
Classic::Classic(char *name, char * s1, char * s2, int n, double x) :Cd(s1, s2, n, x)
{
	strcpy_s(m_name, strlen(name)+1, name);
}
Classic::Classic(char *name,Cd&c) : Cd(c)
{
	strcpy_s(m_name, strlen(name)+1, name);
}
Classic::Classic() : Cd()
{
	m_name[100] = '\0';
}
Classic::~Classic()
{

}
void Classic::Report()const
{
	Cd::Report();
	cout << m_name << endl;
}

Classic &  Classic::operator=(const Classic & d) 
{
	if (this == &d)
	{
		return *this;
	}
	Cd::operator=(d);
	strcpy_s(m_name, strlen(d.m_name)+1,d.m_name);
	return*this;
}

#include 
#include "Classic.h"

using namespace std;

void Bravo(const Cd& disk);
void 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();
	c2.Report();
	cout << "Using type cd * pointer to object:\n";
	pcd->Report();//1.中断:char m_name[20]太小;
	pcd = &c2;
	pcd->Report();

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

	cout << "Testing assignment: ";
	Classic copy;
	copy = c2;		//2.重载=操作符
	copy.Report();
	cout<<"hello.."<<endl;
	system("pause");
	return ;
}

void Bravo(const Cd& disk)
{
	disk.Report();
}

m_performer = new char[strlen(d.m_performer) + 1];
strcpy_s(m_performer, strlen(d.m_performer) + 1,d.m_performer);

13.11-1_第1张图片

你可能感兴趣的:(C++,Primer,Plus,6th)