c++ plus 13 第四题

#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++的朋友,大家一起共同努力,谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值