473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 6412

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: seesaw | last post by:
Compared to "public", how does "private/protected" in inheritance change relationship between classes what is the purpose to define constructor as "private/protected"? is there any usage to...
3
by: Mark A. Gibbs | last post by:
Good day, i'm having a bit of trouble with a base class i'm working on. this is what it boils down to: template <typename T> class foo { protected: foo() { T* p = static_cast<T*>(this); }
2
by: MJ | last post by:
Hi I have a following sample code class base and class derived. I have inherited the base class as private and tried to compile the code its giving an error "conversion from 'class derived *' to...
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Jordi | last post by:
I was wondering, in C++ the syntax for inheritance is basically this: class Descendant : public Parent { .... }; Can someone tell me what would happen when I would replace 'public' by...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
4
by: zhangyefei.yefei | last post by:
i read book <effective c++>,it tell me that public inheritance means is-a ,and private inheritance means is-implemented-in-terms-of. but today i am puzzled by some strange codes. the...
3
by: jared.grubb | last post by:
Can a private Base class method convert to/from a Derived* using static_cast? The CPL book says that a Derived method is allowed to convert to/from Base, but says nothing about whether a Base...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.