C++PrimerPlus学习——第十三章编程练习

13-1
注意char*前面加const,不然就会报错
Classis.h

#ifndef CLASSIC_H_
#define CLASS_H_
#include <string>
class Cd {
private:
	char performers[50];
	char label[20];
	int selections;
	double playtime;
public:
	Cd(const char* s1, const char* s2, int n, double x);
	Cd(const Cd&);
	Cd();
	virtual ~Cd();
	virtual void Report() const;
	virtual Cd& operator=(const Cd& d);
};
class Classic:public Cd
{
private:
	char major[50];
public:
	Classic(const char* s1, const char* s2, const char* s3, int n, double x);
	Classic();
	~Classic();
	void Report();
	Classic& operator=(const Classic& d);
};
#endif // !CLASSIC_H_

Classic.cpp

#include <iostream>
#include "Classic.h"

Cd::Cd(const char* s1, const char* s2, int n, double x)
{
	strcpy_s(performers, strlen(s1) + 1, s1);
	strcpy_s(label, strlen(s2) + 1, s2);
	selections = n;
	playtime = x;
}
Cd::Cd(const Cd& d)
{
	strcpy_s(performers, strlen(d.performers) + 1, d.performers);
	strcpy_s(label, strlen(d.label) + 1, d.label);
	selections = d.selections;
	playtime = d.playtime;
}
Cd::Cd()
{
	performers[0] = '\0';
	label[0] = '\0';
	selections = 0;
	playtime = 0.0;
}
Cd::~Cd()
{

}
void Cd::Report() const
{
	std::cout << "Preformers: " << performers << '\n';
	std::cout << "Label: " << label << '\n';
	std::cout << "selectiongs: " << selections << '\n';
	std::cout << "playtime: " << playtime << '\n';
}
Cd& Cd::operator=(const Cd& d)
{
	if (this == &d)
		return *this;
	strcpy_s(performers, strlen(d.performers) + 1, d.performers);
	strcpy_s(label, strlen(d.label) + 1, d.label);
	selections = d.selections;
	playtime = d.playtime;
	return *this;
}
Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n)
{
	strcpy_s(major, strlen(s1)+1, s1);
}
Classic::Classic():Cd()
{
	major[0] = '\0';
}
Classic::~Classic()
{

}
void Classic::Report()
{
	Cd::Report();
	std::cout << "Major: " << major << '\n';
}
Classic& Classic::operator=(const Classic& d)
{
	if (this == &d)
		return *this;
	Cd::operator=(d);
	strcpy_s(major, strlen(d.major) + 1, d.major);
	return *this;
}

main.cpp

#include <iostream>
#include "Classic.h"
using std::cout;
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.Report();
	c2.Report();

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

	cout << "Testing assignment: ";
	Classic copy;
	copy = c2;
	copy.Report();

	return 0;
}

void Bravo(const Cd& disk)
{
	disk.Report();
}

13-2
主函数仍然不变
Classic.h

#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <string>
class Cd {
private:
	char* performers;
	char* label;
	int selections;
	double playtime;
public:
	Cd(const char* s1, const char* s2, int n, double x);
	Cd(const Cd&);
	Cd();
	virtual ~Cd();
	virtual void Report() const;
	virtual Cd& operator=(const Cd& d);
};
class Classic:public Cd
{
private:
	char* major;
public:
	Classic(const char* s1, const char* s2, const char* s3, int n, double x);
	Classic();
	~Classic();
	void Report();
	Classic& operator=(const Classic& d);
};
#endif // !CLASSIC_H_

Classic.cpp

#include <iostream>
#include "Classic.h"

Cd::Cd(const char* s1, const char* s2, int n, double x)
{
	performers = new char[strlen(s1) + 1];
	strcpy_s(performers, strlen(s1) + 1, s1);
	label = new char[strlen(s2) + 1];
	strcpy_s(label, strlen(s2) + 1, s2);
	selections = n;
	playtime = x;
}
Cd::Cd(const Cd& d)
{
	performers = new char[strlen(d.performers) + 1];
	strcpy_s(performers, strlen(d.performers) + 1, d.performers);
	label = new char[strlen(d.label) + 1];
	strcpy_s(label, strlen(d.label) + 1, d.label);
	selections = d.selections;
	playtime = d.playtime;
}
Cd::Cd()
{
	performers = new char[1];
	performers[0] = '\0';
	label = new char[1];
	label[0] = '\0';
	selections = 0;
	playtime = 0.0;
}
Cd::~Cd()
{
	delete[] performers;
	delete[] label;
}
void Cd::Report() const
{
	std::cout << "Preformers: " << performers << '\n';
	std::cout << "Label: " << label << '\n';
	std::cout << "selectiongs: " << selections << '\n';
	std::cout << "playtime: " << playtime << '\n';
}
Cd& Cd::operator=(const Cd& d)
{
	if (this == &d)
		return *this;
	delete[] performers;
	delete[] label;
	performers = new char[strlen(d.performers) + 1];
	strcpy_s(performers, strlen(d.performers) + 1, d.performers);
	label = new char[strlen(d.label) + 1];
	strcpy_s(label, strlen(d.label) + 1, d.label);
	selections = d.selections;
	playtime = d.playtime;
	return *this;
}
Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n)
{
	major = new char[strlen(s1) + 1];
	strcpy_s(major, strlen(s1)+1, s1);
}
Classic::Classic():Cd()
{
	major = new char[1];
	major[0] = '\0';
}
Classic::~Classic()
{
	delete[] major;
}
void Classic::Report()
{
	Cd::Report();
	std::cout << "Major: " << major << '\n';
}
Classic& Classic::operator=(const Classic& d)
{
	if (this == &d)
		return *this;
	Cd::operator=(d);
	delete[] major;
	major = new char[strlen(d.major) + 1];
	strcpy_s(major, strlen(d.major) + 1, d.major);
	return *this;
}

