473,320 Members | 1,991 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.

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

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() {}
};

Oct 15 '06 #1
9 2064
Pe*******@gmail.com 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
Oct 15 '06 #2
Just try:
class B
{
public:

private int A::a1();
}

AFAIK - this is supported by C++ standard

Oct 15 '06 #3
"Pe*******@gmail.com" <Pe*******@gmail.comwrote:
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.
Oct 15 '06 #4

Pe*******@gmail.com 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.

Oct 15 '06 #5
ir*******@gmail.com wrote:
Just try:
class B
{
public:

private int A::a1();
}

AFAIK - this is supported by C++ standard
It's not.
Oct 16 '06 #6


On Oct 15, 5:15 pm, "Daniel T." <danie...@earthlink.netwrote:
"PengYu...@gmail.com" <PengYu...@gmail.comwrote:
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.

Oct 17 '06 #7


On Oct 15, 2:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
PengYu...@gmail.com 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

Oct 17 '06 #8
On Oct 15, 5:15 pm, "Daniel T." <danie...@earthlink.netwrote:
>"PengYu...@gmail.com" <PengYu...@gmail.comwrote:
>>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.
Oct 17 '06 #9
"Pe*******@gmail.com" <Pe*******@gmail.comwrote:
On Oct 15, 2:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>PengYu...@gmail.com 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.
Oct 17 '06 #10

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

Similar topics

6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
5
by: Neil Steventon | last post by:
Hi, I am new to VB.net , well the objects concept anyway and was wondering if there were any good articles / turorials on when and why you would use certain declarations such as Private test...
26
by: Zytan | last post by:
What happens if I do this: static byte MemberFunction() instead of: public static byte MemberFunction() I know I can't access it. But what does it default to? Private? I can't find any code...
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: 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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.