call base class function or derived class function

This is a discussion on call base class function or derived class function within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am just a little confusing and not 100% confident about the following case. In the following sample, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    call base class function or derived class function

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The reason is that foo is a virtual function. When a virtual function is called, the dynamic type of the object is used to determine which version of that function is executed. In this case, the type of the object is a Goo, so the Goo's version of the function is called.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Daved,


    Question answered. What makes me confused is the "pure" virtual. :-)

    Quote Originally Posted by Daved View Post
    The reason is that foo is a virtual function. When a virtual function is called, the dynamic type of the object is used to determine which version of that function is executed. In this case, the type of the object is a Goo, so the Goo's version of the function is called.

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,024
    Pure virtual is just another virtual function which makes the class abstract, thus requiring the derived to override it or it will become abstract too. Aside from that, it behaves just like any other virtual function.
    But you should know this, shouldn't you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I understand it now, thanks Elysia!


    Quote Originally Posted by Elysia View Post
    Pure virtual is just another virtual function which makes the class abstract, thus requiring the derived to override it or it will become abstract too. Aside from that, it behaves just like any other virtual function.
    But you should know this, shouldn't you?

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  4. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  5. base and derived class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21