13-3
虽然不是很理解,但是这个套路是明白了
dma.h

#ifndef DMA_H_
#define DMA_H_
#include <iostream>

class ABC
{
	char* fullname;
	int level;
public:
	ABC(const char* f = "null", int I = 0);
	ABC(const ABC& ab);
	virtual ~ABC();
	ABC& operator=(const ABC& ab);
	virtual void show();
};

class baseDMA: public ABC
{
private:
	char* label;
	int rating;
public:
	baseDMA(const char* l = "null", int r = 0, const char* f = "null", int lv = 0);
	baseDMA(const baseDMA& rs);
	~baseDMA();
	baseDMA& operator=(const baseDMA& rs);
	virtual void show();
};

class lacksDMA: public ABC
{
private:
	enum{ COL_LEN = 40 };
	char color[COL_LEN];
public:
	lacksDMA(const char* c = "blank", const char* l = "null", int r = 0);
	lacksDMA(const char* c, const baseDMA& rs);
	virtual void show();
};

class hasDMA: public ABC
{
private:
	char* style;
public:
	hasDMA(const char* s = "none", const char* l = "null", int r = 0);
	hasDMA(const char* s, const baseDMA& hs);
	hasDMA(const hasDMA& hs);
	~hasDMA();
	hasDMA& operator=(const hasDMA& rs);
	virtual void show();
};
#endif // !DMA_H_

dma.cpp

#include<cstring>
#include<string>
#include "dma.h"

ABC::ABC(const char* a, int b)
{
	fullname = new char[strlen(a) + 1];
	strcpy_s(fullname, strlen(a) + 1, a);
	level = b;
}
ABC::ABC(const ABC& ab)
{
	fullname = new char[strlen(ab.fullname) + 1];
	strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);
	level = ab.level;
}
ABC::~ABC()
{
	delete[] fullname;
}
ABC& ABC::operator=(const ABC& ab)
{
	if (this == &ab)
		return *this;
	delete[] fullname;
	fullname = new char[strlen(ab.fullname) + 1];
	strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);
	level = ab.level;
	return *this;
}
void ABC::show()
{
	std::cout << "fullname: " << fullname << std::endl;
	std::cout << "level: " << level << std::endl;
}

baseDMA::baseDMA(const char* l, int r, const char* f, int lv):ABC(f, lv)
{
	label = new char[strlen(l) + 1];
	strcpy_s(label, strlen(l) + 1, l);
	rating = r;
}
baseDMA::baseDMA(const baseDMA& rs):ABC(rs)
{
	label = new char[strlen(rs.label) + 1];
	strcpy_s(label, strlen(rs.label) + 1, rs.label);
	rating = rs.rating;
}
baseDMA::~baseDMA()
{
	delete[] label;
}
baseDMA& baseDMA::operator=(const baseDMA& rs)
{
	if (this == &rs)
		return *this;
	ABC::operator=(rs);
	delete[] label;
	label = new char[strlen(rs.label) + 1];
	strcpy_s(label, strlen(rs.label) + 1, rs.label);
	rating = rs.rating;
	return *this;
}
void baseDMA::show()
{
	ABC::show();
	std::cout << "label: " << label << std::endl;
	std::cout << "rating: " << rating << std::endl;
}
lacksDMA::lacksDMA(const char* c, const char* l, int r):ABC(l, r)
{
	strcpy_s(color, strlen(c) + 1, c);
}
lacksDMA::lacksDMA(const char* c, const baseDMA& rs):ABC(rs)
{
	strcpy_s(color, strlen(c) + 1, c);
}
void lacksDMA::show()
{
	ABC::show();
	std::cout << "color: " << color << std::endl;
}
hasDMA::hasDMA(const char* s, const char* l, int r):ABC(l, r)
{
	style = new char[strlen(s) + 1];
	strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char* s, const baseDMA& hs):ABC(hs)
{
	style = new char[strlen(s) + 1];
	strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA& hs):ABC(hs)
{
	style = new char[strlen(hs.style) + 1];
	strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{
	delete[] style;
}
hasDMA& hasDMA::operator=(const hasDMA& hs)
{
	if (this == &hs)
		return *this;
	ABC::operator=(hs);
	delete[] style;
	style = new char[strlen(hs.style) + 1];
	strcpy_s(style, strlen(hs.style) + 1, hs.style);
	return *this;
}
void hasDMA::show()
{
	ABC::show();
	std::cout << "style: " << style << std::endl;
}

main.cpp

#include <iostream>
#include "dma.h"
using std::cout;

int main()
{
	baseDMA a1("asdfadsf", 9, "dsfadfa", 1);
	lacksDMA a2("dfafda", "daf d", 2);
	hasDMA a3("sdfadfas", "dfqwedsa", 3);
	cout << "baseDMA:\n";
	a1.show();
	cout << "lacksDMA:\n";
	a2.show();
	cout << "hasDMA:\n";
	a3.show();
	baseDMA a4 = a1;
	lacksDMA a5 = a2;
	hasDMA a6 = a3;
	return 0;
}

13-4
Port.h

#ifndef PORT_H_
#define PORT_H_
#include <iostream>
using namespace std;

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);
};

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

