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