Jigar Patel
Jigar Patel

Reputation: 65

Virtual function in Inheritance in C++

**Read the below mention code **

#include<iostream>
using namespace std;

class FR
{
public :
    virtual int fun() 
    {
      cout<<"In FR Class "<<endl;
    }

    int Threading()
    {
        fun();
    }
};

class FD : public FR
{
public :
    int fun()
    {
        cout<<" In FD class "<<endl;
    }
};

int main()
{
    FD obj ;

    obj.Threading();
}

Here,output of this is "in FD class".

here we have inheritance the FR class into FD class,so all property from the FR class is inheritade from the based class, but when we call the Threading() function using the FD object than it will called the based class but for Threading class Fun() function is available as virtual and Fun definition is also available so why it not classed the based class Fun() function.

I'm expecting that when we called the Threading() function than it should be called the class FR Fun() function not class FD Fun().

Actual OutPut : "In FD Class"

Expected OutPut : "In FR Class"

Upvotes: 0

Views: 149

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

Virtual functions are called through the table of virtual function pointers.

The derived class FD overwrites the address of the virtual function fun with the address of its own overriding function in its tangle of virtual function pointers.

So in this sequence of calls

obj.Threading();

and

int Threading()
{
    fun();
}

to call the function fun the pointer to the function fun is searched in the table of virtual function pointers of the class FD and correspondingly the function definition provided in the class FD is used. then the o

Upvotes: 3

44stonelions
44stonelions

Reputation: 328

When you declare a method as virtual, a vtable will be generated for that class (if it is concrete, i.e. has no pure virtual members) that contains pointers to each of the virtual functions in the class. A vtable will also be generated for every class that derives from the class with the virtual method (as long as they are also concrete).

Each object of the class then gets a vtable pointer that points to their particular vtable. Your class FR has a pointer to the vtable containing FR::fun, whereas your class FD has a pointer to the vtable containing FD::fun.

Since Threading is non-virtual, when Threading is called on an object of class FD, the method implemented in FR is called with a this pointer to the FD object. Then, when Threading calls this->fun it looks for the function in the FD's vtable and calls FD::fun.

Upvotes: 1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122460

This is known as virtual dispatch and is basically just what virtual means not more. The method to be called is determined by the dynamic type of the object.

You can think of the call as having a this->:

class FR
{
public :
    virtual int fun() 
    {
      cout<<"In FR Class "<<endl;
    }

    int Threading()
    {
        this->fun();
    }
};

When Threading is called on an object of type FD then in above code this is a FR* but the dynamic type of the object to which this points is FD. Hence when the virtual fun() is called it calls FD::fun().

If you do not want virtual dispatch for fun remove the virtual. Then your code will print "In FR Class".

Upvotes: 3

Related Questions