what is private inheritance

M

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 ;
}
 
V

Victor Bazarov

MJ said:
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
 
C

codigo

MJ said:
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 ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top