?
Solved

protected inheritance in C++

Posted on 2003-10-25
10
Medium Priority
?
1,661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Accepted Solution

by:
Mustak_Shaikh earned 720 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:Mustak_Shaikh
Mustak_Shaikh earned 720 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 280 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Six Sigma Control Plans
Suggested Courses

719 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