Page 572, C++ primer, 4th edition

A

asdf

In the example, d2.basemem() is error. I don't know why, basemem is
private member function in the derived class d2, but d2 can use its own
member function, no matter what type it is.
 
H

Howard

asdf said:
In the example, d2.basemem() is error. I don't know why, basemem is
private member function in the derived class d2, but d2 can use its own
member function, no matter what type it is.

How about posting the actual code you've got a question with? Is there some
reason you assume we all have that book?

-Howard
 
Z

Zara

In the example, d2.basemem() is error. I don't know why, basemem is
private member function in the derived class d2, but d2 can use its own
member function, no matter what type it is.

Try to post code...

From the book, but somehow snipped:

class Base {
public:
void basemem();
protected:
int i;
};

struct Public_derived : public Base {
int use_base() {return i;}
};

struct Private_derived : private Base {
int use_base() {return i;}
};

And the lines the OP was refering to:

Base b;
Public_derived d1;
Private_derived d2;

b.basemem(); // ok...
d1.basemem(); // ok...
d2.basemem(); // error...

The problem, dear OP, is that d2 may use all functions from its base
B, but functions *outside* d2 may not use those inherited from Base.

See it this way:
The public part of a class is the outside image you let the rest see.
The protected part is the purse, you only let your family
(descendants) see.
The private part lies within your underpants.

If one of your descendants is proud of you, he inherits publicly form
you and puts your photo on a badge on the lapel, for everyone to see.

But if you embarrass him, and wants to hide the fact that he inherits
from you, he will do it privately. He is putiing you within his
underpants, no way for the others to see you.

Maybe that helps you understand the idea of the kinds of inheritance.

Regards,

Zara
 
K

KiLVaiDeN

Zara said:
[...] *snipped* : Some nice analogies ;)

Dear Zara,

I assume that with this nick, you are a girl !

Therefore, I would like to ask you a question; Following your analogy,
it seems to me that there is a field of confusion. What happens if we
marry ?

Will I have access to your private members ? :)
Will you have access to mine ?
Will I be the only one to be able to access to them ?

What will be the state of... our common bank account ?

Cheers, Keep it up ;)
K
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top