继承与派生(二)

设计一个飞机类plane,由它派生出歼击机类fighter和轰炸机类bomber,歼击机类fighter和轰炸机类bomber又共同派生出歼轰机类fighter_bomber(多用途战斗机)。描述飞机类及其派生类的类族。

*****************************************************************************************

部分程序参考如下(包含plane类的实现),将其补充完整。

提示:plane类中包含两个属性:airframe(机身)和passenger(成员数),fighter 类在plane类上增加属性missile(导弹数)bomber类在plane类上属性bomb(载弹量),fighter_bomber类则继承所有属性。plane类中的display()函数用来输出信息,每个派生类中都会重写display()函数输出自身信息。

#include<iostream>
using namespace std;
class plane//飞机
{
public:
plane(int frame,int num);
int display();
protected:
int airframe;//机身
int passenger;//乘员数
};
plane::plane(int frame,int num)
{
airframe=frame;
passenger=num;
}
int plane::display()
{
cout<<"机身:"<<airframe<<"  "<<"乘员数:"<<passenger<<"  "<<endl;
}
class fighter:public plane//歼击机
{
public:
fighter(int frame,int num,int daodan):plane(frame,num){
missile=daodan;
}
int display(){
cout<<"机身:"<<airframe<<"  "<<"乘员数:"<<passenger<<"  "<<"导弹数:"<<missile<<endl;
}
protected:
int missile;
};
class bomber:public plane//轰炸机
{
public:
bomber(int frame,int num,int zaidan):plane(frame,num){
bomb=zaidan;
}
int display(){
cout<<"机身:"<<airframe<<"  "<<"乘员数:"<<passenger<<"  "<<"载弹量:"<<bomb<<endl;
}
protected:
int bomb;
};
class fighter_bomber:public fighter,public bomber//歼轰机:多用途战斗机
{
public:
fighter_bomber(int frame,int num,int daodan,int zaidan):fighter(frame,num,daodan),bomber(frame,num,zaidan){
}
int display(){
cout<<"机身:"<<fighter::airframe<<"  "<<"乘员数:"<<fighter::passenger<<"  "<<"导弹数:"<<missile<<"  "<<"载弹量:"<<bomb<<endl;
}
};
int main(){
fighter my_fighter(6,1,8);
bomber my_bomber(9,12,6000);
fighter_bomber my_fighter_bomber(7,2,6,2500);
my_fighter.display();
my_bomber.display();
my_fighter_bomber.display();
return 0;
}

 

你可能感兴趣的:(C++,c,继承与派生)