无法运行 关于一个类继承

dentiscalprum 2015-03-26 11:51:46
代码
头文件
#ifndef Cd
#define Cd
class cD
{
private:
char performers[20];
char label[20];
int selections;
double playtime;
public:
cD(char* s1, char* s2, int n, double x);
cD(const cD & d);
cD();
virtual ~cD();
virtual void Report() const;
cD & operator=(const cD& d);
};

class Classic: public cD
{
private:
char * term;
public:
Classic(char *s1, char *s2, char * s3, int n, double x);
Classic(const Classic &d);
Classic();
~Classic();
void Report() const;
Classic & operator=(const Classic & d);
};


#endif

实现
#include "Cd.h"
#include <iostream>
#include <cstring>

cD::cD(char* s1, char* s2, int n, double d): selections(n), playtime(d)
{
strcpy_s(performers, s1);
strcpy(label, s2);
};

cD::cD(const cD & c)
{
strcpy_s(performers, c.performers);
strcpy_s(label, c.label);
playtime = c.playtime;
selections = c.selections;
}

cD::cD()
{
performers[0] = 0;
label[0] = 0;
}

cD::~cD()
{
}
void cD::Report()const
{
std::cout << "电影名字 : " << performers << std::endl
<< "类型 : " << label << std::endl
<< "版本 : " << selections << std::endl
<< "时间 : " << playtime << std::endl;
}

cD & cD::operator=(const cD & c)
{
strcpy_s(performers, c.performers);
strcpy_s(label, c.label);
selections = c.selections;
playtime = c.playtime;
return *this;
}

Classic::Classic(char * s1, char * s2,char * s3, int n, double b):cD(s1, s2, n, b)
{
int len = strlen(s3);
term = new char[len + 1];
strcpy(term, s3);
}

Classic::Classic(const Classic & c):cD(c)
{
int len = strlen(c.term);
term = new char[len + 1];
strcpy_s(term,len, c.term);
}
Classic::Classic()
{
term = new char[1];
term[0] = '\0';
}

void Classic::Report()const
{
cD::Report();
std::cout << "主要作品 : " << term << std::endl;
}

Classic::~Classic()
{
delete [] term;
}

Classic& Classic::operator=(const Classic& c)
{
if(this == &c) return *this;
cD::operator=(c);
delete[] term;
int len = strlen(c.term);
term = new char[len + 1];
strcpy_s(term,len, c.term);
return * this;
}
函数
#include <iostream>
#include "Cd.h"

void Bravo(const cD & disk);

int main()
{
using namespace std;
cD c1("Beatles", "Captional", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat. Fantasia in C", "Alfred Brendel", "Philips", 2, 51.57);

cD *pcd = &c1;

cout << "using object directly:\n";
c1.Report();
c2.Report();

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);
Classic copy;
copy = c2;
copy.Report();
cin.get();
cin.get();

return 0;
}

void Bravo(const cD&disk)
{
disk.Report();
}
...全文
245 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiht594 2015-03-28
  • 打赏
  • 举报
回复
term的strcopy也是不对的 下面代码, 复制到1个文件中就可以了.
// console.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#ifndef Cd
#define Cd
class cD
{
private:
	char performers[20];
	char label[20];
	int selections;
	double playtime;
public:
	cD(char* s1, char* s2, int n, double x);
	cD(const cD & d);
	cD();
	virtual ~cD();
	virtual void Report() const;
	cD &  operator=(const cD& d);
};

class Classic : public cD
{
private:
	char * term;
public:
	Classic(char *s1, char *s2, char * s3, int n, double x);
	Classic(const Classic &d);
	Classic();
	~Classic();
	void Report() const;
	Classic & operator=(const Classic & d);
};


#endif

#include <iostream>
#include <cstring>

cD::cD(char* s1, char* s2, int n, double d) : selections(n), playtime(d)
{
	strcpy_s(performers, s1);
	strcpy_s(label, s2);
};

cD::cD(const cD & c)
{
	strcpy_s(performers, c.performers);
	strcpy_s(label, c.label);
	playtime = c.playtime;
	selections = c.selections;
}

cD::cD()
{
	performers[0] = 0;
	label[0] = 0;
}

cD::~cD()
{
}
void cD::Report()const
{
	std::cout << "电影名字 : " << performers << std::endl
		<< "类型 : " << label << std::endl
		<< "版本 : " << selections << std::endl
		<< "时间 : " << playtime << std::endl;
}

cD & cD::operator=(const cD & c)
{
	strcpy_s(performers, c.performers);
	strcpy_s(label, c.label);
	selections = c.selections;
	playtime = c.playtime;
	return *this;
}

Classic::Classic(char * s1, char * s2, char * s3, int n, double b) :cD(s1, s2, n, b)
{
	int len = strlen(s3);
	term = new char[len + 1];
	memset(term, 0, len + 1);
	strcpy_s(term, len+1,s3);
}

Classic::Classic(const Classic & c) :cD(c)
{
	int len = strlen(c.term);
	term = new char[len + 1];
	strcpy_s(term, len+1, c.term);
}
Classic::Classic()
{
	term = new char[1];
	term[0] = '\0';
}

