call base class function or derived class function

Discussion in 'C Programming' started by George2, Mar 17, 2008.

  1. George2

    George2

    Joined:
    Feb 12, 2008
    Messages:
    108
    Hello everyone,


    I am just a little confusing and not 100% confident about the following case. In the following sample, function call foo (called in function goo in class Foo) will call foo in class Goo, other than foo in class Foo, because we are using an instance of Goo g and no matter in which class, right?

    (I have tested the result is correct, and I am asking the reason here.)

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Foo {
    public:
    	void goo()
    	{
    		foo(); // will call foo in class Goo
    	}
    private:
    	virtual void foo() = 0;
    };
    
    void Foo::foo()
    {
    	cout << "I am here. " << endl;
    }
    
    class Goo : public Foo {
    public:
    	void foo()
    	{
    		Foo::foo();
    	}
    };
    
    int main()
    {
    	Goo g;
    	g.foo();
    	return 0;
    }
    

    thanks in advance,
    George
    George2, Mar 17, 2008
    #1
    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. developereo@hotmail.com
    Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  2. developereo@hotmail.com
    Replies:
    1
    Views:
    386
    Victor Bazarov
    May 23, 2007
  3. Javier
    Replies:
    4
    Views:
    467
    James Kanze
    Jun 8, 2007
  4. pastbin@gmail.com
    Replies:
    2
    Views:
    706
    pastbin@gmail.com
    Feb 7, 2008
  5. prabhakar732

    how call Derived class function in base class

    prabhakar732, Dec 19, 2008, in forum: C Programming
    Replies:
    2
    Views:
    439
    prabhakar732
    Dec 22, 2008
Loading...

Share This Page