Default Arguments and Virtual Function in C++ - GeeksforGeeks

08 Dec, 2021
Let’s first understand what Default arguments and Virtual functions mean.
Default Arguments: These 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.
Virtual Function: A 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. 
To see the combined application of default arguments with virtual functions lets first see the following C++ program,
CPP
// CPP program to demonstrate 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)    {        cout << "Derived::fun(), x = " << x << endl;    }};  // Driver Codeint main(){    Derived d1;    Base* bp = &d1;    bp->fun();    return 0;}
Derived::fun(), x = 0
If we take a closer look at the output, we observe that fun() of the derived class is called and the default value of base class fun() is used. Default arguments do not participate in the signature of functions. So signatures of fun() in the base class and derived class are considered the same, hence the fun() of the base class is overridden. Also, the default value is used at compile time. When the compiler sees that an argument is 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() is called. 
Now predict the output of the following program,
CPP
// CPP program to demonstrate 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;    Base* bp = &d1;    bp->fun();    return 0;}
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).
anshikajain26
CPP-Functions
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Sorting a vector in C++
Friend class and function in C++
Polymorphism in C++
List in C++ Standard Template Library (STL)
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
new and delete operators in C++ for dynamic memory
Destructors in C++
Queue in C++ Standard Template Library (STL)