accessing parent class method

Discussion in 'C++' started by ArbolOne, Jun 1, 2008.

  1. ArbolOne

    ArbolOne Guest

    In my program I am using two classes foo1, the parent class, and foo2
    the child class!
    foo2 has overloaded inserter and extractor operator, but so does foo1!
    How can I access foo1 inserter or extractor?


    Thanks
     
    ArbolOne, Jun 1, 2008
    #1
    1. Advertising

  2. On 2008-06-01 03:07, Victor Bazarov wrote:
    > ArbolOne wrote:
    >> In my program I am using two classes foo1, the parent class, and foo2
    >> the child class!
    >> foo2 has overloaded inserter and extractor operator, but so does foo1!
    >> How can I access foo1 inserter or extractor?

    >
    > Post your code. It is easier when there is substance to the discussion.
    >
    > Meanwhile, here is the example:
    >
    > struct foo {
    > void foobar();
    > };
    >
    > struct bar : foo {
    > void foobar();
    > };
    >
    > int main() {
    > bar b;
    > b.foobar(); // calls bar::foobar
    > b.foo::foobar(); // call foo::foobar
    > }


    I would call it bad style to explicitly call a parent's implemenetation
    of a function (and bad design if you have to), unless it is done in the
    child's implementation of the same function:

    #include<iostream>

    struct foo1 {
    virtual void foobar() {
    std::cout << "foo1\n";
    }
    };

    struct foo2 : public foo1 {
    virtual void foobar() {
    std::cout << "foo2\n";
    foo1::foobar(); // Call foo1's foobar()
    }
    };

    int main() {
    foo1* f = new foo1();
    f->foobar();
    delete f;
    f = new foo2();
    f->foobar();
    }

    --
    Erik Wikström
     
    Erik Wikström, Jun 1, 2008
    #2
    1. Advertising

  3. ArbolOne

    ArbolOne Guest

    On Jun 1, 3:57 am, Erik Wikström <> wrote:
    > On 2008-06-01 03:07, Victor Bazarov wrote:
    >
    >
    >
    > > ArbolOne wrote:
    > >> In my program I am using two classes foo1, the parent class, and foo2
    > >> the child class!
    > >> foo2 has overloaded inserter and extractor operator, but so does foo1!
    > >> How can I access foo1 inserter or extractor?

    >
    > > Post your code. It is easier when there is substance to the discussion.

    >
    > > Meanwhile, here is the example:

    >
    > > struct foo {
    > > void foobar();
    > > };

    >
    > > struct bar : foo {
    > > void foobar();
    > > };

    >
    > > int main() {
    > > bar b;
    > > b.foobar(); // calls bar::foobar
    > > b.foo::foobar(); // call foo::foobar
    > > }

    >
    > I would call it bad style to explicitly call a parent's implemenetation
    > of a function (and bad design if you have to), unless it is done in the
    > child's implementation of the same function:
    >
    > #include<iostream>
    >
    > struct foo1 {
    > virtual void foobar() {
    > std::cout << "foo1\n";
    > }
    >
    > };
    >
    > struct foo2 : public foo1 {
    > virtual void foobar() {
    > std::cout << "foo2\n";
    > foo1::foobar(); // Call foo1's foobar()
    > }
    >
    > };
    >
    > int main() {
    > foo1* f = new foo1();
    > f->foobar();
    > delete f;
    > f = new foo2();
    > f->foobar();
    >
    > }
    >
    > --
    > Erik Wikström

    I got it now!
    Thanks.
     
    ArbolOne, Jun 1, 2008
    #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.

Share This Page