鱼C论坛

 找回密码
 立即注册
查看: 1283|回复: 3

[已解决]继承中的权限问题 这里报错了

[复制链接]
发表于 2020-12-16 21:57:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. using namespace std;

  3. class Car{
  4. public:
  5.         Car(int wh,float we):wheel(wh),weigh(we){}
  6.         void Getinfo(){
  7.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t"<<endl;
  8.         }
  9. protected:
  10.         int wheel;
  11.         float weigh;
  12. };

  13. class LCar:private Car{
  14. public:
  15.         LCar(int wh,float we,int peop):wheel(wh),weigh(we),people_allow(peop){}                                          //这里报错了 说没有weigh和wheel成员变量  不是从Car继承了吗。。。
  16.         void Getinfo(){
  17.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人"<<endl;
  18.         }
  19. private:
  20.         int people_allow;
  21.        
  22. };

  23. class Truck:private Car{
  24. public:
  25.         Truck(int wh,float we,int peop,float we_al):wheel(wh),weigh(we),people_allow(peop),weigh_allow(we_al){}                                     //这里也报错了 同样的原因
  26.         void Getinfo(){
  27.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人 载重量:"<<weigh_allow<<"t "<<endl;
  28.         }
  29. private:
  30.         int people_allow;
  31.         float weigh_allow;
  32.        
  33. };
  34. int main(){
  35.         Truck T(4,21,4,50);
  36.         T.Getinfo()
  37. }
复制代码
最佳答案
2020-12-17 08:42:12
这样写
  1. #include <iostream>
  2. using namespace std;

  3. class Car{
  4. public:
  5.         Car(int wh,float we):wheel(wh),weigh(we){}
  6.         void Getinfo(){
  7.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t"<<endl;
  8.         }
  9. protected:
  10.         int wheel;
  11.         float weigh;
  12. };

  13. class LCar:private Car{
  14. public:
  15.         LCar(int wh,float we,int peop):Car(wh,we),people_allow(peop){}                                          //这里报错了 说没有weigh和wheel成员变量  不是从Car继承了吗。。。
  16.         void Getinfo(){
  17.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人"<<endl;
  18.         }
  19. private:
  20.         int people_allow;
  21.        
  22. };

  23. class Truck:private Car{
  24. public:
  25.         Truck(int wh,float we,int peop,float we_al):Car(wh,we),people_allow(peop),weigh_allow(we_al){}                                     //这里也报错了 同样的原因
  26.         void Getinfo(){
  27.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人 载重量:"<<weigh_allow<<"t "<<endl;
  28.         }
  29. private:
  30.         int people_allow;
  31.         float weigh_allow;
  32.        
  33. };
  34. int main(){
  35.         Truck T(4,21,4,50);
  36.         T.Getinfo();
  37.         return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-17 08:42:12 | 显示全部楼层    本楼为最佳答案   
这样写
  1. #include <iostream>
  2. using namespace std;

  3. class Car{
  4. public:
  5.         Car(int wh,float we):wheel(wh),weigh(we){}
  6.         void Getinfo(){
  7.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t"<<endl;
  8.         }
  9. protected:
  10.         int wheel;
  11.         float weigh;
  12. };

  13. class LCar:private Car{
  14. public:
  15.         LCar(int wh,float we,int peop):Car(wh,we),people_allow(peop){}                                          //这里报错了 说没有weigh和wheel成员变量  不是从Car继承了吗。。。
  16.         void Getinfo(){
  17.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人"<<endl;
  18.         }
  19. private:
  20.         int people_allow;
  21.        
  22. };

  23. class Truck:private Car{
  24. public:
  25.         Truck(int wh,float we,int peop,float we_al):Car(wh,we),people_allow(peop),weigh_allow(we_al){}                                     //这里也报错了 同样的原因
  26.         void Getinfo(){
  27.                 cout<<"车轮数:"<<wheel<<" 车重:"<<weigh<<"t 载客量:"<<people_allow<<"人 载重量:"<<weigh_allow<<"t "<<endl;
  28.         }
  29. private:
  30.         int people_allow;
  31.         float weigh_allow;
  32.        
  33. };
  34. int main(){
  35.         Truck T(4,21,4,50);
  36.         T.Getinfo();
  37.         return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-17 10:33:10 | 显示全部楼层
用public继承应该没问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-17 17:43:17 | 显示全部楼层

谢谢啦 那就是说基类构造函数给基类成员变量赋值只能调用基类的构造函数是吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 02:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表