Solved

protected inheritance in C++

Posted on 2003-10-25
10
1,660 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Simple Linear Regression
Suggested Courses

627 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