Thread: Can't reach protected variable in a derived class?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Can't reach protected variable in a derived class?

    Hi! I don't understand why my compiler gives me this error when I'm trying to run this code:

    Code:
    #include <iostream>
    #include <cmath>
    
    
    using namespace std;
    
    
    class Airplane
    {
        public:
            Airplane();
            ~Airplane();
    
    
        protected:
            int fuel;
            int speed;
            int metersOverGround;
            int steeringWheel;
    
    
        private:
            void ShowData(const Airplane& ap);
    };
    
    
    Airplane::Airplane  () {}
    Airplane::~Airplane () {}
    
    
    class MultiAirplane : public Airplane
    {
        public:
            MultiAirplane(int fuel, int speed, int metersOverGround, int steeringWheel);
            ~MultiAirplane();
    
    
        private:
            Airplane multiAirplane;
    };
    
    
    MultiAirplane::MultiAirplane(int fuel, int speed, int metersOverGround, int steeringWheel)
    {
        cout << "MultiAirplane" << endl;
        this->multiAirplane.fuel = fuel; // WHY WON'T THIS WORK??
    }
    
    
    MultiAirplane::~MultiAirplane()
    {
    
    
    }
    
    
    class SingleAirplane : public Airplane
    {
        public:
            SingleAirplane();
            ~SingleAirplane();
    };
    
    
    void Airplane::ShowData(const Airplane& ap)
    {
        cout << "Speed: " << ap.speed            << endl;
        cout << "Fuel:  " << ap.fuel             << endl;
        cout << "M.O.G: " << ap.metersOverGround << endl;
        cout << "S.W:   " << ap.steeringWheel    << endl;
    }
    
    
    int main(int argc, char* argv[])
    {
        return 0;
    }
    error: 'int Airplane::fuel' is protected
    error: within this context

    The variable is protected. Yeah, that's right. But shouldn't a derived class be able to reach it? Or is it only in a function that the derived class is able to reach protected variables and isn't able to reach protected variables in the constructor?

    Thanks for help!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can access protected variables. When using inheritance.
    This:

    private:
    Airplane multiAirplane;

    is composition. When using composition, the default rules apply: you can only access public members.
    But you are doing it wrong. You are inheriting from Airplane, so you don't need this extra "instance."
    All the members from Airplane is in MultiAirplane already.

    So this:
    this->multiAirplane.fuel = fuel; // WHY WON'T THIS WORK??
    should be
    this->fuel = fuel; // WHY WON'T THIS WORK??
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    So this:
    this->multiAirplane.fuel = fuel; // WHY WON'T THIS WORK??
    should be
    this->fuel = fuel; // WHY WON'T THIS WORK??

    It is usually preferable for the MultiAirplane constructor to pass the arguments directly to the Airplane constructor via the initialiser list.
    Code:
    MultiAirplane::MultiAirplane(int fuel, int speed, int metersOverGround, int steeringWheel) : Airplane(fuel, speed, metersOverGround, steeringWheel)
    {
    }
    Of course, that does require Airplane to supply an appropriate constructor but - since all the members in this case are inherited from Airplane, that would normally be sensible anyway.

    It appears, in this case, that the only reason for making Airplane's members protected is to avoid providing appropriate constructor (or accessors). That is poor design.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Replies: 2
    Last Post: 04-19-2011, 10:03 PM
  3. Access protected base class template variable
    By pjotr in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2007, 05:34 AM
  4. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  5. How can I reach Protected Variables?
    By zayzay in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 06:32 AM