c++ plus 13 第四题
来源:互联网 发布:redis 大量数据排序 编辑:程序博客网 时间:2024/06/07 15:27
#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,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
*********************************************************************************
#include "port.h"
#include <iostream>
using namespace std;
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);
bottles=p.bottles;
style[19]='/0';
}
Port & Port::operator = (const Port & p )
{
if(&p==this)
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)
{
bottles+=b;
return *this;
};
Port & Port::operator -= (int b)
{
if(b>bottles)
cout<<"subtract error!!!"<<endl;
bottles-=b;
return *this;
};
void Port::Show()const
{
cout<<"Brand: "<<brand<<endl;
cout<<"Kind: "<<style<<endl;
cout<<"Bottle: "<<bottles<<endl;
}
ostream & operator << (ostream & os,const Port & p)
{
os<<p.brand<<" "<<p.style<<" "<<p.bottles<<endl;
return os;
};
VintagePort::VintagePort():Port("none","Vintage",0)
{
nickname=new char [1];
nickname[0]='/0';
year=0;
};
VintagePort::VintagePort(const char *br, int b, const char *nn, int y):Port(br,"Vintage",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(&vp==this)
return *this;
delete [] nickname;
Port::operator =(vp);
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<<(Port &)vp;
os<<vp.nickname<<" "<<vp.year<<endl;
return os;
}
******************************************************************************
#include "port.h"
#include <iostream>
using namespace std;
int main()
{
VintagePort a("TianHua",17,"ling",1986);
cout<<a;
a-=2;
cout<<a;
system("pause");
return 0;
}
如有错误,希望路过的朋友多多指点啊,并且很乐意结交正在学习C++的朋友,大家一起共同努力,谢谢!
- 《C Primer Plus》第四章编程题
- c++ plus 13 第四题
- C++primer plus第四章
- C Primer plus 第四章
- C Primer Plus 第四章
- C Primer Plus 第四章 编程练习 1-7题
- C++Primer Plus(第六版) 第八章 第四题
- C++Primer Plus(第六版) 第十三章 第四题
- C++primer plus阅读笔记第四章
- C primer plus 第四章总结
- C++primer Plus 第四章练习题
- 记录——《C Primer Plus (第五版)》第七章编程练习第四题
- 记录——《C Primer Plus (第五版)》第八章编程练习第四题
- 记录——《C Primer Plus (第五版)》第九章编程练习第四题
- 记录——《C Primer Plus (第五版)》第十章编程练习第四题
- 记录——《C Primer Plus (第五版)》第十一章编程练习第四题
- C++primer plus(第四版)第五章习题参考
- 《c primer plus》编程练习回顾-第四章
- 向SQL Server数据库添加图片
- 一个asp.net的编译错误“无法识别的转义序列”
- woodstock table排序问题 (vwp)
- 动态修改内存中的站点地图节点
- c++ plus 13
- c++ plus 13 第四题
- 用实体关系图进行数据库建模
- .net 应用路径获取
- 神秘的最后一班!F-117 再会!
- 2007年的新闻笑话20强
- [整合]利用CSS + Javascript 缩小背景图片
- 编译所有子目录的makefile
- 使用飞信VMDotNet使C#程序脱离.NET FRAMEWORK也能运行的一些心得(转)
- 怎样防止新增后刷新重复插入记录