C++ virtual Keyword
The virtual keyword in C++ is used to enable polymorphism in object-oriented programming. When a member function in a base class is declared as virtual, it allows derived classes to override that function and ensures that the correct function is called for an object, regardless of the type of reference or pointer used.
The virtual keyword is essential for achieving dynamic dispatch, where the function to be called is determined at runtime rather than compile time.
Syntax
</>
Copy
class Base {
public:
virtual return_type function_name(parameters);
};
- virtual
- A keyword used to declare a function as virtual, enabling it to be overridden in derived classes.
- return_type
- The type of value the function returns (e.g.,
void,int). - function_name
- The name of the virtual function.
- parameters
- The list of arguments that the function takes.
Examples for using visual Keyword
Example 1: Basic Virtual Function
This example demonstrates the use of a virtual function in a base class and its override in a derived class.
</>
Copy
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function" << endl;
}
};
class Derived : public Base {
public:
void show() override { // Override the base class function
cout << "Derived class show function" << endl;
}
};
int main() {
Base* basePtr; // Pointer to base class
Derived derivedObj; // Derived class object
basePtr = &derivedObj; // Assign derived class object to base pointer
basePtr->show(); // Calls the derived class function due to virtual
return 0;
}
Output:
Derived class show function
Explanation:
- The
showfunction in the base class is declared asvirtual. - The derived class overrides the
showfunction using theoverridespecifier for clarity. - In the
mainfunction, a pointer to the base class is used to reference an object of the derived class. - When
basePtr->show()is called, the derived class’sshowfunction is invoked, demonstrating polymorphism.
Example 2: Virtual Function in a Hierarchy
This example demonstrates how virtual functions work in a class hierarchy with multiple derived classes.
</>
Copy
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function" << endl;
}
};
class Derived1 : public Base {
public:
void show() override {
cout << "Derived1 class show function" << endl;
}
};
class Derived2 : public Base {
public:
void show() override {
cout << "Derived2 class show function" << endl;
}
};
int main() {
Base* basePtr;
Derived1 d1;
Derived2 d2;
basePtr = &d1;
basePtr->show(); // Calls Derived1's show function
basePtr = &d2;
basePtr->show(); // Calls Derived2's show function
return 0;
}
Output:
Derived1 class show function
Derived2 class show function
Explanation:
- The base class
showfunction is declared asvirtual. - Both
Derived1andDerived2override theshowfunction. - In the
mainfunction, the base class pointerbasePtris assigned objects of bothDerived1andDerived2in sequence. - The correct
showfunction is invoked dynamically based on the type of object being pointed to, demonstrating polymorphism.
Key Points to Remember about the Virtual Keyword
- The
virtualkeyword enables runtime polymorphism by allowing functions to be overridden in derived classes. - When a virtual function is called through a base class pointer or reference, the derived class’s version of the function is executed if it exists.
- Declaring a function as virtual ensures dynamic dispatch, meaning the function call is resolved at runtime.
- To prevent further overrides, you can declare a function as
finalin a derived class. - If a base class declares a virtual function, but no override is provided in the derived class, the base class version is used by default.
