C++ Primer Plus(第6版)---编程作业完成(第十三章)

本文详细展示了C++编程中关于Cd、Classic、BaseDMA、LacksDMA和HasDMA类的实例,包括基类的构造、复制构造、析构及派生类的特性和操作。同时介绍了Port和VintagePort类的继承与操作,涉及对象复制、重载赋值和虚函数应用。
摘要由CSDN通过智能技术生成

charpter13_homework.cpp

#include<iostream>
#include<string>
#include"classic.h"
#include"dma_3.h"
#include"port.h"

using namespace std;
void p13_1(void);
void p13_3(void);
void p13_4(void);

int main()
{
	//p13_1();
	//p13_3();
	p13_4();

	return 0;
}

//-----作业一/二----------
void Bravo(const Cd& disk)
{
	disk.Report();
}
void p13_1(void)
{
	Cd c1("AAA","BBB",14,13.5);
	Classic c2=Classic("CCC","DDD","EEE",2,57.17);
	Cd* pcd=&c1;
	c1.Report();
	c2.Report();
	cout<<endl;
	pcd->Report();
	pcd=&c2;
	pcd->Report();
	cout<<endl;
	Bravo(c1);
	Bravo(c2);
	cout<<endl;
	Classic copy;
	copy=c2;
	copy.Report();
}

//-----------作业三-----------
void p13_3(void)
{
	const int Max=3;
	dmaABC* p_dma[Max];

	string temp_label;
	int temp_rating;
	string temp_color;
	string temp_style;

	for(int i=0;i<Max;i++)
	{
		cout<<"Enter the label:";
		getline(cin,temp_label);//头文件为<string>
		cout<<"Enter the rating: ";
		cin>>temp_rating;
		cout<<"Enter the num(1,2,3): ";
		char temp;
		while(cin>>temp && (temp!='1'&& temp!='2' && temp!='3'))
			cout<<"Enter the num(1,2,3): ";
		while(cin.get()!='\n')
			continue;
		if(temp=='1')//string转为C字符串
			p_dma[i]=new baseDMA(temp_label.c_str(),temp_rating);
		else if(temp=='2')
		{
			cout<<"Enter the color: ";
			getline(cin,temp_color);
			p_dma[i]=new lacksDMA(temp_color.c_str(),temp_label.c_str(),temp_rating);
		}
		else
		{
			cout<<"Enter the style: ";
			getline(cin,temp_style);
			p_dma[i]=new hasDMA(temp_style.c_str(),temp_label.c_str(),temp_rating);
		}
		cout<<endl;
	}
	for(int i=0;i<Max;i++)
	{
		p_dma[i]->View();
		cout<<endl;
	}
	for(int i=0;i<Max;i++)
		delete [] p_dma[i];
	cout<<"Done!"<<endl;
	return;
}

//---------作业四-------------
void p13_4(void)
{
	Port p1("AAA","BBB",20);
	p1.Show();
	Port p2;
	p2=p1;
	cout<<p2<<endl;
	p2-=5;
	cout<<p2<<endl;
	VintagePort v1;
	v1.Show();
	v1+=20;
	cout<<v1<<endl;
	VintagePort v2(v1);
	v2.Show();
}

classic.h

//-----------作业一-------------------

//#ifndef CLASSIC_H_
//#define CLASSIC_H_
//
基类
//class Cd
//{
//private:
//	char performers[50];
//	char laber[20];
//	int selections;
//	double playtime;
//public:
//	Cd(char* s1="None",char* s2="Nul",int n=0,double x=0.0);
//	virtual void Report()const;
//};
//
派生类
//class Classic:public Cd
//{
//private:
//	char importance[50];
//public:
//	Classic(char* s="No",char* s1="None",char* s2="Nul",int n=0,double x=0.0);
//	Classic(char* s,const Cd& d);
//	virtual void Report()const;
//};
//
//#endif


//-------------作业二------------------------

#ifndef CLASSIC_H_
#define CLASSIC_H_

