简体   繁体   中英

How to implement Inheritance in C++ and resolve the error “parent class is not accessible base of child class”?

I am new to C++. I like to explore the idea of Inheritance in C++. Whenever I try to compile the following code I get the error:

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

Execution terminated

Here is my code:

 #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();    
    }

What is really wrong there? What is the cause of the error and how to avoid it. Please explain in detail.

class has private default inheritance, so you would need to specify public , ie

class Ship : public Vehicle { }:

ans so on. struct has public inheritance as default.

You need to specify inheritance access level:

class Bike : public Vehicle

Ie you need to make Vehicle a public base.

If you don't specify an access specifier, inheritance is automatically private . You need to change these line:

 class Ship:Vehicle

to this:

 class Ship:public Vehicle

encapsulation is attained by placing related data in the same place and offering security to the data.include

  1. private - access granted to same class
  2. public - any code can access the data
  3. protected - access granted to methods of the same class,friend class and derived class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM