📄 vehicle.h
字号:
//定义并实现类CVehicle,CShip和CAirPlane
#include <iostream.h > //有些编译系统可能是包含iostream,并指明名字空间std;
#include <string.h>
class CVehicle
{
protected:
int m_madeYear; //出厂年限
int m_capacity; //载客量
public:
void move() { cout << "交通工具移动......" <<endl;}
};
///////////////////////////////////////////////////////////
class CShip:public CVehicle
{
protected:
char * m_pShipMode; //轮船的型号
public:
CShip()
{
m_pShipMode = NULL;
}
void sail() { cout << "轮船在海上航行......\n" <<endl;}
void setFeature(int madeYear,int capacity,char *mode)
{
m_madeYear = madeYear;
m_capacity = capacity;
m_pShipMode = new char[strlen(mode)+1];
strcpy(m_pShipMode,mode);
}
~CShip()
{
if(m_pShipMode)
delete[] m_pShipMode;
}
friend ostream &operator<<(ostream &out,const CShip &s);
};
ostream &operator<< (ostream &out,const CShip &s)
{
out << "轮船的型号:" << s.m_pShipMode
<< ",生产年限:" << s.m_madeYear
<< ",载客量:" << s.m_capacity
<< endl;
return out;
}
///////////////////////////////////////////////////////////
class CAirPlane : public CVehicle
{
protected:
char * m_pPlanMode; //飞机的型号
public:
CAirPlane()
{
m_pPlanMode = NULL;
}
void fly() { cout << "飞机在天空翱翔......\n" <<endl;}
void setFeature(int madeYear,int capacity,char *mode)
{
m_madeYear = madeYear;
m_capacity = capacity;
m_pPlanMode = new char[strlen(mode)+1];
strcpy(m_pPlanMode,mode);
}
~CAirPlane()
{
if(m_pPlanMode)
delete[] m_pPlanMode;
}
friend ostream& operator<< (ostream& out,const CAirPlane& p);
};
ostream &operator<< (ostream &out,const CAirPlane& p)
{
out << "飞机的型号:" << p.m_pPlanMode
<< ",生产年限:" << p.m_madeYear
<< ",载客量:" << p.m_capacity
<< endl;
return out;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -