Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Default Arguments and Virtual Function in C++

  • Difficulty Level : Medium
  • Last Updated : 18 May, 2022

Default Arguments are the values provided during function declaration, such that values can be automatically assigned if no argument is passed to them. In case any value is passed the default value is overridden and it becomes a parameterized argument.

Virtual function is a member function that is declared within a base class and is redefined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. A virtual function also falls under runtime polymorphism.

To see the combined application of default arguments with virtual functions let’s take a sample

Example:

CPP




// C++ program to demonstrate how default arguments
// and virtual function are used together
#include <iostream>
using namespace std;
 
// Initialization of base class
class Base {
public:
    // Initialization of virtual function
    virtual void fun(int x = 0)
    {
        cout << "Base::fun(), x = " << x << endl;
    }
};
 
// Initialization of Derived class
class Derived : public Base {
public:
    // NOTE this virtual function will take an arument
    // But haven't initialized yet
    virtual void fun(int x)
    {
        cout << "Derived::fun(), x = " << x << endl;
    }
};
 
// Driver Code
int main()
{
    Derived d1; // Constructor
 
    // Base class pointer which will
    // Edit value in memory location of
    // Derived class constructor
    Base* bp = &d1;
   
    bp->fun(); // Calling a derived class member function
   
    return 0; // Returning 0 means the program
              // Exectued successfully
}

Output:

Derived::fun(), x = 0

If we take a closer look at the output, we observe that the ‘fun()‘ function of the derived class is called, and the default value of the base class ‘fun()‘ function is used. 

Default arguments do not participate in the signature( function’s name, type, and order) of functions. So signatures of the ‘fun()‘ function in the base class and derived class are considered the same, hence the ‘fun()‘ function of the base class is overridden. Also, the default value is used at compile time. When the compiler sees an argument missing in a function call, it substitutes the default value given. Therefore, in the above program, the value of x is substituted at compile-time, and at run-time derived class’s ‘fun()‘ function is called. 

Example:

CPP




// C++ program To demonstarte how default arguments
// and virtual function are used together
#include <iostream>
using namespace std;
 
class Base {
public:
    virtual void fun(int x = 0)
    {
        cout << "Base::fun(), x = " << x << endl;
    }
};
 
class Derived : public Base {
public:
    virtual void fun(int x = 10) // NOTE THIS CHANGE
    {
        cout << "Derived::fun(), x = " << x << endl;
    }
};
 
int main()
{
    Derived d1; // Constructor
 
    // Base class pointer which will
    // Edit value in memory location of
    // Derived class constructor
    Base* bp = &d1;
 
    bp->fun(); // Calling a derived class member function
 
    return 0; // Returning 0 means the program
              // Exectued successfully
}
Output
Derived::fun(), x = 0

The output of this program is the same as the previous program. The reason is the same, the default value is substituted at compile time. The fun() is called on ‘bp’ which is a pointer of Base type. So compiler substitutes 0 (not 10).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!