c++ PRimer Plus(習(xí)題13.1)/*13.1題的實現(xiàn)文件,書上的測試文件*///哪個方法不需要也根據(jù)實現(xiàn)而定,始終跟隨客戶的腳步//這題細節(jié)方面被坑了,mainworks字符數(shù)組設(shè)置太小了,導(dǎo)致了一系列不可思議的問題#include<iostream>#include"classic.h"using namespace std;void Bravo(const 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.Roport();	c2.Roport();	cout << "Using type cd *pointer to objects:/n";	pcd->Roport();							pcd = &c2;	pcd->Roport();	cout << "Calling a funtion with a Cd reference argument:/n";	Bravo(c1);							//這也是測試虛方法,由調(diào)用對象決定	Bravo(c2);	cout << "Testing assigment: /n";	Classic copy;						//測試派生類的賦值運算符	copy = c2;	copy.Roport();	return 0;}void Bravo(const Cd&disk){	disk.Roport();}#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式//習(xí)題12.1的頭文件#ifndef CLASSIC_H#define CLASSIC_H#include<iostream>class Cd{private:	char performers[50];	char label[20];	int selections;	double playtime;public:	Cd(char *s1, char*s2, int n, double x);	Cd() {};	Cd(const Cd&d);  	~Cd() {};							//由于不是動態(tài)分配內(nèi)存,需不需要這個都行	virtual void Roport()const;	Cd& Operator=(const Cd &d);}; class Classic:public Cd			//公有繼承{private:	char mainworks[50];			//存儲主要作品public:	Classic(char *m, char *s1, char*s2, int n, double x);	Classic() {};	Classic(const Classic&c);	~Classic() {};	void Roport()const;	Classic&operator=(const Classic &c);};#endif // !CLASSIC_H#include"classic.h"//base calss mothodCd::Cd(char *s1, char*s2, int n, double x):selections(n),playtime(x){	std::strcpy(performers, s1);		//使用初始化列表好像無法初始化performers和label	std::strcpy(label, s2);}//copy construction functionCd::Cd(const Cd&d){	std::strcpy(performers, d.performers);	std::strcpy(label,d.label);	selections = d.selections;	playtime = d.playtime;}void Cd::Roport()const{	using std::cout;	using std::endl;	cout << "Performers: " << performers << endl		<< "Lable: " << label << endl		<< "Selections: " << selections << endl		<< "Playtime: " << playtime << endl;}Cd& Cd::operator=(const Cd &d){	if (this == &d)		return *this;	std::strcpy(performers, d.performers);	std::strcpy(label, d.label);	selections = d.selections;	playtime = d.playtime;	return *this;}//Classic mothodsClassic::Classic(char *m, char *s1, char*s2, int n, double x):Cd(s1,s2,n,x){										//成員初始化;列表初始化基類數(shù)據(jù)	std::strcpy(mainworks, m);}void Classic::Roport()const{	Cd::Roport();				//調(diào)用基類的report方法	std::cout << "Mainworks: " << mainworks << std::endl;}Classic&Classic::operator=(const Classic&c){	if (this == &c)		return *this;	Cd::operator=(c);	std::strcpy(mainworks, c.mainworks);	return *this;}Classic::Classic(const Classic&c){	std::strcpy(mainworks, c.mainworks);}c++ Primer Plus(習(xí)題13.2)#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式//習(xí)題12.1的頭文件#ifndef NEWCLASSIC_H#define NEWCLASSIC_H#include<iostream>class Cd{private:	char *performers;	char *label;	int selections;	double playtime;public:	Cd(char *s1, char*s2, int n , double x);	//Cd(const Cd&d);	Cd() {};	virtual ~Cd() { delete[]performers; delete[] label; };							//由于不是動態(tài)分配內(nèi)存,需不需要這個都行	virtual void Roport()const;	Cd& operator=(const Cd &d);};class Classic :public Cd			//公有繼承{private:	char *mainworks;			//存儲主要作品public:	Classic(char *m,char *s1, char*s2, int n, double x);	Classic() {};	virtual ~Classic() { delete[]mainworks; };	void Roport()const;	Classic&operator=(const Classic &c);};#endif // !CLASSIC_H#include"newclassic.h"//base calss mothodCd::Cd(char *s1, char*s2, int n, double x) :selections(n), playtime(x){	performers = new char[std::strlen(s1)+1];	label = new char[std::strlen(s2)+1];	std::strcpy(performers, s1);		//使用初始化列表好像無法初始化performers和label	std::strcpy(label, s2);}void Cd::Roport()const{	using std::cout;	using std::endl;	cout << "Performers: " << performers << endl		<< "Lable: " << label << endl		<< "Selections: " << selections << endl		<< "Playtime: " << playtime << endl;}Cd& Cd::operator=(const Cd &d){	if (this == &d)		return *this;	//這里不加delete[]perfomers是因為構(gòu)造函數(shù)沒用new[]初始化	performers = new char[std::strlen(d.performers) + 1];	label = new char[std::strlen(d.label) + 1];	std::strcpy(performers, d.performers);	std::strcpy(label, d.label);	selections = d.selections;	playtime = d.playtime;	return *this;}//Classic mothodsClassic::Classic(char *m, char *s1, char*s2, int n, double x) :Cd(s1, s2, n, x){										//成員初始化;列表初始化基類數(shù)據(jù)	mainworks = new char[std::strlen(m)+1];	std::strcpy(mainworks, m);}void Classic::Roport()const{	Cd::Roport();				//調(diào)用基類的report方法	std::cout << "Mainworks: " << mainworks << std::endl;}Classic&Classic::operator=(const Classic&c){	if (this == &c)		return *this;	Cd::operator=(c);    //這里也不用delete[]mainworks;	mainworks = new char[std::strlen(c.mainworks) + 1];	std::strcpy(mainworks, c.mainworks);	return *this;}/*13.2題的實現(xiàn)文件,書上的測試文件*///和上一題一模一樣的實現(xiàn)文件,就是把字符數(shù)組改成動態(tài)內(nèi)存分配//老套路,可刪除的函數(shù)有復(fù)制構(gòu)造函數(shù),因為沒用到,這么簡單的題玩了一下午#include<iostream>#include"newclassic.h"using namespace std;void Bravo(const 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.Roport();	c2.Roport();	cout << "Using type cd *pointer to objects:/n";	pcd->Roport();	pcd = &c2;	pcd->Roport();	cout << "Calling a funtion with a Cd reference argument:/n";	Bravo(c1);							//這也是測試虛方法,由調(diào)用對象決定	Bravo(c2);	cout << "Testing assigment: /n";	Classic copy;						//測試派生類的賦值運算符和默認構(gòu)造函數(shù)	copy = c2;	copy.Roport();	return 0;}void Bravo(const Cd&disk){	disk.Roport();}c++ Primer Plus(習(xí)題13.3)
//變更書上三種DMA的關(guān)系,使他們從一個抽象基類中派生出來//這是根據(jù)書上程序清單13.10模仿出來的測試文件//頭文件省略了很多方法,可以說是一個專門解決特定客戶需求的類#include<iostream>#include"dma.h"const int CLENTS = 3;int main(){	using namespace std;	Abc *p[CLENTS];	char temp[30];	int temrating;	char choose;	for (int i = 0; i < CLENTS; i++)	{		cout << "Enter the label: ";		cin.getline(temp, 30);		cout << "Enter the rating: ";		cin >> temrating;		cout << "Enter 1 for baseDMA class or "			"2 for lacksDMA class or 3 for hasDMA class: ";		while (cin >> choose && (choose != '1'&&choose!='2'&&choose!='3'))			cout << "Make you choice rightly!";					if (choose == '1')			p[i] = new baseDMA(temp, temrating);		else if (choose == '2')		{			char temc[10];			cin.get();			//混合輸入數(shù)字和字符串切記處理換行符			cout << "Enter the color: ";			cin.get(temc, 10);			p[i] = new lacksDMA(temc, temp, temrating);		}		else		{			char temstyle[10];			cout << "Enter the style: ";			cin.get();			cin.get(temstyle, 10);			p[i] = new hasDMA(temstyle, temp, temrating);		}		while (cin.get() != '/n')			continue;	}	cout << endl;	for (int i = 0; i < CLENTS; i++)	{		p[i]->Report();		cout << endl;	}	for (int i = 0; i < CLENTS; i++)	{		delete p[i];			//free emory!	}	cout << "*************Test complete!**************/n";	return 0;}#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式//使用抽象基類abc派生出三個類#ifndef DMA_H#define DMA_H#include<iostream>class Abc{private:	char *label;	int rating;protected:	char* Label()const { return label; }		//保護方法,報告抽象基類中的成員	int Rating()const { return rating; }public:	Abc(const char *l = "NULL", int r = 0);	virtual void Report()const = 0;			//純虛函數(shù)的標(biāo)志,基類可以不提供定義	virtual ~Abc() { delete[]label; }};class baseDMA:public Abc{public:	baseDMA(const char *l = "NULL", int r = 0) :Abc(l, r) {};	virtual ~baseDMA() {};	virtual void Report()const;};class lacksDMA:public Abc{private:	enum{COL_LEN=40};	char color[COL_LEN];public:	lacksDMA(const char *la = "NULL", const char*l = "NULL", int r = 0);	virtual ~lacksDMA() {};	virtual void Report()const;};class hasDMA :public Abc{private:	char *style;public:	hasDMA(const char*s = "NULL", const char*l = "NULL", int r = 0);	virtual ~hasDMA() { delete[]style; };	virtual void Report()const;};#endif // !DMA_H#include"dma.h"using std::cout;using std::endl;//Abc mothodAbc::Abc(const char *l, int r){	label = new char[std::strlen(l) + 1];	std::strcpy(label, l);	rating = r;}//baseDMA METHODvoid baseDMA::Report()const{	cout << "Label: " << Label() << endl		//調(diào)用抽象基類的保護方法		<< "Rating: " << Rating() << endl;}//lacksDMA methodlacksDMA::lacksDMA(const char *la, const char*l, int r) :Abc(l, r) {	std::strcpy(color, la);}void lacksDMA::Report()const{	cout << "Label: " << Label() << endl		<< "Rating: " << Rating() << endl		<< "Color: " << color << endl;}//hasDMA methodhasDMA::hasDMA(const char*s, const char*l, int r) :	Abc(l, r){	style = new char[std::strlen(s) + 1];	std::strcpy(style, s);}void hasDMA::Report()const{	cout << "Label: " << Label() << endl		<< "Rating: " << Rating() << endl		<< "Style: " << style << endl;}c++ Primer Plus(習(xí)題13.4)//一題填空題,得自己編寫文件測試全部方法//b:有的方法需要重新定義,比如說析構(gòu)函數(shù)還有構(gòu)造函數(shù),等等,都是必須重新定義的//c:重載的賦值運算符和輸出符基類的引用完全夠用,不用聲明成虛的方法#include<iostream>#include"port.h"int main(){	using std::cout;	using std::endl;	Port a("Hong", "tawny", 100);	VintagePort b("La", 20, "The Noble", 2017);	cout << "Resource A: " << a << "/nResource B: " << b << endl;	cout << "Test copy construction c=a: /n";	Port c=a;	c.Show();	cout << "Test << operator:/n";	cout << c;	cout << "/nTest a+=15 and b-=15:";	a += 15;	b -= 15;	cout << " A: " << a << " B: " << b << endl;	cout << "Test virtual method:/n ";	Port *p = &a;	p->Show();	p = &b;	p->Show();	cout << "c=b ";	c = b;	c.Show();	cout << "/n*************Test complete!**************/n";	return 0;}#pragma once#pragma execution_character_set("utf-8")//本文件為utf-8編碼格式#ifndef PORT_H#define PORT_H#include<iostream>using namespace std;class Port{private:	char *brand;	char style[20];			//ie.,tawny,ruby,vintage	int bottles;public:	Port(const char*br = "none", const char *st = "none", int b = 0);	Port(const Port&p);	virtual ~Port() { delete[] brand; }	Port&operator=(const Port&p);	Port&operator+=(int b);				// add b to bottles	Port&operator-=(int b);	virtual void Show()const;	friend ostream& operator<<(ostream&os, const Port&p);protected:	int BottleCount()const { return bottles; }	};class VintagePort :public Port{private:	char *nickname;	int year;public:	VintagePort();	VintagePort(const char*nn, int y, const char*br, int b);	VintagePort(const VintagePort &vp);	~VintagePort() { delete[]nickname; };	VintagePort&operator=(const VintagePort&vp);	virtual void Show()const;	friend ostream&operator<<(ostream&os, const VintagePort&pt);};#endif // !PORT_H#include"port.h"using std::cout;using std::endl;//Port methodsPort::Port(const char*br, const char *st, int b){	brand = new char[std::strlen(br) + 1];	std::strcpy(brand, br);	std::strcpy(style, st);	bottles = b;}Port::Port(const Port&p){	brand = new char[std::strlen(p.brand) + 1];	std::strcpy(brand, p.brand);	std::strcpy(style, p.style);	bottles = p.bottles;}Port&Port::operator=(const Port&p){	if (this == &p)		return *this;	delete[]brand;	brand = new char[std::strlen(p.brand) + 1];	std::strcpy(brand, p.brand);	std::strcpy(style, p.style);	bottles = p.bottles;	return *this;}Port&Port::operator+=(int b){	bottles += b;	return *this;}Port&Port::operator-=(int b){	bottles -= b;	return *this;}//out put methodsvoid Port::Show()const{	cout << "Brand: " << brand << endl		<< "Kind: " << style << endl		<< "Bottles: " << bottles << endl;}ostream& operator<<(ostream&os, const Port&p){	os << p.brand << " , " << p.style << " , " << p.bottles;	return os;}//VintagePort methods VintagePort::VintagePort(){	nickname = new char[1];	nickname = nullptr;	year = 0;}VintagePort::VintagePort(const char*br,int b, const char*nn, int y):Port(br,"vintage",b){	nickname = new char[std::strlen(nn) + 1];	std::strcpy(nickname, nn);	year = y;}VintagePort::VintagePort(const VintagePort &vp):Port(vp){	delete[]nickname;	nickname = new char[std::strlen(vp.nickname) + 1];	std::strcpy(nickname, vp.nickname);	year = vp.year;}VintagePort&VintagePort::operator=(const VintagePort&vp){	if (this == &vp)		return *this;	delete[]nickname;	nickname = new char[std::strlen(vp.nickname) + 1];	std::strcpy(nickname, vp.nickname);	year = vp.year;	return *this;}void VintagePort::Show()const{	Port::Show();					//調(diào)用基類的方法,用來顯示基類部分	cout << "Nick name: " << nickname << endl		<< "Year: " << year << endl;}ostream&operator<<(ostream&os, const VintagePort&pt){	os << (const Port&)pt;	os << " , " << pt.nickname << " , ";	return os;}
新聞熱點
疑難解答
圖片精選