
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Default Arguments and Virtual Function in C++
Default Argument
A default argument is a value provided during the function declaration that can be automatically assigned if no argument is provided when the function is called. If a value is passed at the time of the function call, this default value is overridden, and the argument becomes a parametrized Argument.
Syntax
The syntax below shows how we can define a default argument function:
int fun(int x = 0){ // function body }
Example of Default Argument
The following C++ example shows the working of the default argument:
#include<iostream> using namespace std; //function defined with default arguments int sum(int x = 5, int y = 10) { return (x + y); } int main() { // calling function with default value cout << sum() << endl; // calling function with overridden value cout << sum(10, 12) << endl; return 0; }
Following is the output of the above code:
15 22
Virtual Functions
A virtual function is the member function that is declared in the base class using the keyword virtual and is overridden in the derived class.
Characteristics of Virtual Function
Following are the characteristics of the virtual function:
- Virtual function make sure the correct function is called for an object, regardless of the type of the reference (or pointer) for the function call.
- They are mainly used to achieve runtime polymorphism and resolve the function call at runtime.
Syntax
Following is the syntax of the virtual function:
class Base{ public: virtual return_type functionName(){ .... } } class Derived : public Base{ public: // Overriden virtual function // of the base class return_type functionName(){ ... } }
Example of Virtual Function
In the following example, we are counting the area of the shape using the virtual function in C++:
#include<iostream> using namespace std; class shape { public: virtual void area(int size = 0) { cout << "Area of a shape!" << endl; } }; class circle: public shape { public: void area(int r) override { double areaaa = 3.14 * r * r; cout << "Area of a Circle: " << areaaa << endl; } }; class square: public shape { public: void area(int s) override { int areaaa = s * s; cout << "Area of a Square: " << areaaa << endl; } }; int main() { shape * shapePtr; circle cir; square sqr; // Pointing to circle object shapePtr = & cir; shapePtr -> area(5); // Pointing to square object shapePtr = & sqr; shapePtr -> area(4); return 0; }
Area of the above shape:
Area of a Circle: 78.5 Area of a Square: 16
Implementation of Default Argument and Virtual Function
C++ program to demonstrate how default arguments and virtual function are used together:
#include <iostream> using namespace std; class Base { public: // declaratin of virtual function virtual void fun(int n = 5) { cout << "Base::fun(), n = " << n << endl; } }; class Derived: public Base { public: void fun(int n) { cout << "Derived::fun(), n = " << n << endl; } }; int main() { Derived d1; Base * basePointer = &d1; // Calling a derived class member function basePointer -> fun(); return 0; }
Following is the outpu of the above code:
Derived::fun(), n = 5