By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

what is private inheritance

P: n/a
MJ
Hi
I have a following sample code
class base and class derived.
I have inherited the base class as private and tried to compile the
code its giving an error
"conversion from 'class derived *' to 'class base *' exists, but is
inaccessible"
If I inherit using public it works well ...

I am not very clear about the private inheritance

I will be happy if someone can guide me ... when shall I use public,
private and protected inheritance

-------------------------------------------------------
#include<iostream>
using namespace std;
class base{
public:
int b;
virtual f(){ cout << " base" << endl;}
};
class derived:private base{
public:
int d;
f()
{
cout << " derived" << endl;
}
};
int main () {
derived *d = new derived;
d->f();
base *b;
b = d;
int a;
cin >> a;
return 0 ;
}
-------------------------------------------------------

Regards
MJ...

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
MJ wrote:
I have a following sample code
class base and class derived.
I have inherited the base class as private and tried to compile the
code its giving an error
"conversion from 'class derived *' to 'class base *' exists, but is
inaccessible"
If I inherit using public it works well ...

I am not very clear about the private inheritance

I will be happy if someone can guide me ... when shall I use public,
private and protected inheritance


Private and protected inheritance are not to be used in the situation
that requires "is-a" relationship.

Otherwise, private and protected inheritance is just the same as declaring
members private or protected. Private ones are only accessible to members
and friends, and protected ones are accessible to members, friends, and
classes that derive from this one. Think of the base class as a member of
the class that derives from it. Anything related to that "member" should
follow the rules of access qualifiers. Conversions from derived to base
is part of that "anything".

V
Jul 23 '05 #2

P: n/a

"MJ" <ma********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi
I have a following sample code
class base and class derived.
I have inherited the base class as private and tried to compile the
code its giving an error
"conversion from 'class derived *' to 'class base *' exists, but is
inaccessible"
If I inherit using public it works well ...

I am not very clear about the private inheritance

I will be happy if someone can guide me ... when shall I use public,
private and protected inheritance


Private inheritence is usually a form of composition. The resulting class
has the functionality and members of the base class (but that functionality
is hidden inside the base). Unlike public or protected inheritence, you
can't state that the derived type is_a base type. This relationship is
usually described as: The Derived class in_terms_of the base class.

#include <iostream>
using std::cout;
using std::endl;

class Base
{
int b;
public:
Base(int n): b(n) { }
virtual ~Base() { }
void f() const
{
cout << "(base) b = " << b;
}
};

class Derived : private Base
{
int d;
public:
Derived(int n, int m) : Base(n), d(m) { }
~Derived() { }
void f() const
{
cout << "(derived) d = " << d;
cout << endl;
Base::f();
cout << endl;
}
};

int main()
{
Derived *d = new Derived(100, 10);

d->f();

delete d;

return 0 ;
}
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.