Port.cpp

#include "Port.h"
#include <cstring>
Port::Port(const char* br, const char* st, int b)
{
	brand = new char[strlen(br) + 1];
	strcpy_s(brand, strlen(br) + 1, br);
	strcpy_s(style, 20, st);
	bottles = b;
}

Port::Port(const Port& p)
{
	brand = new char[strlen(p.brand) + 1];
	strcpy_s(brand, strlen(p.brand) + 1, p.brand);
	strcpy_s(style, 20, p.style);
	bottles = p.bottles;
}

Port& Port::operator=(const Port& p)
{
	if (this == &p)
		return *this;
	delete[] brand;
	brand = new char[strlen(p.brand) + 1];
	strcpy_s(brand, strlen(p.brand) + 1, p.brand);
	strcpy_s(style, p.style);
	bottles = p.bottles;
	return *this;
	return *this;// TODO: 在此处插入 return 语句
}

Port& Port::operator+=(int b)
{
	bottles += b;
	return *this;// TODO: 在此处插入 return 语句
}

Port& Port::operator-=(int b)
{
	bottles -= b;
	return *this;	// TODO: 在此处插入 return 语句
}

void Port::Show() const
{
	std::cout << "Brand: " << brand << std::endl;
	std::cout << "King: " << style << std::endl;
	std::cout << "Bottles: " << bottles << std::endl;
}

ostream& operator<<(ostream& os, const Port& p)
{
	std::cout << p.brand << ", " << p.style << ", " << p.bottles << std::endl;
	return os;// TODO: 在此处插入 return 语句
}

ostream& operator<<(ostream& os, const VintagePort& vp)
{
	os << (const Port&)vp;
	std::cout << vp.nickname << ", " << vp.year << std::endl;
	return os;// TODO: 在此处插入 return 语句
}

VintagePort::VintagePort()
{
	nickname = new char[1];
	nickname[0] = '\0';
	year = 0;
}

VintagePort::VintagePort(const char* br, const char* st, int b, const char* nn, int y): Port(br, st, b)
{
	nickname = new char[strlen(nn) + 1];
	strcpy_s(nickname, strlen(nn) + 1, nn);
	year = y;
}

VintagePort::VintagePort(const VintagePort& vp)
{
	nickname = new char[strlen(vp.nickname) + 1];
	strcpy_s(nickname, strlen(vp.nickname), vp.nickname);
	year = vp.year;
}

void VintagePort::Show() const
{
	Port::Show();
	std::cout << "Nickname: " << nickname << std::endl;
	std::cout << "Year: " << year << std::endl;
}

main.cpp

#include <iostream>
#include "Port.h"
using std::cout;

int main()
{
	Port wine1("Gallo", "tawny", 20);
	VintagePort wine2("Romance Conti", "vintage", 10, "The Noble", 1876);
	VintagePort wine3("Merlot", "ruby", 30, "Old Velvet", 1888);
	cout << "Show Port object:\n";
	wine1.Show();
	cout << wine1 << endl;
	cout << "Show VintagePort object:\n";
	wine2.Show();
	cout << wine2 << endl;
	cout << "Show VintagePort object:\n";
	wine3.Show();
	cout << wine3 << endl;
	Port(wine4) = wine1;
	cout << "Show VintagePort object:\n";
	wine4.Show();
	cout << wine4<< endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值