C++ Inheritance Question

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Most of my programming experience insofar has been C# and Java, I've been working with C++ lately and my question is about the use of the public modifier when using inheritance.

An Example:

class BaseClass
{
// Blah Blah Blah
};

class DerivedClass : public BaseClass
{
// More Blah
}

What is the meaning of the "public" following the colon? I've been looking around on the internet and most things just say to use it without commenting on its meaning.

Also, would:

class DerivedClass : private BaseClass {};

or

class DerivedClass : BaseClass{};

ever desirable?
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
I see, so essentially using private would essentially encapsulate your base class within your derived class, meaning you wouldn't have access to any public members of BaseClass outside of the derived class (unless you write that functionality back into your derived class).
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: ObscureCaucasian
I see, so essentially using private would essentially encapsulate your base class within your derived class, meaning you wouldn't have access to any public members of BaseClass outside of the derived class (unless you write that functionality back into your derived class).

Correct

Private blocks people from outside the class from sneaking a peek as to what is inside.

Private is the default; so you must declare Public what you desire to expose.

 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
And the main point is, that having blocked public visibility of those inherited members, the derived type cannot be said to have an "is-a" relationship with the base type. Thus my calling it composition rather than specialization. There are better and more explicit means of creating composition relationships, so I stay away from private inheritance.