C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

what is private inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
MJ
Guest





PostPosted: Fri Apr 29, 2005 2:10 pm    Post subject: what is private inheritance Reply with 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

-------------------------------------------------------
#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





PostPosted: Fri Apr 29, 2005 2:18 pm    Post subject: Re: what is private inheritance Reply with quote



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





PostPosted: Fri Apr 29, 2005 8:01 pm    Post subject: Re: what is private inheritance Reply with quote




"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.