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

Discussion in 'C++' started by PengYu.UT@gmail.com, Oct 15, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Oct 15, 2006
    #2
    1. Advertising

  3. Guest

    Just try:
    class B
    {
    public:

    private int A::a1();
    }

    AFAIK - this is supported by C++ standard
     
    , Oct 15, 2006
    #3
  4. Daniel T. Guest

    "" <> 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.
     
    Daniel T., Oct 15, 2006
    #4
  5. Salt_Peter Guest

    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.
     
    Salt_Peter, Oct 16, 2006
    #5
  6. red floyd Guest

    Re: How to make the public inherented member function private orprotected?

    wrote:
    > Just try:
    > class B
    > {
    > public:
    >
    > private int A::a1();
    > }
    >
    > AFAIK - this is supported by C++ standard
    >


    It's not.
     
    red floyd, Oct 16, 2006
    #6
  7. Guest

    On Oct 15, 5:15 pm, "Daniel T." <> wrote:
    > "" <> 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.
     
    , Oct 17, 2006
    #7
  8. Guest

    On Oct 15, 2:49 pm, "Victor Bazarov" <> wrote:
    > 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, 2006
    #8
  9. Daniel T. Guest

    > On Oct 15, 5:15 pm, "Daniel T." <> wrote:
    >> "" <> 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.
     
    Daniel T., Oct 17, 2006
    #9
  10. Daniel T. Guest

    "" <> wrote:
    > On Oct 15, 2:49 pm, "Victor Bazarov" <> wrote:
    >> 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.
     
    Daniel T., Oct 17, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper

    Public, Private, Protected? Thanks.

    Shapper, May 31, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    20,948
    clintonG
    May 31, 2005
  2. bigbinc
    Replies:
    7
    Views:
    38,016
    Dale King
    Sep 22, 2003
  3. DaveLessnau
    Replies:
    3
    Views:
    434
    Howard
    May 16, 2005
  4. Replies:
    2
    Views:
    825
    Noah Roberts
    Jun 9, 2006
  5. Peng Yu
    Replies:
    3
    Views:
    1,094
    Simon Forman
    Sep 21, 2009
Loading...

Share This Page