473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

private/protected

Compared to "public",

how does "private/protected" in inheritance change relationship between
classes

what is the purpose to define constructor as "private/protected"?

is there any usage to define destructor as "private/protected"?

Thanks!
Jul 22 '05 #1
3 2896
"seesaw" <se****@turboweb.com> wrote in message
news:2_*********************@bgtnsc05-news.ops.worldnet.att.net
Compared to "public",

how does "private/protected" in inheritance change relationship
between classes

what is the purpose to define constructor as "private/protected"?

is there any usage to define destructor as "private/protected"?

Thanks!


If you have a laundry list of questions like this, then I suggest you need
to

1. Get a good C++ textbook.
2. Consult the FAQ

http://www.parashift.com/c++-faq-lite/
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2

"seesaw" <se****@turboweb.com> wrote in message
news:2_*********************@bgtnsc05-news.ops.worldnet.att.net...
Compared to "public",
Generally I´d suggest to get a good C++ book (like Accelerated C++ by A.
Koenig & B. Moo) where all these questions are covered in depth. However,
I´ll try to give a short answer which should give you an idea.

how does "private/protected" in inheritance change relationship between
classes
public inheritance represents "is-a" relationship, whereas this is not the
case with private inheritance. This means that a class B which is privately
derived from A is NOT an object of type A and hence cannot be converted to
one (at least not by the compiler). Furthermore, all members inherited from
A are instantly private members of B, no matter of their access type in
class A!

As a consequence of all this the relation of private inheritance is rather a
"implemented in terms of" relation, which of course is something that can
also be achieved by layering. For a detailed discussion of when to use
layering and when to use private inheritance I´d refer you to Effective C++
by Scott Meyers.

what is the purpose to define constructor as "private/protected"?
This way you prevent the user from directly constructing an object.
Consequently you can specify and regulate the way such an object is created
or you can indicate that the user should only use this class as a base
class.

is there any usage to define destructor as "private/protected"?

Yes, there is and I guess you also want to know which :-) Declaring a dtor
protected will prevent the deletion of an object via a pointer for example.
Another reason would be to indicate that a certain class can only be used as
a base class.

HTH
Chris
Jul 22 '05 #3
>public inheritance represents "is-a" relationship, whereas this is not the
case with private inheritance. This means that a class B which is privately
derived from A is NOT an object of type A and hence cannot be converted to
one (at least not by the compiler).


The "is-a" relationship does not exist for public users of the derived type,
but it does exist within the derived type and is available to friends of the
derived type who are then freely able to convert between the derived type and
its private base.

A popular C++ author describes this as "controlled polymorphism."

struct Base
{
};

class Derived : private Base
{
friend void FriendOfDerived();
};

void FriendOfDerived()
{
Derived d;
Base *bptr = &d; //Okay! Conversion available here!
}

int main()
{
Derived d;
Base *bptr = &d; //Error! Conversion not available here!

return 0;
}

Is this "controlled polymorphism" actually used in real-word designs?

I would be curious to know.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
2
by: MJ | last post by:
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...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
7
by: My4thPersonality | last post by:
1st, simple question, is there a protected in C#? I mean a comparable keyword like the C++ protected, which defines a member as accessable for decendants, but not for the outside world. Then,...
4
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
5
by: JamesO | last post by:
Hello all, Is there a way to make a class private to the namespace so that other classes in the namespace can see and use it but noone outside a namespace can see or use it? Feel free to send...
2
by: Morten71 | last post by:
Just curious. In VB, if I, from design-mode in vs2005, double-click a button (or any other control), the code-behind file opens and the default declaration is automatically inserted with method...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.