How do I access a derived classes data members when creating a pointer of the base class to hold the derived class?

43 views Asked by At

I have a class Airplane:

class Airplane {
    private:
        int value;
   public:
        // some public functions that arent relevant
};

and then another class that derives from Airplane

class Model : public Airplane {
    private:
        bool flag;
    public:
       // some functions
};

What I need to do is create a pointer of type airplane that points to the child class, aka model.

Like so: Airplane* A = new Model(); Then I can use A->functions to access the public functions of airplane to modify airplanes private data members.

My problem is I need to modify the private data members of Model. I created public member functions in model to change models private members, but it doesnt let me do A->modelfunctions. Is there a way to modify the Model private members with my system above?

My instructions were to create a dynamic instance of a Model and hold the address in an Airplane pointer. I want to do this while being able to modify private variables from both classes, either directly or through public functions. Right now I can modify "value" from airplane, but not "flag" from model using the Airplane* A = new Model(); system.

Thank you.

0

There are 0 answers