无法运行 关于一个类继承,该如何解决
无法运行 关于一个类继承
代码
头文件
#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();
}
------解决思路----------------------
term的strcopy也是不对的
下面代码, 复制到1个文件中就可以了.
头文件
#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();
}
------解决思路----------------------
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();
}