468,727 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

Private vs. protected inheritance

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
Jul 22 '05 #1
5 6192

"Christian Meier" <ch***@gmx.ch> wrote in message
news:c6**********@newshispeed.ch...
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
Jul 22 '05 #2

"Allan Bruce" <al*****@TAKEAWAYf2s.com> schrieb im Newsbeitrag
news:c6**********@news.freedom2surf.net...

"Christian Meier" <ch***@gmx.ch> wrote in message
news:c6**********@newshispeed.ch...
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


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
Jul 22 '05 #3

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

"Allan Bruce" <al*****@TAKEAWAYf2s.com> schrieb im Newsbeitrag
news:c6**********@news.freedom2surf.net...
"Christian Meier" <ch***@gmx.ch> wrote in message
news:c6**********@newshispeed.ch...
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

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


--
( - Remove capital X from email to reply - )

Jul 22 '05 #4
> >
take out this line below
void coutAnythingDerived() { coutAnything(); }


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...


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
Jul 22 '05 #5
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
"Allan Bruce" <al*****@TAKEAWAYf2s.com> schrieb im Newsbeitrag
news:c6**********@news.freedom2surf.net...

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

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...


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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by seesaw | last post: by
3 posts views Thread by Mark A. Gibbs | last post: by
2 posts views Thread by MJ | last post: by
1 post views Thread by Tony Johansson | last post: by
8 posts views Thread by __PPS__ | last post: by
6 posts views Thread by Jordi | last post: by
4 posts views Thread by zhangyefei.yefei | last post: by
3 posts views Thread by jared.grubb | last post: by
reply views Thread by zhoujie | last post: by
xarzu
1 post views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.