#include<iostream>
using namespace std;
#include<string>
class Car{
protected:
string model;
double price;
public:
Car(string m,double p)
{
model=m;
price=p;
}
void show()
{
cout<<"型号:"<<model<<" "<<"价格:"<<price<<endl;
}
};
class Jiaoche:virtual public Car{
//座位数
protected:
int seat;
public:
Jiaoche(string m,double p,int s):Car(m,p)
{
seat=s;
}
virtual void show1()
{
show();
cout<<"座位数:"<<seat;
}
};
class Kache:virtual public Car{
//载货重量
protected:
double load;
public:
Kache(string m,double p,double l):Car(m,p)
{
load=l;
}
void show2()
{
show();
cout<<"载重量:"<<load<<endl;
}
};
class Pika: public Jiaoche,public Kache{
//座位,重量
public:
Pika(string m,double p,int s,double l): Car(m,p),Jiaoche(m,p,s),Kache(m,p,l)
{
}
void show3()
{
cout<<"型号:"<<model<<"价格:"<<price<<"座位数:"<<seat<<"载重量:"<<load<<endl;
}
};
int main()
{
Car c1("奥迪",20);
Jiaoche c2("长安",30,7);
Kache c3("五菱",40,700);
Pika c4("吉普",50,4,700);
c1.show();
c2.show1();
c3.show2();
c4.show3();
Car &p1=c1;
Car &p2=c2;
Car &p3=c3;
p1.show();
p2.show();
p3.show();
return 0;
}
实验九 派生类与继承(二)假设汽车包含如下属性:型号、价格。三种车的继承问题
最新推荐文章于 2024-05-22 00:22:29 发布