Solved

protected inheritance in C++

Posted on 2003-10-25
10
1,631 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to send memory stream from ics Client To ics server ? 11 82
Device same like our heart 12 77
Child Form in front 4 38
VB6 ListBox Question 4 33
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now