Exploring Virtual Functions with Default Arguments in C++
Introduction
In this code example, we will explore the concept of virtual functions with default arguments in C++. We will see how virtual functions can be overridden in derived classes and how default arguments work in this context.
Key Concepts
- Virtual functions: Virtual functions are functions that can be overridden in derived classes. They allow for dynamic binding, which means that the appropriate function is called based on the type of the object at runtime.
- Default arguments: Default arguments are values assigned to function parameters that are used when no argument is provided during a function call. They provide a default value for the parameter if no value is explicitly passed.
Code Structure
The code provided consists of two classes: base and derived. The base class contains a virtual function called display with a default argument of 0. The derived class inherits from the base class and overrides the display function.
Code Examples
Let's analyze the code step by step:
C++
#include<iostream> using namespace std; class base { public: virtual void display(int a=0) { cout<<"Base :"<<a<<endl; } }; class derived:public base { public: void display(int a) { cout<<"Derived :"<<a<<endl; } }; int main() { base b,*ptr; ptr=&b; derived d; ptr=&d; ptr->display(10); return 0; }
Code Output
Derived: 10
Code Explain
- We include the necessary header file iostream and use the std namespace for convenience.
- We define the base class with a virtual function display. The function takes an integer parameter a with a default value of 0. Inside the function, it prints "Base:" followed by the value of a.
- We define the derived class which inherits from the base class. It overrides the display function and prints "Derived:" followed by the value of a.
- In the main function, we create an object b of the base class and a pointer ptr of the type base. We assign the address of b to ptr.
- We create an object d of the derived class.
- We assign the address of d to ptr. This is allowed because derived is a subclass of base.
- We call the display function using the ptr pointer and pass the value 10 as an argument. Since the display function is virtual, the appropriate function is called based on the type of the object pointed to by ptr. In this case, the display function of the derived class is called, and it prints "Derived:10".
- Finally, we return 0 to indicate the successful execution of the program.
Conclusion
In this code example, we learned about virtual functions with default arguments in C++. We saw how virtual functions can be overridden in derived classes and how default arguments work in this context. By using virtual functions, we can achieve dynamic binding and ensure that the appropriate function is called based on the type of the object at runtime. Default arguments provide a convenient way to assign a default value to a function parameter if no value is explicitly passed during a function call.
Learn More...
- Multiple Inheritance in C++
- Binary + Operator Overloading In CPP
- Exploring Class-to-Basic Type Conversion with a Product Example in C++
- Creating a Diamond Pattern in CPP Program
- create a structure named complex having data member real and imag. Create member function add_complex which takes structure as an argument and return structure. Using function add two complex numbers.