do private members in base clases get inherited in derived classes?

I need to know if a private member in the base class will be inherited in a derived class. As such:
(I AM USING @ AS TABS)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

class base
@@@@{
@@@@private:
@@@@@@@@int something
@@@@}
class derived::base
@@@@{
@@@@int noreason
@@@@}


would derived inherit the something integer?
Yes.

Why not spaces?
No. Private is not inherited. That is what protected is for.
Last edited on
And this is why I shouldn't answer questions on here while I'm watching videos - definitely read 'protected' there... sorry
:P I do that sometimes too, scroll through type and answer while I am reading another forum or emails.
In fact, the whole base class is inherited.
The question arises what of those inherited are accessible to its derived class.

For example, a bird lays eggs and flies, that characteristic is all inherited down the hierarchy, but the derived class (like chicken) can NOT CHANGE or access the flying hieght of bird family, though it can have its own version (like virtual/overriding) version of flying function.

The access levels of public, private and proted are to identify what properties of the parent/class are accessible to its children and instantiated objects.

Hope it is clear now. Good luck :)






when you derive a class, how it will be inherited depends on how you declare it.

when you use public derive, protected data members will be inherited, but not private data members. lets say you declare something under private. if you want it to be inherited, change the private to protected.

what i mean is :

class baseclass
{
protected:
int something;
}
class derivedclass : public baseclass
{
private:
char somethings;
}

by doing so, the data member something gets inherited into class derivedclass
@bentan:

As noted above, the public/private/protected are to show only the what access level is given to the derived classes.

A data property or funcationality from base class, private, protected or public, all are inherited to children/derived class. But the access level given for that property or funcationality determines which one could be accessed by its dervied class or object instance.

For example:
class Car {
private:
Wheel wheels[4];
int brakeType;
protected:
bool isFourWheelDrive;
};

class Sedan : public Car
{
public:
changeDrive() { isFourWheelDrive = false; //changed from default 4 to 2 }

// but brakeType and numberOfWheel are not accessible
};


In above example, do you think the Sedan does not have a "Wheel" as they are private to Car and not inherited (as you say).
NOOPE. They are inherited as they are part of the CAR/SEDAN and without one it is a not a vehicle.
But the Sedan does not have access to change the number of wheels it built upon and type of disk/drum brakes using.

The public/private/protected is an ACCESS level and does not prevent the functionality and data inheritance when a child class derives from base class.
In the above example, Wheels are protected, so the Sedan design does/can not change the number of wheels nor its object (I can not ask a car dealer to put an extra wheel or remove a wheel from the Sedan object I would be buying; the object can not be changed, PERIOD.)
That is, a "private" property of the Car design.


Hope you understand better now. Good luck :)
Last edited on
Yea. I should've been more clear.

They are inherited yes. But they are NOT usable from the child class. Thus access to them is not inherited :)
Topic archived. No new replies allowed.