void Classic::Report()const
{
	cD::Report();
	std::cout << "主要作品 : " << term << std::endl;
}

Classic::~Classic()
{
	delete[] term;
}

Classic& Classic::operator=(const Classic& c)
{
	if (this == &c) 
		return *this;
	cD::operator=(c);
	delete[] term;
	int len = strlen(c.term);
	term = new char[len + 1];
	strcpy_s(term, len+1, c.term);
	return *this;
}
#include <iostream>

void Bravo(const cD & disk);

int main()
{
	using namespace std;
	cD c1("Beatles", "Captional", 14, 35.5);
	Classic c2 = Classic("Piano Sonat", "Alfred Brendel", "Philips", 2, 51.57);

	cD *pcd = &c1;

	cout << "using object directly:\n";
	c1.Report();
	c2.Report();

	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);
	Classic copy;
	copy = c2;
	copy.Report();
	cin.get();
	return 0;
}

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

dentiscalprum 2015-03-28
  • 打赏
  • 举报
回复
有没有大神啊
dentiscalprum 2015-03-28
  • 打赏
  • 举报
回复
引用 10 楼 jiht594 的回复:
[quote=引用 9 楼 q849179433 的回复:] [quote=引用 7 楼 jiht594 的回复:] 你要比较一下2个长度, 超过了就不copy 或者 strncpy_s(performers, s1, _countof(performers)-1); 只copy部分数据(performers长度-1, 最后1个留给'\0')
怎么比较 字母个数也没超过20 啊[/quote] 超了. if (strlen(s1) < 20) strcpy_s(performers, s1); else printf("...")//这设置个断点试试[/quote]用了 但是还会错误
jiht594 2015-03-27
  • 打赏
  • 举报
回复
引用 1 楼 jiht594 的回复:
strcpy_s有越界检查, 错误提示也说了"buffer is too small" strcpy_s(performers, s1); 所以要保证performers能存下, c.performers里面的字符串(包括\0), 否则就崩漏 可以使用strncpy_s(performers, s1, _countof(performers)-1);, 这样做会导致不是全部字符都拷贝到第一个参数
strcpy_s(performers, s1);需要你自己来保证 performers能存下s1, 否则就崩
jiht594 2015-03-27
  • 打赏
  • 举报
回复
strcpy_s有越界检查, 错误提示也说了"buffer is too small" strcpy_s(performers, s1); 所以要保证performers能存下, c.performers里面的字符串(包括\0), 否则就崩漏 可以使用strncpy_s(performers, s1, _countof(performers)-1);, 这样做会导致不是全部字符都拷贝到第一个参数
dentiscalprum 2015-03-27
  • 打赏
  • 举报
回复
引用 11 楼 jiht594 的回复:
Piano Sonata in B flat. Fantasia in C 数数多少个字符
我删到10个以内了
jiht594 2015-03-27
  • 打赏
  • 举报
回复
Piano Sonata in B flat. Fantasia in C 数数多少个字符
jiht594 2015-03-27
  • 打赏
  • 举报
回复
引用 9 楼 q849179433 的回复:
[quote=引用 7 楼 jiht594 的回复:] 你要比较一下2个长度, 超过了就不copy 或者 strncpy_s(performers, s1, _countof(performers)-1); 只copy部分数据(performers长度-1, 最后1个留给'\0')
怎么比较 字母个数也没超过20 啊[/quote] 超了. if (strlen(s1) < 20) strcpy_s(performers, s1); else printf("...")//这设置个断点试试
dentiscalprum 2015-03-27
  • 打赏
  • 举报
回复
引用 7 楼 jiht594 的回复:
你要比较一下2个长度, 超过了就不copy 或者 strncpy_s(performers, s1, _countof(performers)-1); 只copy部分数据(performers长度-1, 最后1个留给'\0')
怎么比较 字母个数也没超过20 啊
jiht594 2015-03-27
  • 打赏
  • 举报
回复
或者使用stl的string或者微软的CStrng等, 可以自动伸缩长度的字符串类. 也可以自己动手实现. 不过没必要
jiht594 2015-03-27
  • 打赏
  • 举报
回复
你要比较一下2个长度, 超过了就不copy 或者 strncpy_s(performers, s1, _countof(performers)-1); 只copy部分数据(performers长度-1, 最后1个留给'\0')
penghuahuijuan 2015-03-27
  • 打赏
  • 举报
回复
strcpy_s(performers, s1); 所以要保证performers足够大
dentiscalprum 2015-03-27
  • 打赏
  • 举报
回复
引用 4 楼 u014260892 的回复:
strcpy_s(performers, s1); 所以要保证performers足够大
要怎么改呢
dentiscalprum 2015-03-27
  • 打赏
  • 举报
回复
引用 3 楼 LINUX2012LINUX 的回复:
提示的很明显,缓冲区太小了!
要怎么改呢 菜鸟
principl 2015-03-27
  • 打赏
  • 举报
回复
提示的很明显,缓冲区太小了!

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