继承中的权限问题 这里报错了
#include <iostream>using namespace std;
class Car{
public:
Car(int wh,float we):wheel(wh),weigh(we){}
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t"<<endl;
}
protected:
int wheel;
float weigh;
};
class LCar:private Car{
public:
LCar(int wh,float we,int peop):wheel(wh),weigh(we),people_allow(peop){} //这里报错了 说没有weigh和wheel成员变量不是从Car继承了吗。。。
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人"<<endl;
}
private:
int people_allow;
};
class Truck:private Car{
public:
Truck(int wh,float we,int peop,float we_al):wheel(wh),weigh(we),people_allow(peop),weigh_allow(we_al){} //这里也报错了 同样的原因
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人 载重量:"<<weigh_allow<<"t "<<endl;
}
private:
int people_allow;
float weigh_allow;
};
int main(){
Truck T(4,21,4,50);
T.Getinfo()
} 这样写
#include <iostream>
using namespace std;
class Car{
public:
Car(int wh,float we):wheel(wh),weigh(we){}
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t"<<endl;
}
protected:
int wheel;
float weigh;
};
class LCar:private Car{
public:
LCar(int wh,float we,int peop):Car(wh,we),people_allow(peop){} //这里报错了 说没有weigh和wheel成员变量不是从Car继承了吗。。。
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人"<<endl;
}
private:
int people_allow;
};
class Truck:private Car{
public:
Truck(int wh,float we,int peop,float we_al):Car(wh,we),people_allow(peop),weigh_allow(we_al){} //这里也报错了 同样的原因
void Getinfo(){
cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人 载重量:"<<weigh_allow<<"t "<<endl;
}
private:
int people_allow;
float weigh_allow;
};
int main(){
Truck T(4,21,4,50);
T.Getinfo();
return 0;
} 用public继承应该没问题 xieglt 发表于 2020-12-17 08:42
这样写
谢谢啦 那就是说基类构造函数给基类成员变量赋值只能调用基类的构造函数是吗
页:
[1]