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分配内存的实例变量
应使用深度复制就是复制他的变量参数而不是传递地址,还要重定义赋值=函数,不然就会出现致命的错误。
应该
这只是一个简单的练习内容
- C++类继承CD练习
- 类多继承及包含练习(c++primer plus)
- C/C++_log2000_继承的练习1
- C++练习继承类
- 类的继承练习
- #小练习 类与继承
- Python练习代码 -- 类, 继承
- 13章 类继承练习
- 继承和抽象类练习
- 继承练习
- Objective-C学习 继承之僵尸练习
- [[ 江南style]]---->类的继承 java练习
- 动物叫(练习继承,抽象类)
- C++类的继承练习(1)
- C++面向对象-类继承练习
- Python类的继承的练习
- C++-类的继承
- C++Primer(类继承)
- gloox开发环境搭建(转)
- 《让她降落》
- “由于这台计算机没有终端服务器客…
- 设置SQL Server数据库AWE机制…
- SGM调试小记
- C++类继承CD练习
- 详细讲述 SQL SERVER&nb…
- 浅谈SQL Server 对于内存的管理
- **openpyxl修改表格中的公式自动求和autoSUM
- 模型淡出Shader实现
- SQL Server连接中的四个最常…
- python
- 网络术语解释
- 硬件能力与智能AI-Zoomla!逐浪CMS2 x3.9.2正式发布