设计一个基类Vehicle,它具有一个需要传递参数的构造函数...1
发布网友
发布时间:2024-03-09 14:59
我来回答
共1个回答
热心网友
时间:2024-06-22 03:04
#include<iostream>
using namespace std;
class Vehicle{
protected:
int wheel;
double weight;
public:
Vehicle(int wl,double wgt):wheel(wl),weight(wgt){
//cout<<"Vehicle()"<<endl;
}
};
class Car:virtual public Vehicle{
protected:
int passengers;
public:
Car(int wl,double wgt,int ps):passengers(ps),Vehicle(wl,wgt){
//cout<<"Car()"<<endl;
}
};
class Truck:virtual public Vehicle{
protected:
double payload;
public:
Truck(int wl,double wgt,double pl):payload(pl),Vehicle(wl,wgt){
//cout<<"Truck()"<<endl;
}
};
class Notchback:public Car,public Truck{
int flag;
public:
Notchback(int wl,double wgt,int ps,double pl,int f):flag(f),Vehicle(wl,wgt),Car(0,0,ps),Truck(0,0,pl){
if(flag==0) cout<<"Notchback constructs from Car"<<endl;
else cout<<"Notchback constructs from Truck"<<endl;
}
};
int main(){
cout<<"nb1:"<<endl;
Notchback nb1(2,3,6,2,0);
cout<<"nb2:"<<endl;
Notchback nb2(2,3,6,2,1);
}
注:
nb1为flag为0,从Car类继承
nb2为flag为非0数,从Truck类继承