?? vehicle.h
字號:
//定義并實現類CVehicle,CShip和CAirPlane
#include <iostream.h > //有些編譯系統(tǒng)可能是包含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 + -