/*********************************************************************************
这一次的代码没有什么东西,只是用来当作练习记忆的一些特性,学编程,多动手才是王道,
就算代码再简单也要写一写,不写不知道。。。。
PS:这一次的代码没有提供测试代码,只提供了类的声明、接口和方法实现
**********************************************************************************/
/**********************************************************************************
Port.h文件
包括一些主要的类声明、以及方法。提供类的接口
**********************************************************************************/
#include<iostream>
#ifndef DMA_H_
#define DMA_H_
// class Port
using std::ostream ; //因为vc++6.0有一个小小的bug,所以这里不能用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) ;
virtual void Show() const ;
friend ostream & operator<<(ostream &os ,const VintagePort &vp) ;
};
#endif
/*************************************************************************
Port.cpp文件
提供类方法的实现
**************************************************************************/
#include<iostream>
#include"Port.h"
#include<cstring>
using std::ostream ;
using std::endl ;
using std::cout ;
//Port method
void Port::Show() const
{
cout << "Brand : " << brand << endl ;
cout << "Kind : " << style << endl ;
cout << "Bottles : " << bottles << endl ;
}
Port & Port::operator -=(int b)
{
bottles -= b ;
return *this ;
}
Port & Port::operator +=(int b)
{
bottles += b ;
return *this ;
}
Port & Port::operator =(const Port &p)
{
int n = 0 ;
if(this == &p)
return *this ;
delete [] brand ;
n = strlen(p.brand) ;
brand = new char[n+1] ;
strcpy(brand,p.brand) ;
strcpy(style,p.style) ;
bottles = p.bottles ;
return *this ;
}
Port::Port(const char *br ,const char *st,int b)
{
int n = 0 ;
n = strlen(br) ;
brand = new char[n+1] ;
strncpy(brand,br,n+1) ;
brand[n] = '\0' ;
strcpy(style,st) ;
bottles = b ;
}
Port::Port(const Port &p)
{
int n = 0 ;
n = strlen(p.brand) ;
brand = new char[n+1] ;
strcpy(brand,p.brand) ;
strcpy(style,p.style) ;
bottles = p.bottles ;
}
//Port friend method
ostream & operator<<(std::ostream & os , const Port &p) //友元在定义上不可以写关键字friend
{
os << p.brand << ", " << p.style << ", " << p.bottles << endl ;
return os ;
}
//VintagePort method
ostream & operator<<(ostream &os, const VintagePort &vp)
{
cout << (const Port &)vp ; //强制转型可以调用父类的友元函数
cout << vp.nickname << ", " << vp.year << endl ;
return os ;
}
void VintagePort::Show() const
{
Port::Show() ;
cout << "Nickname : " << nickname << endl ;
cout << "Year : " << year << endl ;
}
VintagePort & VintagePort::operator =(const VintagePort &vp)
{
int n = 0 ;
if(this == &vp)
return *this ;
Port::operator =(vp) ;
n = strlen(vp.nickname) ;
nickname = new char[n+1] ;
strcpy(nickname,vp.nickname) ;
year = vp.year ;
return *this ;
}
VintagePort::VintagePort(const VintagePort &vp):Port(vp) //成员初始化列表,很好用。。
{
int n = 0 ;
n = strlen(vp.nickname) ;
nickname = new char[n+1] ;
strcpy(nickname,vp.nickname) ;
year = vp.year ;
}
VintagePort::VintagePort():Port()
{
char *nickname = "null" ;
int year = 0 ;
}
VintagePort::VintagePort(const char *br,int b ,const char *nn ,int y):Port(br,"vintage",b)
{
int n = 0 ;
n = strlen(nn) ;
nickname = new char[n+1] ;
strcpy(nickname,nn) ;
year = y ;
}