【C++学习】习题记录 第十三章 类继承

第十三章 类继承

第一题:以已有类,派生出一个新类,并用指定程序进行测试。

//fiel1 onehead.h
#ifndef ONEHEAD_H_
#define ONEHEAD_H_
#include<string>
#include<iostream>
using std::cout;
using std::endl;
class Cd
{
//private:
protected:
	char performers[50];
	char label[20];
	int selections;
	double playtime;
public:
	Cd(){};
	Cd(char *s1, char *s2, int n, double x);
	Cd(const Cd &d);
	virtual ~Cd(){};
	virtual void Report()const;
	//virtual Cd & operator=(const Cd & d);
};
#endif
//file2 oneheadplus.h
#ifndef ONEHEADPLUS_H_
#define ONEHEADPLUS_H_
#include"onehead.h"
class Classic:public Cd
{
public:
	Classic(){};
	Classic(char *s3, char *s1, char *s2, int n, double x);
	~Classic(){};
	void Report()const;
	virtual Classic & operator=(const Classic & d);
private:
	char abstract[60];
};
#endif
//file3 onehead.cpp
#include"onehead.h"
Cd::Cd(char *s1, char *s2, int n, double x)
{
	strncpy_s(performers, s1, 49);
	strncpy_s(label, s2,19 );
	selections = n;
	playtime = x;
}

Cd::Cd(const Cd &d)
{
	strcpy_s(performers, d.performers);
	strcpy_s(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
}
void Cd::Report()const
{
	cout << "----CD Methord----" << endl;
	cout << "Performers: " << performers << endl;
	cout << "Label     : " << label << endl;
	cout << "Slections : " << selections << endl;
	cout << "Play time : " << playtime << endl;
}
//file4 oneheadplus.cpp
#include"oneheadplus.h"
Classic::Classic(char *s3, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x)
{
	strncpy_s(abstract, s3, 59);
}
void Classic::Report()const
{
	cout << "----Classic Methord----" << endl;
	cout << "Abstract  : " <<abstract << endl;
	cout << "Performers: " << performers << endl;
	cout << "Label     : " << label << endl;
	cout << "Slections : " << selections << endl;
	cout << "Play time : " << playtime << endl;
}
Classic & Classic::operator=(const Classic & d)
{
	if (this == & d)
		return *this;
	strcpy_s(abstract, d.abstract);
	strcpy_s(performers, d.performers);
	strcpy_s(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
	return *this;
}
//file5 one.cpp
#include"oneheadplus.h"

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\n";
	c1.Report();
	c2.Report();

	cout << "\n\nUsing type cd* pointer to object : \n\n";
	pcd->Report();
	pcd = &c2;
	pcd->Report();

	cout << "\n\nCalling a function with a Cd reference argument: \n\n";
	Bravo(c1);
	Bravo(c2);

	cout << "\n\nTesting assigment: \n\n";
	Classic copy;
	copy = c2;
	copy.Report();
	system("pause");
	return 0;
}
void Bravo(const Cd&disk)
{
	disk.Report();
}

结果显示13.1

第二题:重写上一题,利用动态内存分配。
与第一题代码基本相同只有少部分修改。

//file1 onehead.h
#ifndef ONEHEAD_H_
#define ONEHEAD_H_
#include<string>
#include<iostream>
using std::cout;
using std::endl;
class Cd
{
//private:
protected:
	//char performers[50];
	//char label[20];
	char *performers;
	char *label;
	int selections;
	double playtime;
public:
	Cd(){};
	Cd(char *s1, char *s2, int n, double x);
	Cd(const Cd &d);
	virtual ~Cd(){};
	virtual void Report()const;

};
#endif
//file2 oneheadplus.cpp
#ifndef ONEHEADPLUS_H_
#define ONEHEADPLUS_H_
#include"onehead.h"
class Classic:public Cd
{
public:
	Classic(){};
	Classic(char *s3, char *s1, char *s2, int n, double x);
	~Classic(){};
	void Report()const;
	virtual Classic & operator=(const Classic & d);
private:
	//char abstract[60];
	char *abstract ;
};
#endif
//file3 onehead.cpp
#include"onehead.h"
Cd::Cd(char *s1, char *s2, int n, double x)
{

	performers=s1;
	label=s2;
	//strncpy_s(performers, s1, 49);
	//strncpy_s(label, s2,19 );
	selections = n;
	playtime = x;
}

Cd::Cd(const Cd &d)
{
	performers = d.performers;
	label = d.label;
	//strcpy_s(performers, d.performers);
	//strcpy_s(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
}
void Cd::Report()const
{
	cout << "----CD Methord----" << endl;
	cout << "Performers: " << performers << endl;
	cout << "Label     : " << label << endl;
	cout << "Slections : " << selections << endl;
	cout << "Play time : " << playtime << endl;
}
//file4 oneheadplus.cpp
#include"oneheadplus.h"
Classic::Classic(char *s3, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x)
{
	abstract=s3;
	//strncpy_s(abstract, s3, 59);
}
void Classic::Report()const
{
	cout << "----Classic Methord----" << endl;
	cout << "Abstract  : " <<abstract << endl;
	cout << "Performers: " << performers << endl;
	cout << "Label     : " << label << endl;
	cout << "Slections : " << selections << endl;
	cout << "Play time : " << playtime << endl;
}
Classic & Classic::operator=(const Classic & d)
{
	if (this == & d)
		return *this;
	abstract = d.abstract;
	performers = d.performers;
	label = d.label;
	//strcpy_s(abstract, d.abstract);
	//strcpy_s(performers, d.performers);
	//strcpy_s(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
	return *this;
}
//file5 one.cpp
#include"oneheadplus.h"

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\n";
	c1.Report();
	c2.Report();

	cout << "\n\nUsing type cd* pointer to object : \n\n";
	pcd->Report();
	pcd = &c2;
	pcd->Report();

	cout << "\n\nCalling a function with a Cd reference argument: \n\n";
	Bravo(c1);
	Bravo(c2);

	cout << "\n\nTesting assigment: \n\n";
	Classic copy;
	copy = c2;
	copy.Report();
	system("pause");
	return 0;
}
void Bravo(const Cd&disk)
{
	disk.Report();
}

