Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I want to reach the private variables of the parent class, But when I recall the display() method, it shows the variables in the derived class. I appreciate if someone can tell me how to do this. And also there is no change when I declare the inheritance through public keyword or without it, why is it so?
Much Thanks.

C++
#include <iostream>

using namespace std;

class parent_class {
private:
    int private1, private2;
public:
    void assign(int p1, int p2) {
        private1 = p1;
        private2 = p2;
    }
    void display() {
        printf("\nprivate1=%-5d private2=%-5d", private1, private2);
    }
};

class derived_class :parent_class {
private:
    int private3;
public:
    void assign_derived(int p1, int p2, int p3) {
        assign(p1, p2); private3 = p3;
    }
    void display_derived() {
        printf("\nDerived Class Variables:");
        display();
        printf("\n private3=%-5d\n", private3);
    }
};

int main() 
{
    parent_class parent;
    derived_class derived1; 
    
    parent.assign(-5, -12);
    derived1.assign_derived(20, 32, 45);

    //derived1.display();
    derived1.display_derived();

    return 0;
}


What I have tried:

I tried accessing the parent variables in the main function, but I want to be able to reach them through the derived class itself, not main().
Posted
Updated 11-Apr-23 13:29pm
v2
Comments
0x01AA 11-Apr-23 15:59pm    
Looks like you mix up the things....
In your main you instanciate parent_class parent; and derived_class derived1; and I think that ends in your confusion.
These two instances are completely unrelated instances and each of these instances hold it's _own_ data.

The instance parent has private1 equals to -1 and private2 equals to -12.
While the instance of derived1 has the values private1 of 20 and private2 of 32 (both stored in base class/in other words members of the base class) and additinal the member private3 of 45, member of the dreived class.

Again: The instances of parent_class parent; and derived_class derived1; are completely unrelated variables.

To add to what Carlos has said ... When you derive a class, the child class inherits all the properties, methods, and variables of it's parent class, with the access modifiers unchanged - even if you declare the derived class as public, the private members of the parent remain private

And private members of a class are only available within that specific class: they are not accessible by any other class, even if it is derived from the original.
This is part of the specification of the language, and is there so that a class can hide implementation details from all other classes, and thus change the implementation without affecting any other class, provided that the methods the "outside world" can access perform exactly the same function as a black box - same inputs, same outputs - but the way that function works can be wildly different!

If the parent class wants variables accessible by derived classes, it should mark them as protected, not private
 
Share this answer
 
v2
The variables 'in the derivate class' ARE the variables of the parent class.
There is an object, instance of the derivate class, which has all the variables: the ones declared in the parent (base) class and the ones declared in the derivate one.
 
Share this answer
 
v2
The two previous solutions explained the situation well. The only part missing is that one common way to deal with this situation is to add Get and Set methods to the base class that are declared as protected or public. Methods like those are often implemented to access private member variables. They are usually not just called Get and Set and usually have the variable's name included in addition to the words Get and Set.
 
Share this answer
 
Quote:
I want to be able to reach them through the derived class itself, not main().

Confusion probably arises here because the name, the value, and the visibility of a member variable within a class are not cleanly differenced. Basically, one must clearly differentiate between the name and the value of a variable. In each instance of a class, there are variables with the same name, but each variable has its own value.

Additionally, one can still specify the visibility of a member variable. This determines whether a caller of the class can see it at all. The usual way to set up controlled access to member variables was already mentioned by Rick York. A setter for the variables is in the program above the method assign() a suitable counterpart, e.g., readout() would make it possible to read the variables again.

One could add, still, that with the production of a derived class always also its own instance of the parent class is produced. A possible confusion here, or one could call it also problem in the program above, could be prevented, if one does not create an instance of the parent class, since this happens anyway automatically each time.

If one would like that in principle always in all derived classes the variables of the parent class also have the same value, still the keyword "static" could be a solution.

C++
class derived_class :parent_class {
private:
    int private3;
public:
    void assign(int p1, int p2, int p3);
    void readout(int &p1, int& p2, int& p3);
};

The call could then look like this:
C++
// parent_class parent;
derived_class derived1; 
    
// parent.assign(-5, -12);
derived1.assign(20, 32, 45);

//derived1.display();
printf("\nDerived Class Variables:");
int p1, p2, p3;
derived1.readout(p1, p2, p3);
cout << "private1 = " << p1 << "private2 = " << p2 << "private3 = " << p3 << endl;

An alternative instead of reading out all variables at once with readout() would be to read them out individually, e.g. with int get_p1().

One more note about the code above : With C++, the output stream cout should be used instead of the C function printf() for good reasons.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900