Private vs. protected inheritance

C

Christian Meier

Hi dear programmers

I looked for the difference between private and protected inheritance, but
couldn't find anything.
Here is my sample code:

#include <iostream>
using std::cout;
using std::endl;


class Base
{
public:
Base() { cout << "***Base constructor called***" << endl; }
~Base() { cout << "***Base destructor called***" << endl; }

protected:

void coutAnything() { cout << "Anything" << endl; }

};

class Derived : protected Base
{
public:
Derived() { cout << "***Derived constructor called***" << endl; }
~Derived() { cout << "***Derived destructor called***" << endl; }

void coutAnythingDerived() { coutAnything(); }

};


int main()
{
Derived derivedInstance;

derivedInstance.coutAnythingDerived();
}


It does not matter if I have a private or a protected inheritance.... Can
anyone add a function where I can see the difference? The difference to a
public inheritance is clear by the way. I have only problems with protected
/ private.

Thanks for your help!

Regards,
Chris
 
A

Allan Bruce

Christian Meier said:
Hi dear programmers

I looked for the difference between private and protected inheritance, but
couldn't find anything.
Here is my sample code:

#include <iostream>
using std::cout;
using std::endl;


class Base
{
public:
Base() { cout << "***Base constructor called***" << endl; }
~Base() { cout << "***Base destructor called***" << endl; }

protected:

void coutAnything() { cout << "Anything" << endl; }

};

class Derived : protected Base
{
public:
Derived() { cout << "***Derived constructor called***" << endl; }
~Derived() { cout << "***Derived destructor called***" << endl; }

take out this line below
void coutAnythingDerived() { coutAnything(); }

Allan
 
C

Christian Meier

Allan Bruce said:
take out this line below

Allan

And what next? Try to call the funtion coutAnything() in the main() like
this:

derivedInstance.coutAnything();

That doesn't work.... not with protected nor with private inheritance...

- Chris
 
H

hall

On 2004-04-26 13:44 Christian Meier spoke thusly

And what next? Try to call the funtion coutAnything() in the main() like
this:

derivedInstance.coutAnything();

That doesn't work.... not with protected nor with private inheritance...

- Chris

Exactly. coutAnything() is a protected member of Base and thus it will
become a protected member of your Derived class. So, it doesent work in
the same way as you cannot a protected or private member of any class
from outside.

If you want to see the difference between private and protected
inheritance, you will have to study what happens to the access to public
members of your base class in the derived class. (Well, among other
things...)

/h
 
A

Allan Bruce

And what next? Try to call the funtion coutAnything() in the main() like
this:

derivedInstance.coutAnything();

That doesn't work.... not with protected nor with private inheritance...

sorry, you are right.
The main difference between private and protected is that if you inherit a
class which has private members then the derived class cannot access them,
however if the members had been protected then they would be accessible from
the derived class, for example:

#include <iostream>
using std::cout;
using std::endl;

class Base
{
public:
Base() { cout << "***Base constructor called***" << endl; }
~Base() { cout << "***Base destructor called***" << endl; }

protected:
int mProtectedNum;

private:
int mPrivateNum;

};

class Derived : protected Base
{
public:
Derived() { cout << "***Derived constructor called***" << endl; }
~Derived() { cout << "***Derived destructor called***" << endl; }

void setProtected(int xiIn){mProtectedNum = xiIn;}
//void setPrivate(int xiIn){mPrivateNum = xiIn;} /* problem here - cannot
access mPrivateNum as it is private in the superclass */

};

int main()
{
Derived derivedInstance;

derivedInstance.setProtected(5);
derivedInstance.setPrivate(10);

return 0;
}



HTH
Allan
 
C

Christian Meier

Thanks for your explanations but I got it now.
I began to understand when I made three classes:
class Base;
class Derived1 : protected /*private*/ Base;
class Derived2 : public Derived1;

Derived2 can only access to members of Base if Derived1 has a protected
inheritance....

Thanks for your help!

Regards,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top