c++primer plus 第13章 编程题第2题
生活随笔
收集整理的這篇文章主要介紹了
c++primer plus 第13章 编程题第2题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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完以后后續的語句不會執行
posted on 2018-07-05 09:33 syne 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/syne-cllf/p/9266589.html
總結
以上是生活随笔為你收集整理的c++primer plus 第13章 编程题第2题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用阿里云ACM简化你的Spring C
- 下一篇: 基于傅里叶变换的音频重采样算法 (附完整