public base class

Discussion in 'C++' started by ET2 Ng, Jul 19, 2003.

  1. ET2 Ng

    ET2 Ng Guest

    Hi all,

    If I recall correctly, the functions inherited from a base class are
    private in the derived class.

    I want to expose some of them (but not all of them) directly through my
    derived class. In other words, I'm trying to avoid something like:

    void PublicFunction_DerrivedClass()
    {
    PublicFunction_BaseClass();
    }

    I would appreciate any help.
    --

    Best wishes,
    Allen

    No SPAM in my email !!
    ET2 Ng, Jul 19, 2003
    #1
    1. Advertising

  2. ET2 Ng wrote:

    > Hi all,
    >
    > If I recall correctly, the functions inherited from a base class are
    > private in the derived class.
    >
    > I want to expose some of them (but not all of them) directly through
    > my
    > derived class. In other words, I'm trying to avoid something like:
    >
    > void PublicFunction_DerrivedClass()
    > {
    > PublicFunction_BaseClass();
    > }
    >
    > I would appreciate any help.


    That all depends on what kind of inheritance you use. The default for
    classes is private inheritance, and the default for structs is public
    inheritance.
    To use public inheritance for classes, do something like this:

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

    int main(){
    B b;
    b.foo();
    }

    --
    Where am I going, and what am I doing in this handbasket?
    Jacques Labuschagne, Jul 19, 2003
    #2
    1. Advertising

  3. ET2 Ng

    ET2 Ng Guest

    Hi John, Jacques, all,

    Aha! Exactly what I was looking for! Thanks a bunch; I knew there had
    to be a way to do that!
    --

    Best wishes,
    Allen

    No SPAM in my email !!

    "John Carson" <> wrote in message
    news:...
    > "ET2 Ng" <-haa.net> wrote in message
    > news:NNiSa.63251$
    > > Hi all,
    > >
    > > If I recall correctly, the functions inherited from a base class
    > > are private in the derived class.
    > >

    >
    > As Jacques says, this depends on the type of inheritance. Private
    > inheritance is the default for classes but you can specify public or
    > protected.
    >
    > > I want to expose some of them (but not all of them) directly
    > > through my derived class. In other words, I'm trying to avoid
    > > something like:
    > >
    > > void PublicFunction_DerrivedClass()
    > > {
    > > PublicFunction_BaseClass();
    > > }
    > >
    > > I would appreciate any help.
    > > --
    > >
    > > Best wishes,
    > > Allen
    > >
    > > No SPAM in my email !!

    >
    > Rather than redefine functions, you can utilise "using" declarations. Use
    > private inheritance and then make a using declaration for every function
    > that you want to bring from private access to public access, e.g.,
    >
    > class Base
    > {
    > public:
    > void print5()
    > {
    > std::cout << 5 << '\n';
    > }
    > void print10()
    > {
    > std::cout << 10 << '\n';
    > }
    > };
    >
    > // private inheritance so print5 and print1 are private
    > class Derived : Base
    > {
    > public:
    > using Base::print5; // make print5 public
    > };
    >
    >
    > int main()
    > {
    > Derived d;
    > d.print5(); // OK
    > d.print10(); // error
    > return 0;
    > }
    >
    >
    > --
    > John Carson
    > 1. To reply to email address, remove donald
    > 2. Don't reply to email address (post here instead)
    >
    ET2 Ng, Jul 20, 2003
    #3
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,344
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    554
    SpamProof
    Oct 21, 2003
  3. Banaticus  Bart
    Replies:
    9
    Views:
    614
    Gary Labowitz
    May 7, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  5. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
Loading...

Share This Page