//基类
class Cd
{
private:
	char* performers;//复制构造,析构,重载赋值
	char* laber;
	int selections;
	double playtime;
public:
	Cd(char* s1="None",char* s2="Nul",int n=0,double x=0.0);
	Cd(const Cd & c);//复制构造
	Cd& operator=(const Cd & c);
	virtual void Report()const;
	virtual ~Cd();//析构
};

//派生类
class Classic:public Cd
{
private:
	char* importance;
public:
	Classic(char* s="No",char* s1="None",char* s2="Nul",int n=0,double x=0.0);
	Classic(char* s,const Cd& d);
	Classic(const Classic & c);
	Classic& operator=(const Classic& c);
	virtual void Report()const;
	~Classic();
};

#endif

classic.cpp

#include<iostream>
#include<cstring>
#include"classic.h"
using std::cout;
using std::endl;
using std::strcpy;
using std::strlen;

//--------------作业一-----------------
//
基类
//Cd::Cd(char* s1,char* s2,int n,double x)
//{
//	strcpy(performers,s1);
//	strcpy(laber,s2);
//	selections=n;
//	playtime=x;
//}
//void Cd::Report()const
//{
//	cout<<"performers: "<<performers<<endl;
//	cout<<"laber: "<<laber<<endl;
//	cout<<"selections: "<<selections<<endl;
//	cout<<"playtime: "<<playtime<<endl;
//}
//
派生类
//Classic::Classic(char* s,char* s1,char* s2,int n,double x):Cd(s1,s2,n,x)
//{
//	strcpy(importance,s);
//}
//Classic::Classic(char* s,const Cd& d):Cd(d)
//{
//	strcpy(importance,s);
//}
//void Classic::Report()const
//{
//	Cd::Report();
//	cout<<"importance: "<<importance<<endl;
//}



//-----------作业二--------------
//基类
Cd::Cd(char* s1,char* s2,int n,double x)
{
	performers=new char[strlen(s1)+1];
	strcpy(performers,s1);
	laber=new char[strlen(s2)+1];
	strcpy(laber,s2);
	selections=n;
	playtime=x;
}
Cd::Cd(const Cd & c)//复制构造
{
	performers=new char[strlen(c.performers)+1];
	strcpy(performers,c.performers);
	laber=new char[strlen(c.laber)+1];
	strcpy(laber,c.laber);
	selections=c.selections;
	playtime=c.playtime;
}
Cd& Cd::operator=(const Cd & c)
{
	performers=new char[strlen(c.performers)+1];
	strcpy(performers,c.performers);
	laber=new char[strlen(c.laber)+1];
	strcpy(laber,c.laber);
	selections=c.selections;
	playtime=c.playtime;
	return *this;
}
void Cd::Report()const
{
	cout<<"performers: "<<performers<<endl;
	cout<<"laber: "<<laber<<endl;
	cout<<"selections: "<<selections<<endl;
	cout<<"playtime: "<<playtime<<endl;
}
Cd::~Cd()//析构
{
	delete [] performers;
	delete [] laber;
}
//派生类
Classic::Classic(char* s,char* s1,char* s2,int n,double x):Cd(s1,s2,n,x)
{
	importance=new char[strlen(s)+1];
	strcpy(importance,s);
}
Classic::Classic(char* s,const Cd& d):Cd(d)
{
	importance=new char[strlen(s)+1];
	strcpy(importance,s);
}
Classic::Classic(const Classic & c):Cd(c)
{
	importance=new char[strlen(c.importance)+1];
	strcpy(importance,c.importance);
}
Classic& Classic::operator=(const Classic& c)
{
	//成员函数,使用作用域运算符来调用
	Cd::operator=(c);
	importance=new char[strlen(c.importance)+1];
	strcpy(importance,c.importance);
	return *this;
}
void Classic::Report()const
{
	Cd::Report();
	cout<<"importance: "<<importance<<endl;
}
Classic::~Classic()
{
	delete [] importance;
}

dma_3.h

