call base class function or derived class function

Joined
Feb 12, 2008
Messages
108
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top