 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
MJ Guest
|
Posted: Fri Apr 29, 2005 2:10 pm Post subject: what is private inheritance |
|
|
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...
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Apr 29, 2005 2:18 pm Post subject: Re: what is private inheritance |
|
|
MJ wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
codigo Guest
|
Posted: Fri Apr 29, 2005 8:01 pm Post subject: Re: what is private inheritance |
|
|
"MJ" <mayurdjain (AT) gmail (DOT) com> wrote
| Quote: | 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 ;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|