Solved

protected inheritance in C++

Posted on 2003-10-25
10
1,644 Views
Last Modified: 2013-11-26
To C++ Expert :

 I have a code looks like the follows :

class Base{
  public:
    Base(){}
    virtual ~Base(){}
} ;

class Derived: protected Base{
  public:
    virtual ~Derived(){}
} ;

int main()
{
    Base* pb = new Derived ;
    return 0 ;
}

However, it won't compile .... the error message says
 " fields of `Base' are inaccessible in `Derived' due to  
   private inheritance " ...

Therefore, I have a few questions here :
1. I use a protected inheritance ..... why it complains
   about the private inheritance ?
2. Why a pointer of the base type can not point to the
    class of derived type ? Is that just because it is a
    protected inheritance ? If so, why ?

 Thanks a lot !
 
meow .......
0
Comment
Question by:meow00
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Accepted Solution

by:
Mustak_Shaikh earned 180 total points
ID: 9620310
Whenever you do a protected inheritence all the public base class members become protected derived class members and all protected base class members become private derived class members.
A pointer of the base type can indeed point to the class of derived type , there is no problem as long as if you public inheritence. because when you do public inheritence base class interface is a subset of derived class's interface , so in that case there is no problem of pointing to derived class's object with base object pointer.
Even when we do public inheritence , and when we make base pointer pointing to derived class's object , basically this pointer pointer points to common data members and functions i.e. the common interface.
as this case is not possible with niether protected nor private inheritence.

let me know if need more info on this.

Mustak_Shaikh
0
 
LVL 1

Author Comment

by:meow00
ID: 9620970
got a general idea now ... but would u mind explaining a bit more
about the "base class interface" and "derived class interface" ?
do they mean data members and member functions ? or they mean something more than that ? thanks !
0
 
LVL 9

Assisted Solution

by:tinchos
tinchos earned 50 total points
ID: 9621698
Yes, I believe that Mustak refered to all the members and methods in the class.

I believe he meant the following.

class Base
{
public:
             int getX();
             int getY();

             int x;
             int y;
}

class Derived
{
             int getW();
             int getX();

             int w;
             int z;
}

in this case the base class interface would be

             int getX();
             int getY();

             int x;
             int y;

and the derived interface

             int getX();
             int getY();

             int x;
             int y;

             int getW();
             int getX();

             int w;
             int z;

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 1

Assisted Solution

by:Mustak_Shaikh
Mustak_Shaikh earned 180 total points
ID: 9621935
tinchos,

yes you are correct.
continuing with your answer:
in this scenario if we see the comman interface, then
that interface is having following member function only:
int getX()
so if you declare this
Base *ptrbase = new derived;
this ptrbase will only point to getX() only.
now coming back to your problem, your common
interface is having no data member or funciton at all,
since base class interface and derived class interface are disjoint set,
so you can't do this:
Base *ptrbase = new derived;

regards,
Mustak_Shaikh
0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9621948
tenchos,

i think in your example you have forgotted to include public:
word in Dervied class defination, since by default scope
is private and in that your derived class's interface is null or empty.
0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9621961
All,
there is a small correction about my last comment
where i said only getX() function will be there in common interface
part, but rather all the public data members and function will be there in it.
i.e.
  int getX();
  int getY();

  int x;
  int y;


Regards,
Mustak_Shaikh
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9621968
Yeap, you're right

I forgot the public keyword in the derived class

It would be like

class Base
{
public:
             int getX();
             int getY();

             int x;
             int y;
}

class Derived
{
public:
             int getW();
             int getX();

             int w;
             int z;
}
0
 
LVL 1

Author Comment

by:meow00
ID: 9622633
I see .... so is that true that :
when a pointer of base type points to a derived object:
it can only points to the "public , common" members in the derived class. If there is no "public, common" members in the derived class, we can not use a base type pointer to point to a derived object ?
Thanks  a lot !
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 70 total points
ID: 9623893
meow00:

> 1. I use a protected inheritance ..... why it complains
>    about the private inheritance ?

Just to reiterate what was said before:  When you use protected inheritance, everything about the base class is PRIVATE to everything outside of the derived class.  That's why it says "private".

> 2. Why a pointer of the base type can not point to the
>     class of derived type ? Is that just because it is a
>     protected inheritance ? If so, why ?

"Protected" and "Private" inheritance basically HIDE the fact that the Derived class is based on Base class.  When you hide that fact, the compiler will no longer let you treat pointers to Derived as pointers to Base.  Make it public inheritance, then it will let you.

Hope that helps,
Dex*
0
 

Expert Comment

by:Scarecrow248
ID: 9630839
 Hi there!
  Sorry guys but i don't agree that "When you do a protected inheritence  all protected base class members become private derived class members."
  As i know
+In (protected) derived class the public and protected parts of Base are protected but
+the private parts are still private
 (if you don't belive me, please check atutorial at :
http://www.parashift.com/C++-faq-lite)
 I also think that somewhere in his program mewoo have tried to access private data or methods of Base from Derived.
ScareCrow
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question