C++学习-------类继承之友元和操作符重载

 
/*********************************************************************************
这一次的代码没有什么东西,只是用来当作练习记忆的一些特性,学编程,多动手才是王道,
就算代码再简单也要写一写,不写不知道。。。。
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 ;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值