c++primer plus 第13章 编程题第2题
#pragma once #ifndef CD_H_ #define CD_H_ //base classclass Cd { private:char * performers;char * label;int selection; //number of selectiondouble playtime; public:Cd();Cd(const Cd & d);Cd(char * s1, char * s2, int n, double x);virtual ~Cd();virtual void Report() const; //reports all cd dataCd & operator= (const Cd & d); }; #endif // !CD_H_ #pragma once
CD.h
#pragma once #ifndef CLASSIC_H_ #define CLASSIC_H_ #include"CD.h"class Classic : public Cd { private:char * mainprod; public:Classic();Classic(char * s1, char * s2, char * s3, int n, double x);Classic(const Classic & a);~Classic();Classic & operator=(const Classic & a);void Report() const; };#endif // !CLASSIC_H_
classic.h
#include"classic.h" #include<cstring> #include<iostream> using std::strcpy; using std::strlen; using std::cout; using std::endl; //base cd Cd::Cd() {performers = new char[1];performers[0] = '\0';label = new char[1];label[0] = '\0'; //构造函数保持统一格式对应析构函数selection = 0;playtime = 0.0; }Cd::Cd(const Cd & a) {int len;len = strlen(a.performers);performers = new char[len + 1];strcpy(performers, a.performers);len = strlen(a.label);label = new char[len + 1];strcpy(label, a.label);selection = a.selection;playtime = a.playtime; }Cd & Cd::operator=(const Cd & a) {//记住第一步要先delete掉以前的,节省内存delete[]performers;delete[]label;int len;len = strlen(a.performers);performers = new char[len + 1];strcpy(performers, a.performers);len = strlen(a.label);label = new char[len + 1];strcpy(label, a.label);selection = a.selection;playtime = a.playtime;return *this; }Cd::Cd(char * s1, char * s2, int n, double x) {int len;len = strlen(s1);performers = new char[len + 1]; //记得要加一,strlen不算‘\0’ strcpy(performers, s1);len = strlen(s2);label = new char[len + 1];strcpy(label, s2);selection = n;playtime = x; }Cd::~Cd() {delete[]performers;delete[]label; }void Cd::Report() const {cout << "performers: " << performers << endl;cout << " label: " << label << endl;cout << " selection: " << selection << endl;cout << " playtime: " << playtime << endl; }Classic::~Classic() {//Cd::~Cd;//这句不用,写了报错,重复删除delete[]mainprod;//派生析构只用删除派生类里的新成员就好 }Classic::Classic() : Cd() {mainprod = new char[1];mainprod[0] = '\0'; //构造函数要统一型式以兼容析构函数 }Classic::Classic(char * s1, char * s2, char * s3, int a, double x) : Cd(s1, s2, a, x) {int len;len = strlen(s3);mainprod = new char[len + 1];strcpy(mainprod, s3); }Classic::Classic(const Classic & a) : Cd(a) {int len;len = strlen(a.mainprod);mainprod = new char[len + 1];strcpy(mainprod, a.mainprod); }Classic & Classic::operator=(const Classic & a) {//先要用基类的重载= 给基类部分赋值Cd::operator=(a);delete[]mainprod;int len;len = strlen(a.mainprod);mainprod = new char[len + 1];strcpy(mainprod, a.mainprod);//别忘记要返回值return *this; }void Classic::Report() const {Cd::Report();cout << " mainproduction: " << mainprod << endl; }
method.cpp
#include<iostream> using namespace std; #include"classic.h" //will contain #include cd.h void Bravo(const Cd & disk); //记住要自己修改cd.h文件 int main() {Cd c1("Beatles", "Capitol", 14, 35.5);Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philios", 2, 57.17);Cd * pcd = &c1;cout << "using object directly:\n";c1.Report();c2.Report();//Cd sd; cout << "using type cd * pointer to object:\n";pcd->Report();pcd = &c2;pcd->Report();cout << "Calling a function with a Cd reference argument:\n";Bravo(c1);Bravo(c2);cout << "testing assignment" << endl;Classic copy;copy = c2;copy.Report();return 0; }void Bravo(const Cd & disk) {disk.Report(); }
test.cpp
先调用基类构造,再调用派生类构造,析构函数则相反,先调用派生类析构,再调用基类的析构函数。
派生类的析构只用删除派生里的新成员,而不用管基类部分,因为派生类析构函数会自动调用基类的析构函数,所以他自身的职责是对派生类的构造函数执行工作的清理
在method重载赋值运算符函数里应当加上
if(this == &a)
{
return *this;
}//简化情况是否是自身
//return完以后后续的语句不会执行
转载于:https://www.cnblogs.com/syne-cllf/p/9266589.html
c++primer plus 第13章 编程题第2题相关推荐
- c++primer plus 第11章 编程题第7题
c++primer plus 第11章 编程题第7题 #pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream ...
- C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上
/*************************************************************************************************** ...
- C++ Primer Plus 第七章编程题练习
C++ Primer Plus 第七章编程题练习 第一题 题目描述 编写一个程序,不断要求用户输入两个数,直到其中的一个为0.对于每两个数,程序将使用一个 函数来计算它们的调和平均数,并将结果返回给m ...
- 《C++ Primer》第13章 13.5节习题答案
<C++ Primer>第13章 拷贝控制 13.5节 动态内存管理类 习题答案 练习13.39:编写你自己版本的StrVec,包括自己版本的reserve.capacity(参见9.4节 ...
- c primer plus 第五章编程练习
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 目录 文章目录 前言 ##1.编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创 ...
- C++primer plus第四章编程练习自编程序
//第四章编程练习 //eg.1显示信息 #include <iostream> using namespace std; const int SIZE = 20; int main() ...
- C++ Primer Plus 第13章笔记
第13章 类继承 13.1 一个简单的基类 class RatedPlayer : public TableTennisPlayer {... } 冒号指出RatedPlayer继承TableTenn ...
- C Primer Plus 第13章 文件输入/输出 13.11 编程练习答案
2019独角兽企业重金招聘Python工程师标准>>> 1.修改程序清单13.1中的程序,使之不采用命令行参数,而是请求用户输入文件名并读入用户的响应. (程序清单13.1示范了如何 ...
- C Primer Plus 第十一章 编程练习 1-15题
第1题 #include<stdio.h> void readChar(char* words , int len); int main(void) {int LEN;printf(&qu ...
最新文章
- 真正的博士是如何参加AAAI, ICML, ICLR等AI顶会的?
- getcwd()和dirname(__FILE__)的区别
- PowerShell图形化编程2-快速入门
- Python求找出1000以内所有的“完数”
- ZOJ - 3228 Searching the String(AC自动机求不重复子串出现次数)
- IntelliJ IDEA 12 中用 Maven + Jetty 来开发Web项目
- 如何用 Nodejs 分析一个简单页面
- MacOS iNode客户端连接SSL
- 你需要启用steam社区界面功能以进行购买_STEAM播放器成了:不用买游戏就能单独购买游戏音轨...
- cgi加载java class_深入研究Java类加载机制
- 利用Python制作证件照
- 30字的完美个人简历
- psm倾向得分匹配法举例_倾向得分匹配(PSM)操作过程与问题反思
- 【软件分析/静态程序分析学习笔记】3.数据流分析(Data Flow Analysis) (上):可达性分析(Reaching Definitions)
- 深度学习基础知识(八):神经网络基础之导数
- 《强化学习》 基本概念和交叉熵方法
- java中default关键字_Java 中的 default 关键字及代码示例
- 管理系统联系人之补充
- Linux Top 详细介绍,包含task排序
- Shell脚本中获取命令运行结果、特殊变量使用、条件判断等常用操作