如何在C++实现继承并解决错误"parent class is not accessible base of child class"?



我是C++的新手。我喜欢探索C++中的继承思想。每当我试图编译以下代码时,我都会收到错误:

for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
D:C Practice FilesVehicle.cpp: In function `int main()':
D:C Practice FilesVehicle.cpp:26: error: `void Vehicle::setStationary_state(bool)' is inaccessible
D:C Practice FilesVehicle.cpp:141: error: within this context
D:C Practice FilesVehicle.cpp:141: error: `Vehicle' is not an accessible base of `Ship'
Execution terminated

这是我的代码:

 #include <iostream.h>
 #include <conio.h>
 using std::string;
 class Vehicle{
          private:           
               bool stationary_state;
               double max_speed;
               double min_speed;
               double weight;
               double volume;
               int expected_life;
               string fuel_type;
               string model;
               string year_of_manufacture;   
          public:        
               Vehicle(){
               }
               void setStationary_state(bool m){
                         stationary_state = m;                 
               }  
               bool getStationary_state(){
                         return stationary_state;                 
               }    
    };

    class Bike:Vehicle{
           private: 
               string bike_type;
           public:
               void setBike_Type(string t){
                    type = t;
               }      
               string getBike_Type(){
                    return bike_type;
               }
    };      

    class Aircraft:Vehicle{
          private:
             short no_of_wings;
          public:
             void setNo_of_wings(short wings)
             {
                 no_of_wings = wings; 
             }   
             short getNo_of_wings()
             {
                  return no_of_wings;      
             }
          };

    class Car:Vehicle{
          private: 
             string reg_no;
             string type;
          public:
             void setType(string t)
             {
               if ((t=="Pneumatic") || (t=="Hydraulic"))
               {   
                  type = t;
               }
               else
               {
                  cout<<"nInvalid entry. Please enter the correct type:";
                  setType(t);        
               }
             }    
          };


    class Ship:Vehicle{
          private:
             bool has_radar_detection;  

          public:
             void setRadar_Detection(bool r){
                  has_radar_detection = r;                                           
             }
             bool getRadar_Detection(){
                  return has_radar_detection;                                 
             }    
          };

        int x;  
    main()
    {
      Vehicle v;
      Bike b;
      Car c;
      Aircraft a;
      Ship s;
      s.setStationary_state(true);
      c.setType("xyz");

      /*v.setStationary_state(true);  
      if (!(v.getStationary_state()))
      {
         cout<<"Vehicle is moving";                        
      }
      else 
      {
         cout<<"Vehicle is at rest";  
      }        
      */
      getch();    
    }

那里到底出了什么问题?错误的原因是什么以及如何避免。请详细解释。

class具有私有默认继承,因此需要指定public,即

class Ship : public Vehicle { }:

等等。默认情况下,struct具有公共继承。

您需要指定继承访问级别:

class Bike : public Vehicle

也就是说,你需要使Vehicle成为public的基础。

如果不指定访问说明符,继承将自动为private。您需要更改以下行:

 class Ship:Vehicle

到此:

 class Ship:public Vehicle

封装是通过将相关数据放在同一位置并为data.include 提供安全性来实现的

  1. private-授予同一类访问权限
  2. public-任何代码都可以访问数据
  3. protected-授予同一类、友元类和派生类的方法的访问权限

相关内容

  • 没有找到相关文章

最新更新