结果显示13.2

第三题:修改DMA的示例程序,使其中的三个类都由ABC类派生而来,并在定义中添加vitural View()方法处理数据显示

//file1 threehead.h
#ifndef THREEHEAD_H_
#define THREEHEAD_H_
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class ABC
{
private:
	char *label;
	int rating;
public:
	ABC(){};
	ABC(char *l, int r)
	{
		label = l;
		rating = r;
	};
	ABC(const ABC & D)
	{
		label = D.label;
		rating = D.rating;
	};
	virtual ~ABC(){ };
	virtual void View()const = 0;

protected:
	char *la()const { return label; };
	int ra()const  { return rating; };
};


class DMA:public ABC
{
public:
	DMA(){};
	DMA(const DMA & D) :ABC(D){};
	DMA(char *l, int r) :ABC(l,r){};
	virtual ~DMA(){};
	virtual void View()const;
};

class LackDMA :public ABC
{
private:
	char color[40];
public:
	LackDMA(){};
	LackDMA(char *s, char *l, int r) :ABC(l, r)
	{
		strncpy_s(color, s, 39);
		color[39] = '\0';
	}
	virtual ~LackDMA(){};
	virtual void View()const;
};
class HasDMA :public ABC
{
private :
	char * style;
public:
public:
	HasDMA(){};
	HasDMA(char *s, char *l, int r) : ABC(l, r)
	{
		style=s;
	}
	virtual ~HasDMA(){};
	virtual void View()const;
};
#endif
//file2 threehead.cpp
#include"threehead.h"

void DMA::View()const
{
		cout << "---DMA---"<<endl;
		cout << "Lable  : " << la() << endl;
		cout << "Rating : " << ra() << endl;
}
void LackDMA::View()const
{
		cout << "---LackDMA---" << endl;
		cout << "Lable  : " << la() << endl;
		cout << "Rating : " << ra() << endl;
		cout << "Clolr  : " << color << endl;
}
void HasDMA::View()const
{
	cout << "---HasDMA---" << endl;
	cout << "Lable  : " << la() << endl;
	cout << "Rating : " << ra() << endl;
	cout << "Style  : " << style << endl;
}
//file3 three.cpp
#include"threehead.h"
#include<string>
using namespace std;
void readf(int);
 char s1[40];
 char s2[40];
 int  i1;
