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

P

PengYu.UT

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

Victor Bazarov

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
 
I

iracly.kv

Just try:
class B
{
public:

private int A::a1();
}

AFAIK - this is supported by C++ standard
 
D

Daniel T.

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.
 
S

Salt_Peter

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.
 
P

PengYu.UT

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.
 
D

Daniel T.

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top