Inheritance related confusion

If a base class is publicly inherited ,member functions of derived class(inherited member functions from base class or member function of derived class) can access the private member of base class or not?
Hi,

It should be easy to write some code to show what happens there.
If you have a class that's publicly inherited from a base class

-public members of base class will become public members of the derived class
-protected members of base class will become protected members of the derived class
-private members of the base class will become inaccessible to the derived class

Hope this helps,

Hugo.
Last edited on
What is the meaning of inaccessible here,inaccessible to member function of derived class or inaccessible to oblects of derived class?
Okay consider this class:
1
2
3
4
5
6
7
8
9
10
11
12
class BaseClass
{
    private:
        int number;

    public:
        BaseClass();
        ~BaseClass();

        void setNumber(int);
        int returnNumber();
}


If you create an object of BaseClass BaseClass baseObject; You can only access number via the two public functions setNumber(int) and returnNumber() since number is private.

There is therefore no way for you to access number that way: baseObject.number
It is therefore inaccessible

So if you have a derived class from BaseClass

1
2
3
4
class DerivedClass : public BaseClass
{
    //...
}


You will neither be able to access number directly like so:
1
2
DerivedClass derivedObject;
derivedObject.number //... 


Nor will you be able to access it via any public function you make within your derived class.
It will be inexistant to the derived class.

Here is a driver program that might help you understand the whole issue:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

class BaseClass
{
	private:
		int number;
		
	public:		
		void setNumber(int);
		int returnNumber();
};

void BaseClass::setNumber(int n)
{
	number = n;
}

int BaseClass::returnNumber()
{
	return number;
}


class DerivedClass : public BaseClass
{
	private:
		
	public:
		//void attemptSetNumber(int);
		//int attemptReturnNumber();
};

/*
void DerivedClass::attemptSetNumber(int n)
{
	number = n;			//ERROR 'int BaseClass::number' is private
}

int DerivedClass::attemptReturnNumber()
{
	return number;		//ERROR 'int BaseClass::number' is private
}
*/

int main()
{
	
	BaseClass baseObject;
	
	baseObject.setNumber(5);
	cout << "\n baceObject number is: " << baseObject.returnNumber();
	
	DerivedClass derivedObject;
	derivedObject.setNumber(10);
	cout << "\n derivedObject number is:" << derivedObject.returnNumber();
	cout << "\n\n";

	

system("PAUSE");

return 0;
}
Followup to the previous test case, with private inheritance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class DerivedClass : private BaseClass
{
public:
  void attemptSetNumber(int);
};

void DerivedClass::attemptSetNumber(int n)
{
  setNumber( n );  // OK: call public base member 
}

int main()
{
	BaseClass baseObject;
	baseObject.setNumber(5); // OK: public member
	
	DerivedClass derivedObject;
	derivedObject.attemptSetNumber(10); // OK: public member
	baseObject.setNumber(5); // ERROR: 'BaseClass' is not an accessible base of 'DerivedClass'

	BaseClass& R = derivedObject; // ERROR: 'BaseClass' is an inaccessible base of 'DerivedClass'
	R.setNumber(20); // would be ok, if you could have R.
	return 0;
}

The DerivedClass inherits from BaseClass, and therefore it can access the public (and protected) members of the BaseClass.

The inheritance is private, and therefore all BaseClass members of the DerivedClass are private for outsiders.
Topic archived. No new replies allowed.