系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:C++PrimerPlus:第十三章类和继承:编程练习4
Benevolent Order Programmers用来维护瓶装葡萄酒箱,为描述它,BOP portmaster设置了一个Port类,其声明如下:
show()方法按下面的格式显示信息:
Brand:Gallo
Kind:tawnyBottles:20
operator<<()函数按下面的格式显示信息(末尾没有换行符):Gallo,tawny,20
PortMaster 完成了 Port 类的方法定义后派生了 VintagePort 类,然后被解职–因为不小心将一瓶 45度 Cockburn 泼到了正在准备烤肉调料的人身上,VintagePort 类如下所示:
您被指定负责完成 VintagePort。
- a.第一个任务是重新创建 Port 方法定义,因为前任被开除时销毁了方法定义
- b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
- c.第三个任务是解释为何没有将operator-()和 operator<<( )声明为虚的。
- d.第四个任务是提供 VintagePort 中各个方法的定义。
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
Benevolent Order Programmers用来维护瓶装葡萄酒箱,为描述它,BOP portmaster设置了一个Port类,其声明如下:
#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);//adds b to bottles
Port& operator-=(int b);//subtracts b from bottles,if available
int BootleCount()const { return bottles; }
virtual void Show()const;
friend ostream& operator<<(ostream& os, const Port& p);
};
show()方法按下面的格式显示信息:
Brand:Gallo
Kind:tawnyBottles:20
operator<<()函数按下面的格式显示信息(末尾没有换行符):Gallo,tawny,20
PortMaster 完成了 Port 类的方法定义后派生了 VintagePort 类,然后被解职–因为不小心将一瓶 45度 Cockburn 泼到了正在准备烤肉调料的人身上,VintagePort 类如下所示:
//style necessarily = "vintage"
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);
};
您被指定负责完成 VintagePort。
- a.第一个任务是重新创建 Port 方法定义,因为前任被开除时销毁了方法定义
- b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
- c.第三个任务是解释为何没有将operator-()和 operator<<( )声明为虚的。
- d.第四个任务是提供 VintagePort 中各个方法的定义。
提示:以下是本篇文章正文内容,下面案例可供参考
一、编程练习
C++PrimerPlus:第十三章类和继承:编程练习4
二、使用步骤
1.a.第一个任务是重新创建 Port 方法定义,因为前任被开除时销毁了方法定义
#ifndef PE13_4_H
#define PE13_4_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);//adds b to bottles
Port& operator-=(int b);//subtracts b from bottles,if available
int BootleCount()const { return bottles; }
virtual void Show()const;
friend ostream& operator<<(ostream& os, const Port& p);
};
#endif
2.b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
由于 Port 存在深拷贝的需要,拷贝构造函数 Port(const Port & p)、析构函数 ~Port()、赋值运算符重载 Port & operator=(const Port & p) 等三个成员函数不能使用编译器自动生成的潜拷贝版本,所以必须重写定义;其他成员函数编译器不会自动生成,
按功能需要,必须得自行定义;
3.c.第三个任务是解释为何没有将operator-()和 operator<<( )声明为虚的。
运算符没有进行虚函数定义,这里根据传参的不同,进行自动动态编译。在show的方法中,他没有参数传递,所以不知道需要执行那个类中的show,这里基类和派生类都给他传有参数, 故不需要进行虚函数定义
4.d.第四个任务是提供 VintagePort 中各个方法的定义。
//style necessarily = "vintage"
class VintagePort :public Port
{
private:
char* nickname;//昵称
int year; //年份
public:
//VintagePort();
VintagePort(const char* br = "", int b = 0, const char* nn = "", int y = 0);
VintagePort(const VintagePort& vp);
~VintagePort() { delete[]nickname; }
VintagePort& operator=(const VintagePort& vp);
virtual void Show()const;
friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
5 实验现象
总结
`提示:对实验现象进行总结
1,基类show执行的时候,成功返回基类对象的参数
2,派生类进行show的时候,成功执行基类及派生类的输出
3,派生类赋值进行成功输出
4,基类给基类指针进行赋值,成功输出基类
5,当派生类给基类指针进行赋值操作,输出执行派生类show的方法