int main()
{	
	int kind;
	DMA a;
	LackDMA b;
	HasDMA c;

	cout << "Choose class 1 ,2 , 3 to save(q to quit): ";
	cin >> kind;
	while (cin)
	{
		if (kind == 1)
		{
			cout << "MODE 1\n";
			readf(kind);
			a = DMA(s2, i1);
		}
		else if (kind == 2)
		{
			cout << "MODE 2\nEnter color: ";
			readf(kind);
			b = LackDMA(s1, s2, i1); 
		}
		else if (kind==3)
		{
			cout << "MODE 3\nEnter style: ";
			readf(kind);
			c = HasDMA(s1, s2, i1); 
		}	
		else
		{
			break;
		}
		cout << "Choose class 1 ,2 , 3 to save(q to quit): ";
		cin >> kind;
	}
	a.View();
	b.View();
	c.View();
	cout << "Done!"<<endl;
	return 0;
}

void readf(int i)
{
	cin.get();
	if (i == 1)
	{
		cout << "Enter lable: ";
		cin.getline(s2, 30);
		cout << "Enter rating: ";
		cin >> i1;
	}
	else
	{

		cin.getline(s1, 30);
		cout << "Enter lable: ";
		cin.getline(s2, 30);
		cout << "Enter rating: ";
		cin >> i1;
	}
}

第四题:修改实例程序,补全类,补全定义

//file1 fourhead.h
#ifndef FOURHEAD_H_
#define FOURHEAD_H_
#include<iostream>
using namespace std;
class Port
{
public:
	Port(char * br="none",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 BottoleCount()const{ return bottles; };
	virtual void Show()const;
	friend ostream &operator<<(ostream &os,const Port &p);
private:
	char *brand;
	char style[20];
	int bottles;
};

class VintagePort:public Port
{
private:
	char *nickname;
	int year;
public:
	VintagePort(){};
	VintagePort(char * br, int b, 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
//file2 fourhead.cpp
#include"fourhead.h"

// Port method
Port::Port(char * br, char *st, int b)
{
	brand = br;
	int len = strlen(st);
	int i = 0;
	for (; i < len&&i < 19; i++)
		style[i] = st[i];
	style[i] = '\0';
	bottles = b;
}
Port::Port(const Port&p)
{
	brand = p.brand;
	strcpy_s(style, p.style);
	bottles = p.bottles;
}
Port & Port::operator =(const Port &p)
{
	brand = p.brand;
	strcpy_s(style, p.style);
	bottles = p.bottles;
	return *this;
}
Port &Port:: operator +=(int b)
{
	bottles += b;
	return *this;
}
Port &Port:: operator -=(int b)
{
	if (bottles >= b)
		bottles -= b;
	else
		cout << "Don,t have so many bottles!";
	return *this;
}
void Port::Show()const 
{ 
	cout << "Brand: " << brand << endl;
	cout << "Kind: " << style << endl;
	cout << "Brand: " << bottles << endl;
}
ostream &operator<<(ostream &os, const Port &p)
{
	os << p.brand << ", " << p.style << ", " << p.bottles;
	return os;
}
// VintagePort method
VintagePort::VintagePort(char * br, int b, char *nn, int y) :Port(br, "vintage", b) 
{
	nickname = nn;
	year = y;
}
VintagePort::VintagePort(const VintagePort &vp) : Port(vp)
{
	nickname = vp.nickname;
	year = vp.year;
}
VintagePort &VintagePort::operator=(const VintagePort &vp)
{
	Port::operator=(vp);
	nickname = vp.nickname;
	year = vp.year;
	return *this;
}
void VintagePort::Show()const
{
	cout << "---VintgaePort---\n";
	Port::Show();
	cout << "Nickname: " << nickname<<endl;
	cout << "Year: " << year<<endl;
}
ostream & operator<<(ostream & os, const VintagePort &vp)
{
	os << Port(vp);
	cout << ", " << vp.nickname << ", " << vp.year;
	return (os);
}
//file3 four.cpp
#include"fourhead.h"
#include<iostream>

int main()
{
	Port port1("por", "tawny", 20);
	cout << port1<<endl;
	port1.Show();
	VintagePort vp("vpor", 24, "nice", 16);
	cout << vp <<  endl;
	vp.Show();
	VintagePort vp3;
	vp3 = vp;
	cout << vp3 << endl;
	port1 +=1;
	cout << "port1 += 1: "<<port1.BottoleCount() << endl;
	port1 -= 15;
	cout << "port1 -= 15: " << port1.BottoleCount() << endl;
	return 0;
}

第十二章还是要补全的,接下来补第十二章。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值