Solved

protected inheritance in C++

Posted on 2003-10-25
10
1,617 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Specific format 21 144
Adding to a VBA? 6 49
Visual Studio 2005 text editor 10 23
How to make an ADE file by code? 11 42
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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 …

747 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

11 Experts available now in Live!

Get 1:1 Help Now