calling base class method from derive class object without virtual

Discussion in 'C++' started by Tinku, Jan 31, 2010.

  1. Tinku

    Tinku Guest

    Hi

    #include<iostream>


    class base
    {
    public:
    int get();
    int put();
    };
    class Derive: public base
    {
    public:
    int get();
    int put();
    };

    int base::get()
    {
    int x = 5;
    return x;
    }
    int base::put()
    {
    int y = 10;
    return y;
    }
    int Derive::get()

    {
    int a = 9;
    return a;
    }
    int Derive::put()
    {
    int b = 20;
    return b;
    }

    int main()
    {
    Derive d;
    d.get(); // this line will return 9 according to class how can I
    get 5 from here (base class method get();)

    return 0;
    }

    How can I call base class method from derive class object without
    using virtual.
    Tinku, Jan 31, 2010
    #1
    1. Advertising

  2. Tinku

    Robert Fendt Guest

    Re: calling base class method from derive class object withoutvirtual

    And thus spake Tinku <>
    Sun, 31 Jan 2010 00:39:53 -0800 (PST):

    > int main()
    > {
    > Derive d;
    > d.get(); // this line will return 9 according to class how can I
    > get 5 from here (base class method get();)
    >
    > return 0;
    > }
    >
    > How can I call base class method from derive class object without
    > using virtual.


    Actually, virtual functions and polymorphy work precisely the other way round.

    To answer your question, you can do two things: (1) specify the function by "d.Base::get()" (that _should_ work, though I hardly use that syntax); (2) get a pointer to 'd', cast that to a base pointer, and then call get() on it.

    Regards,
    Robert
    Robert Fendt, Jan 31, 2010
    #2
    1. Advertising

  3. Re: calling base class method from derive class object withoutvirtual

    On Jan 31, 11:39 am, Tinku <> wrote:
    > Hi
    >
    > #include<iostream>
    >
    > class base
    > {
    > public:
    >         int get();
    >         int put();};
    >
    > class Derive: public base
    > {
    > public:
    >         int get();
    >         int put();
    >
    > };
    >
    > int base::get()
    > {
    >         int x = 5;
    >         return x;}
    >
    > int base::put()
    > {
    >         int y = 10;
    >         return y;}
    >
    > int Derive::get()
    >
    > {
    >         int a = 9;
    > return a;}
    >
    > int Derive::put()
    > {
    >         int b = 20;
    >         return b;
    >
    > }
    >
    > int main()
    > {
    >    Derive d;
    >    d.get(); // this line will return 9 according to class how can I
    > get 5 from here (base class method get();)
    >
    >    return 0;
    >
    > }
    >
    > How can I call base class method from derive class object without
    > using virtual.


    Actually, virtual functions are used to enable polymorphism and
    calling
    member functions of derived class object via base class objects
    (pointers and references). If you want to call base class member
    functions
    through derived class object use something like this:

    class Base {
    public:
    int get() { cout << "I am in base\n"; return 9; }
    };

    class Derived : public Base {
    public:
    int get() { return Base::get(); } // call base class members
    };

    int main()
    {
    Derived d;
    d.get();
    return 0;
    }
    Saeed Amrollahi, Jan 31, 2010
    #3
  4. * Tinku:
    >
    > int main()
    > {
    > Derive d;
    > d.get(); // this line will return 9 according to class how can I
    > get 5 from here (base class method get();)
    >
    > return 0;
    > }
    >
    > How can I call base class method from derive class object


    d.base::get();


    > without using virtual.


    I think you may have misunderstood 'virtual'; it sort of goes the other way.


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Jan 31, 2010
    #4
    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. tomek
    Replies:
    2
    Views:
    664
    Andrey Tarasevich
    Dec 1, 2003
  2. Andreas Lagemann
    Replies:
    8
    Views:
    464
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    428
  4. gyan
    Replies:
    3
    Views:
    353
    Ian Collins
    Aug 23, 2006
  5. Raider
    Replies:
    2
    Views:
    394
    Ivan Vecerina
    Sep 20, 2006
Loading...

Share This Page