日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++primer plus 第13章 编程题第2题

發布時間:2025/5/22 c/c++ 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。