Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to make the public inherented member function private or protected?

Reply
Thread Tools

How to make the public inherented member function private or protected?

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      10-15-2006
Suppose I have class A, which defines a lot of member functions a1 ...
an. The class B publically inherent from A, because it want to use A's
member function.

But B only want make member function a1() private. Is there any easy
way to do it?

Thanks,
Peng

class A{
public:
A() {}
int a(int) {}
int a1(int) {}
int a2(int) {}
int a3(int) {}
....
....
int an(int) {}
};

class B : public A{
public:
B() {}
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Suppose I have class A, which defines a lot of member functions a1 ...
> an. The class B publically inherent from A, because it want to use A's
> member function.
>
> But B only want make member function a1() private. Is there any easy
> way to do it?


Sure. Let B declare its own 'a1' and make it private. What's the
problem?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
iracly.kv@gmail.com
Guest
Posts: n/a
 
      10-15-2006
Just try:
class B
{
public:

private int A::a1();
}

AFAIK - this is supported by C++ standard

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-15-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> Suppose I have class A, which defines a lot of member functions a1 ...
> an. The class B publically inherent from A, because it want to use A's
> member function.


That is not a valid reason to publicly inherit from a class.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      10-15-2006

(E-Mail Removed) wrote:
> Suppose I have class A, which defines a lot of member functions a1 ...
> an. The class B publically inherent from A, because it want to use A's
> member function.
>
> But B only want make member function a1() private. Is there any easy
> way to do it?
>
> Thanks,
> Peng
>
> class A{
> public:
> A() {}
> int a(int) {}
> int a1(int) {}
> int a2(int) {}
> int a3(int) {}
> ...
> ...
> int an(int) {}
> };
>
> class B : public A{
> public:
> B() {}
> };


That is what overriding a function does. If you declare function int
a1() in B, it hides A::a1(). You can still call A::a1() from B and it
doesn't mater if B::a1() is public or private. You can also declare
a1() as protected in A - although that often makes poor logic.

class B : public A
{
public:
B() {} // invokes A's ctor automatically
int a1() { A::a1(); }
};

If you plan to store instances of B using pointers to A, the
destructors need to be virtual. I'ld suggest learning about
pure-virtual functions and abstract classes too.
You should be very careful when using inheritance. Often enough, the
relationship should be one of composition (a car has-a motor) rather
than derivation.

class B
{
A a;
public:
B() : a() { }
int a1() { a.a1(); }
};

In which case A's member functions are not part of B's interface.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      10-16-2006
(E-Mail Removed) wrote:
> Just try:
> class B
> {
> public:
>
> private int A::a1();
> }
>
> AFAIK - this is supported by C++ standard
>


It's not.
 
Reply With Quote
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      10-17-2006


On Oct 15, 5:15 pm, "Daniel T." <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> > Suppose I have class A, which defines a lot of member functions a1 ...
> > an. The class B publically inherent from A, because it want to use A's
> > member function.That is not a valid reason to publicly inherit from a class.


What would be a valid reason?

For my particular example, for example, there are 100 member functions
for A like a1,...,a100?
I want to have a class, which is almost same as B, except a few member
functions need to be modified or added. I think it is valid to derive B
from A practically.

 
Reply With Quote
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      10-17-2006


On Oct 15, 2:49 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Suppose I have class A, which defines a lot of member functions a1 ...
> > an. The class B publically inherent from A, because it want to use A's
> > member function.

>
> > But B only want make member function a1() private. Is there any easy
> > way to do it?Sure. Let B declare its own 'a1' and make it private. What's the

> problem?


There is no problem. I didn't think of this solution.

Thanks,
Peng

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-17-2006
> On Oct 15, 5:15 pm, "Daniel T." <(E-Mail Removed)> wrote:
>> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>>
>>> Suppose I have class A, which defines a lot of member functions a1
>>> ... an. The class B publically inherent from A, because it want to
>>> use A's member function.

>>
>> That is not a valid reason to publicly inherit from a class.

>
> What would be a valid reason?


Because you want clients of A to be able to use a B as if it's an A.

> For my particular example, for example, there are 100 member
> functions for A like a1,...,a100? I want to have a class, which is
> almost same as B, except a few member functions need to be modified
> or added. I think it is valid to derive B from A practically.


It's hard to say if public inheritance is a good idea from your above
discription, you make no reference to the clients of A at all.

If you don't want people to be able to call the function 'a1' on a B
object and A has the function 'a1' publicly available, then you *cannot*
publicly derive from A. It's that simple.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-17-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> On Oct 15, 2:49 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>
>>> Suppose I have class A, which defines a lot of member functions a1
>>> ... an. The class B publically inherent from A, because it want to
>>> use A's member function.
>>>
>>> But B only want make member function a1() private. Is there any
>>> easy way to do it?

>>
>> Sure. Let B declare its own 'a1' and make it private. What's the
>> problem?

>
> There is no problem. I didn't think of this solution.


The problem is, that it won't work.

void fn( B* b ) {
A* ab = b;
ab->a1();
}

'a1' will still be available to clients and callable.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments