0

From what I have learned in class, derived classes do not inherit their parent class's:

  • Constructors/Destructors
  • Friends
  • Private Members

However if we upcasted the derived class to its parent class, would we have access to the private members of the parent class and would the Parent class's friends now apply to this upcasted class?

I am inclined to think the upcasted derived class will not be able to access the parent's private methods and variables since there is a reason the derived class does not inherit them in the first place.

(Below code was added after the verified answer was verified)

This c++ code was used to test the question I asked:

    #include <iostream>
    class A;
    void funcGetAVal(A);

    class A
    {
        private:

            int aVal;
            friend void ::funcGetAVal(A);
        public:
            A()
            {
                aVal = 0;
            }
    };

    class B : public A
    {
        public:
            int bVal;
            B() : A()
            {
                bVal = 0;
            }
    };

    void funcGetAVal(A aClass)
    {
        std::cout << "A val accessed from friend function: " << aClass.aVal << std::endl;
    }


    int main()
    {
        B * b = new B;
        A * a = new A;
        A * bNowA = reinterpret_cast<A *>(b); //This reinterprets the instance of the derived class into an instance of the base class
        //std::cout << bNowA->aVal << std::endl; //This caused a compile-time error since aVal is a private member of A
        ::funcGetAVal(*a); //This calls the funcGetAVal function with an instance of the base class
        ::funcGetAVal(*bNowA); //This calls the funcGetAVal function with an instance of the derived class reinterpreted as the base class
        //Both funcGetAVal function calls print out 0
        return 0;
    }

The results are in line with R Sahu's answer. Access of the private member of A from an instance of B caused an error, and the friend function was able to access the derived class's aVal.

aj1996
  • 15
  • 6
  • 1
    What do you mean by "accessing" private methods and variables? You are not able to access private methods and variables regardless of how you cast it. – adam Mar 17 '16 at 03:02
  • By "accessing" I mean calling the function or using the variable with the base class. For example, if class A has a private integer: aVal, and class B inherits A, then can B access aVal if B has a instance named b by entering b.aVal? Your second sentence answers this question about the private member access. – aj1996 Mar 17 '16 at 03:09
  • 1
    You have misinterpreted something. A parent class is completely included in the derived instance. Just because attribute is made unavailable (by private), does not mean the attributes are not there. Up-casting (of a derived instance to a base object) is never needed and ill advised. Please re-read the "is-a" discussion in your text book(s). – 2785528 Mar 17 '16 at 03:09
  • 1
    I suggest you try it ... use 'reinterpret_cast', write a [MCVE], and tell us what you are having problems with. – 2785528 Mar 17 '16 at 03:12
  • Indeed, all the private variables and methods will be there in the inherited class, but you cannot directly use them, even if you upcast it to the parent class. This is not because of inheritance, it is because of the functions and variables being private. – adam Mar 17 '16 at 03:14
  • The help and suggestions are greatly appreciated, I coded up a test example, added it into the original post, and it all makes sense now. – aj1996 Mar 17 '16 at 04:33

1 Answers1

1

However if we upcasted the derived class to its parent class, would we have access to the private members of the parent class

No.

private members of a class are accessible in the scope of the class. A member function of a class can access its private members. By upcasting a derived class pointer to a base class pointer in a function that is not a member function of the base class, you won't be able to access the private members of the base class. You can only access its public members.

and would the Parent class's friends now apply to this upcasted class?

If you pass a pointer to the base class to a friend of the base class, then the friend function can access the private and protected members of the base class.

R Sahu
  • 204,454
  • 14
  • 159
  • 270