#ifndef DMA_3_H_
#define DMA_3_H_
#include<iostream>
using std::ostream;

//抽象基类
class dmaABC
{
private:
	char* label;
	int rating;
protected:
	char* Label(){return label;};
	int Rating(){return rating;};
public:
	dmaABC(const char* lab="null",int rat=0);
	dmaABC(const dmaABC & d);//复制构造
	dmaABC& operator=(const dmaABC& d);//重载赋值
	virtual void View()const=0;//至少有一个纯虚函数
	virtual ~dmaABC();//虚析构函数
};


//派生类baseDMA
class baseDMA:public dmaABC
{
public:
	baseDMA(const char* lab="null",int rat=0);
	void View()const;
};

//派生类lacksDMA,没有new操作
class lacksDMA:public dmaABC
{
private:
	enum{COL_LEN=40};
	char color[COL_LEN];
public:
	lacksDMA(const char* c="blank",const char* lab="null",int rat=0);
	lacksDMA(const char* c,const dmaABC& d);
	void View()const;
};

//派生类hasDMA,有new操作
class hasDMA:public dmaABC
{
private:
	char* style;
public:
	hasDMA(const char* s="None",const char* lab="null",int rat=0);
	hasDMA(const char*s,const dmaABC& d);
	hasDMA(const hasDMA& hasD);//复制构造
	hasDMA& operator=(const hasDMA& hasD);//重载赋值
	void View()const;
	~hasDMA();//析构
};

#endif

dma_3.cpp

#include<iostream>
#include<cstring>
#include "dma_3.h"
using std::cout;
using std::endl;
using std::strcpy;
using std::strlen;

//抽象基类dmaABC
dmaABC::dmaABC(const char* lab,int rat)
{
	label=new char[strlen(lab)+1];
	strcpy(label,lab);
	rating=rat;
}
dmaABC::dmaABC(const dmaABC & d)//复制构造
{
	label=new char[strlen(d.label)+1];
	strcpy(label,d.label);
	rating=d.rating;
}
dmaABC& dmaABC::operator=(const dmaABC& d)//重载赋值
{
	//检查自己
	if(this==&d)
		return *this;
	//删除自己
	delete [] label;
	//开始复制
	label=new char[strlen(d.label)+1];
	strcpy(label,d.label);
	rating=d.rating;
	return *this;
}
void dmaABC::View()const//至少有一个纯虚函数
{
	cout<<"label: "<<label<<endl;
	cout<<"rating: "<<rating<<endl;
}
dmaABC::~dmaABC()//虚析构函数
{
	delete [] label;
}

//派生类 baseDMA
baseDMA::baseDMA(const char* lab,int rat):dmaABC(lab,rat)
{
}
void baseDMA::View()const
{
	dmaABC::View();
}

//派生类 lacksDMA
lacksDMA::lacksDMA(const char* c,const char* lab,int rat):dmaABC(lab,rat)
{
	strcpy(color,c);
}
lacksDMA::lacksDMA(const char* c,const dmaABC& d):dmaABC(d)
{
	strcpy(color,c);
}
void lacksDMA::View()const
{
	dmaABC::View();
	cout<<"color: "<<color<<endl;
}

//派生类 hasDMA
hasDMA::hasDMA(const char* s,const char* lab,int rat):dmaABC(lab,rat)
{
	style=new char[strlen(s)+1];
	strcpy(style,s);
}
hasDMA::hasDMA(const char*s,const dmaABC& d):dmaABC(d)
{
	style=new char[strlen(s)+1];
	strcpy(style,s);
}
hasDMA::hasDMA(const hasDMA& hasD):dmaABC(hasD)//复制构造
{
	style=new char[strlen(hasD.style)+1];
	strcpy(style,hasD.style);
}
hasDMA& hasDMA::operator=(const hasDMA& hasD)//重载赋值
{
	//检查自己
	if(this==&hasD)
		return *this;
	//调用基类重载赋值
	dmaABC::operator=(hasD);
	//删除自己
	delete [] style;
	style=new char[strlen(hasD.style)+1];
	strcpy(style,hasD.style);
	return *this;
}
void hasDMA::View()const
{
	dmaABC::View();
	cout<<"style: "<<style<<endl;
}
hasDMA::~hasDMA()//析构
{
	delete [] style;
}

port.h

#ifndef PORT_H_
#define PORT_H_
#include<iostream>
using std::ostream;
//基类 Port
class Port
{
private:
	char* brand;
	char style[20];
	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);
	Port& operator-=(int b);
	int BottleCount()const{return bottles;};
	virtual void Show()const;
	friend ostream& operator<<(ostream& os,const Port& p);
};

//派生类 VintagePort
class VintagePort:public Port
{
private:
	char* nickname;
	int year;
public:
	VintagePort();
	VintagePort(const char* br,int b,const char* nn,int y);
	VintagePort(const VintagePort& vp);
	~VintagePort(){delete [] nickname;};
	VintagePort& operator=(const VintagePort& vp);
	void Show()const;
	friend ostream& operator<<(ostream & os,const VintagePort& vp); 
};

#endif

port.cpp

#include<iostream>
#include"port.h"
using std::cout;
using std::endl;
using std::ostream;
using std::strlen;
using std::strcpy;

//基类 Port
Port::Port(const char* br,const char* st,int b)
{
	brand=new char[strlen(br)+1];
	strcpy(brand,br);
	strncpy(style,st,19);
	style[19]='\0';
	bottles=b;
}
Port::Port(const Port& p)//复制构造
{
	brand=new char[strlen(p.brand)+1];
	strcpy(brand,p.brand);
	strncpy(style,p.style,19);
	style[19]='\0';
	bottles=p.bottles;
}
	
Port& Port::operator=(const Port& p)//重载赋值
{
	//检查自己
	if(this==&p)
		return *this;
	//删除自己
	delete [] brand;
	//复制
	brand=new char[strlen(p.brand)+1];
	strcpy(brand,p.brand);
	strncpy(style,p.style,19);
	style[19]='\0';
	bottles=p.bottles;
	return *this;
}
Port& Port::operator+=(int b)
{
	if(b>0)
		bottles+=b;
	else
		cout<<"Error!\n";
	return *this;
}
Port& Port::operator-=(int b)
{
	if(bottles>b && b>0)
		bottles-=b;
	else
		cout<<"Error!\n";
	return *this;
}
	
void Port::Show()const
{
	cout<<"Brand: "<<brand<<endl;
	cout<<"Kind: "<<style<<endl;
	cout<<"Bottles: "<<bottles<<endl;
}
ostream& operator<<(ostream& os,const Port& p)
{
	os<<p.brand<<", "<<p.style<<", "<<p.bottles;
	return os;
}

//派生类 VintagePort
VintagePort::VintagePort():Port()
{
	nickname=new char[2];
	strcpy(nickname,"n");
	year=0;
}
VintagePort::VintagePort(const char* br,int b,const char* nn,int y):Port(br,"None",b)
{
	nickname=new char[strlen(nn)+1];
	strcpy(nickname,nn);
	year=y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp)
{
	nickname=new char[strlen(vp.nickname)+1];
	strcpy(nickname,vp.nickname);
	year=vp.year;
}
	
VintagePort& VintagePort::operator=(const VintagePort& vp)
{
	//检查自己
	if(this==&vp)
		return *this;
	//调用基类重载赋值’
	Port::operator=(vp);
	//删除自己
	delete [] nickname;
	//复制
	nickname=new char[strlen(vp.nickname)+1];
	strcpy(nickname,vp.nickname);
	year=vp.year;
	return *this;
}
void VintagePort::Show()const
{
	Port::Show();
	cout<<"nickname: "<<nickname<<endl;
	cout<<"year: "<<year<<endl;
}
ostream& operator<<(ostream & os,const VintagePort& vp)
{
	os<<(const Port& )vp;
	os<<", "<<vp.nickname<<", "<<vp.year;
	return os;
}

如有错误,欢迎指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值