Home Resource Centre Virtual Function In C++ | Syntax, Rules, Working & More (+ Examples)

Virtual Function In C++ | Syntax, Rules, Working & More (+ Examples)

Virtual functions in C++ are special methods that have inheritance and overriding properties. By including virtual functions in base classes and overriding them in derived classes, we add flexibility to the code. They also help perceive objects from several derived classes as belonging to the same base class, enabling polymorphism.

In this article, we will explore the concept of virtual function in C++ and how it is essential for code efficiency and reusability.

What Is A Virtual Function In C++?

A virtual function in C++ language is a base class member function that can be overridden in the derived class. They facilitate dynamic dispatch, which selects the optimum function implementation based on the object's runtime type. 

  • In addition to supporting runtime polymorphism and dynamic dispatch, virtual function in C++ enhance overall code efficiency and streamline development.
  • They enable objects of multiple derived classes to be treated consistently through a single interface provided by the base class.
  • For this, we use the virtual keyword when defining the function in the parent class and the override keyword in the derived class.
  • Data retrieval and processing are also made simpler by the usage of virtual functions in the C++ Standard Library's find() function, among other functions.
  • A virtual function in C++ can be created as a friend function of some other class.

To further understand virtual functions, let's examine a real-world example. Consider a class hierarchy with the classes Rectangle and Circle derived from the base class Shape.

  • Say the base class Shape includes a virtual method calculateArea(), which calculates the area of the shape object.
  • Each derived class can give its own implementation of the calculateArea() method, in line with its own calculation logic.
  • As we are not aware of the exact kinds of objects in a vector of Shape objects, we call the calculateArea() function on each object.
  • The proper version of the function will be called at runtime based on the actual object type, ensuring accurate area calculations for rectangles and circles.

Syntax For Virtual Function In C++

Below is the syntax for declaring a virtual function in C++, followed by an explanation of its components. 

class BaseClass {
public:
virtual returnType functionName(parameters) {
// Function implementation
}
};

Here,

  • The BaseClass refers to the name of the class, which consists of the virtual function.
  • The virtual keyword indicates that the function being defined/declared is virtual (to be overridden).
  • functionName refers to the name of the function we are defining, and the curly brackets contain the function body.
  • The returnType reflects the data type of the function's return value, and parameters refer to the input variables/ arguments that the function takes.

Rules To Define Virtual Function In C++

One must consider a few guidelines when working with the virtual function in C++. They are:

  • The base class declares virtual functions: When declaring virtual functions in the base class, the keyword virtual must occur before the return type. This inidcates that derived classes can override the function. For example:

virtual returnType functionName(parameters);

  • Virtual functions can be overridden in derived classes: Derived classes must offer their own implementation of a virtual function specified in the base class. However, to override a virtual function, the signature of the functions must be the same, as specified in the next point.
  • Function signature must match exactly: The function signature of the overriding function in the derived class must be identical to that of the virtual function in the base class.
    • The overriding would not be valid if the return types or parameter values used are of different types.
    • That is, the overriding function of the derived class must have the exact same function signature as the virtual function of the base class, including name, return type, and parameters.
  • Use of override keyword: You must use the override keyword when declaring the overriding function in derived class. It helps to make the code more legible by offering a compiler check to ensure that the method being overridden truly exists in the base class. For example:

override returnType functionName(parameters);

  • Function Call/ Virtual functions can be called through base class pointers or references: Virtual functions are normally called using a pointer or reference from the base class (i.e., base pointer), regardless of whether the object being referenced is a member of a derived class (i.e., object of type derived).
    • The function is called using the pointer to or reference of base class object (or base pointer) and the function name.
    • The appropriate derived class implementation is invoked at runtime based on the actual object type.
    • The proper function implementation may then be selected depending on the actual object type at the moment of usage in a process known as dynamic dispatch.
  • Virtual functions can have a default implementation: A virtual function in C++ base class offers a default implementation, which can be modified as needed by derived classes. This default implementation might be useful when particular derived classes don't offer a specialized implementation.
  • Pure virtual functions: Pure virtual functions are virtual functions that are specified in the base class without any implementation. To declare these functions, we must end the function definition syntax with an equal to 0 (i.e., = 0). For example:

virtual returnType functionName (parameters) = 0 

These rules regulate the use and behaviour of the virtual function in C++ and provide a means of implementing runtime polymorphism and dynamic dispatch. They enable code reuse and extension, which makes object-oriented programming adaptable and efficient.

Implementation Of Virtual Function In C++

As mentioned before, a virtual function in C++ programming is declared in the base class and can be overridden by derived classes. This makes it feasible to treat objects from different derived classes equally by using a base class-provided common interface thus enducing polymorphism and dynamic dispatch. 

All in all, it encourages code reuse, extensibility, and flexibility in object-oriented programming. Let's look at a C++ code example showcasing the virtual function's implementation in C++ programs.

Code Example:

Output:

Cat meows.
Dog barks.

Explanation:

We begin the C++ program example by including the <iostream> header to use cout and cin commands. 

  1. Then, we create three classes, i.e., Animal, Cat, and Dog.
  2. The Animal class is the base class here, which has a virtual method called makeSound.
  3. The function prints the string message- 'Animal makes a sound.' using court. And the newline escape sequence shifts the cursor to the next line.
  4. Next, we define the classes Cat and Dog, which are derived from the base class Animal.
  5. We also add the proper sound implementation in place of each derived class's makeSound method. These functions are defined to override the function from the base class.
  6. In the main() function, we declare a pointer variable to objects of the Animal class called animalPtr. We will use this pointer of type Animal to hold the positions of objects from the derived classes.
  7. After that, we create objects of classes Cat and Dog, names cat and dog, respectively.
  8. Next, we assign the location of the cat object to the pointer using the reference operator, i.e., &cat.
  9. Then, we call the makeSound() function using the 'this' pointer and the pointer to object animal (which currently holds the address of the cat object).
  10. As mentioned in the code comment, this invokes the overriding makeSound function from the Cat class and outputs Cat meows.
  11. Following this, we assign the address of the object dog to the animalPtr pointer and call the makeSound() function, just like before.
  12. This time, the overriding makeSound function in the Dog class is invoked, outputting Dog barks.

In this illustration, the virtual function makeSound enables objects from various derived classes to be handled consistently using a standard interface that the base class provides. It demonstrates polymorphism and dynamic dispatch when the appropriate function implementation is decided upon at runtime based on the actual object type. That is-

  • If the object is of the base class type, the function from the base class (parent class) is invoked.
  • If the object belongs to the child class or derived class, the overriding function from the respective child class is invoked.

Working Of Virtual Function In C++

The concept of virtual function in C++ may seem complex. Below is a detailed and easy explanation of their working mechanism along with the concepts of VTABLE and VPTR. This will help you understand the underlying concept and make more efficient use of virtual functions in C++ programs.

  • The virtual keyword is used in the base class to specify the virtual function in C++, indicating that the derived classes can override it.
  • When a class has a virtual function, a VTABLE (or virtual function table) is automatically produced. Also referred to as a lookup table, the VTABLE stores the addresses of the virtual functions for that class.
  • Each object of a class having virtual functions contains a hidden pointer known as the VPTR (or virtual pointer). The VPTR denotes the class-related VTABLE.
  • The VTABLE is created for each class, not per object. It is shared by all objects of the same class and any classes that are derived from it.
  • The VTABLE contains entries for every virtual function specified in the base class as well as its overridden versions in the derived classes. For each entry in the VTABLE, the address of the matching function is saved.
  • When a virtual function in C++ is executed through a base class pointer or reference, the object's VPTR is utilized to locate the VTABLE connected to the object's class.
  • The compiler determines the VTABLE offset for the virtual function being called based on the function call. This offset helps find the relevant entry in the VTABLE.
  • The VTABLE entry includes the address of the virtual function, and the function call is directed to the appropriate override version based on the actual object type at runtime.

Below is a C++ program example that shows the working mechanism of a virtual function in C++ (such as the VTABLE and VPTR).

Code Example:

Output:

Base virtual function.
Derived virtual function.

Explanation:

  1. In this code example, we have created two classes, i.e., a base class called Base and a derived class called Dervied
  2. The Base class has a virtual function called virtualFunction, which prints a string message using the cout command.
  3. The Derived class also contains its own implementation of the virutalFunction() function, which overrides the virtual method from the base class.
  4. In the main() function, we first create a pointer to objects of the Base class, basePtr. This is used for both base and derived class objects.
  5. Then we create a Base class object, baseObj, and an object of Derived class, derivedObj
  6. The objects baseObj and derivedObj are each identified with these properties after being created with their own VPTRs pointing to the VTABLEs associated with their respective classes.
  7. We then call the virtual function using the 'this' pointer, i.e., basePtr->. The VPTR of the object is used to locate the VTABLE, and the proper version of the function is called depending on the actual object type.
  8. The VPTR identifies the base class VTABLE in the initial call, and the base class virtual function is executed, which prints 'Base virtual function' using std::cout.
  9. The second call invokes the overridden virtual function of the derived class's VTABLE, outputting 'Derived virtual function'.

This shows how dynamic dispatch and polymorphism are made possible by the virtual function in C++, the VTABLE and VPTR mechanisms, and other features.

Check out this amazing course and participate in a coding sprint to supercharge your C++ programming skills!

Compile-Time Polymorphism Vs. Run-Time Polymorphism

In C++, there are two types of polymorphisms, i.e., compile-time polymorphism and run-time polymorphism. The table below compares the two. 

Feature Compile-Time Polymorphism Run-Time Polymorphism
Definition Method or operator overloading resolved during compilation. Method overriding resolved during runtime.
Also Known As Static Polymorphism Dynamic Polymorphism
Binding Time Early binding (at compile time) Late binding (at runtime)
Performance Faster, as binding happens at compile time. Slower due to binding happening at runtime.
Flexibility Less flexible; method behavior is fixed at compile time. More flexible; method behavior can change at runtime.
Examples Function Overloading, Operator Overloading, Template Specialization Virtual functions in C++, method overriding in Java
Inheritance Requirement Not required; works with functions of the same name. Requires inheritance; typically involves a base and derived class.
Compile-Time Check Errors are caught at compile time. Errors might only be detected at runtime.
Usage When we need the same function name for different operations. When we need polymorphic behavior depending on the object's actual type.

Note that run-time polymorphism is resolved at runtime based on the object's dynamic type. Compile-time polymorphism is handled by the compiler during compilation based on the object's or arguments' static type.

Also, run-time polymorphism provides versatility through inheritance and virtual function in C++. But compile-time polymorphism provides efficiency through function overloading and operator overloading. Both forms of polymorphism are helpful in C++ programming and serve different tasks depending on the demands of the application.

Pure Virtual Function In C++

A pure virtual function in C++ is when the virtual mechanism (or virtual function) is defined in a base class, but the function does not have any implementation. Another term for it is an abstract function. The primary purpose of a pure virtual function is to define an interface in the base class that all derived classes must implement.

Pure Virtual Function In C++ Syntax:

virtual return_type function_name(parameters) = 0;

Here,

  • The virtual keyword signifies a virtual function, and function_name is its name. 
  • Parameters refer to the input it takes, and return_type refers to the data type of return value. 
  • The equal to zero (= 0) signifies that the function has no implementation, i.e., it is a pure virtual function. It signifies that derived classes must override the function because the base class does not implement it.

Code Example:

Output:

Drawing a circle.
Drawing a rectangle.

Code Explanation:

In this C++ code example, we first create a base class, Shape.

  1. This class declares a pure virtual method draw() without providing any implementation. As a result, the Shape class may be referred to as the abstract class.
  2. We then create two derived classes, Circle and Rectangle. They each have their own implementation of the draw() method since they are derived from the Shape class. These functions are defined using the override keyword.
  3. In the main() function, we create objects of classes, Circle and Rectangle, called circle and rectangle, respectively.
  4. Next, we call the draw() function for both these objects individually.
    • When the draw() function is triggered for the circle object, the overridden draw() function of the Circle class is called. This prints 'Drawing a circle' on the console using std::cout.
    • Similarly, when the draw() function is triggered using the rectangle object, the overridden draw() of the Rectangle class is called, outputting 'Drawing a rectangle'.
  5. This shows how we may specify a common interface in the base class utilizing pure virtual functions and leave the implementation details to the child classes.

It ensures the implementation of the function in derived classes and supports polymorphism by allowing objects from multiple derived classes to be treated uniformly through a single base class interface.

Also read: Pure Virtual Function In C++ & Abstract Classes ( With Examples)

Override Identifier & Virtual Function In C++

The override identifier is a contextual term that gives explicit information and improves code readability when overriding virtual functions in C++. It is used in the derived class to indicate that a function is intended to replace a virtual function from the base class.

In C++, the override identifier is added to the derived class after the function declaration.

Syntax Of Identifier Override In C++:

return_type function_name() override;

Here,

  • The override keyword acts as a safeguard to guarantee that the function is, in fact, overriding a virtual function in the base class. This is essential because compilation errors will occur if the derived class does not override any virtual function in C++.
  • The terms return_type and function_name refer to the data type of return value and the name of the virtual function.

Code Example:

Output:

Base class
Derived class

Code Explanation:

In this example, we create a base class called Base which has a virtual function called print(). The function prints a phrase using std::cout.

  1. Then, we create a derived class called Derived, which overrides the print() function.
  2. By adding the override identifier after the function declaration in Dervied, we explicitly specify that the print() method is meant to override the virtual function from the base class in the derived class.
  3. In the main() method, we create objects of the base and derived classes, called baseObj and derivedObj, respectively.
  4. We then call the print() function for both objects.
    • When the print() method is used on the baseObj, the virtual function in the base class is executed, printing 'Base class'.
    • When we use the print() function on the derivedObj, we get the overridden function in the derived class, which prints 'Derived class'.
  5. Finally, the program completes execution with the return 0 statement. 

Note: The const keyword (constant) in member function declarations ensures const-correctness, indicating that the function does not modify the object's state.

How To Use C++ Override Identifier?

As mentioned before, the override identifier is used explicitly when a function in a derived class is meant to override a virtual function in C++ from its base class. Below is a description of how to use the override identifier.

  • Ensuring Correct Function Overriding: When a derived class plans to override a virtual function from a base class, using the override identifier sends a clear signal to the compiler. It also aids in identification of possible mistakes when the function name or signature in the derived class differs from that in the base class.
  • Compiler Verification: When the override identifier is used, the compiler examines if the function declared in the derived class is indeed overriding a virtual function in the base class.
    • It provides compile-time safety by allowing the compiler to certify that the function being overridden is present in the base class.
    • If the virtual function in the base class does not exist or if the function in the derived class does not have the same name and signature, the compiler generates an error.
  • Enhancing Code Readability: The override identifier also improves code clarity by indicating that a function is intended to override a virtual function in C++. It serves as a visual indication for other programmers working with the code, making the goal apparent and reducing the potential of errors or inadvertent function concealment.

Below is an illustration highlighting the use of the override identifier and its usage in reference to the virtual function in C++ programs.

Code Example:

Output:

Derived class

Explanation:

  1. In the example above, the Derived class, which inherits from the Base class, overrides the virtual function print() in the base class.
  2. We clearly state that we want to override the virtual method from the base class by using the override identifier in the derived class.
  3. When the base class pointer, basePtr, is used to invoke the print() method for derivedObj, the overridden print() function in the derived class is invoked. This uses std::cout to output the 'Derived class' in the console.

By validating that the function in the derived class is truly replacing a virtual function in the base class, the override identifier offers compile-time safety and better code dependability.

Late Binding (Dynamic Binding) & Virtual Function In C++

Late binding, also known as dynamic binding or runtime binding in C++, allows the choice of the right function or method implementation to be made at runtime based on the actual object type. Late binding is described as follows:

  • Each object has two types in C++: The two types of objects include static type and dynamic type. A static type is a type that is selected at compile time depending on the defined type of pointer or reference. The particular type of object being pointed to or addressed.
  • Polymorphism and Virtual Functions: Late binding is linked to polymorphism and virtual functions in C++. By marking a function in the base class as virtual, descendant classes are allowed to replace it with their own implementations.
  • Late Binding Mechanism: When a virtual function in C++ is invoked via a base class pointer or reference, the choice of which function implementation to call is decided at runtime based on the dynamic type of the object being referred to or referenced.
  • Dynamic Dispatch: Late binding uses a method called dynamic dispatch to resolve the function call. A virtual pointer (VPTR) and a virtual function table (VTABLE) are assigned to each object for this.
  • Virtual Function Table (VTABLE): The addresses of the virtual functions are listed in the virtual function table (VTABLE), a lookup table. Each class with at least one virtual function has its own VTABLE. The VTABLE is created for each class, not per object.
  • Virtual Pointer (VPTR): Each object in a class containing virtual functions contains a secret pointer known as the VPTR. The VPTR indicates the class-related VTABLE. During runtime, the VPTR is used to choose the proper VTABLE.
  • Late Binding: When a virtual function in C++ is invoked, the object's VPTR is used to locate the VTABLE. The compiler estimates the offset of the virtual function within the VTABLE based on the function call. After collecting its address from the VTABLE entry, the call to the relevant function is directed to the appropriate implementation based on the dynamic type of the object.
  • Achieving Polymorphic Behavior: Late binding in C++ allows objects from different derived classes to be treated similarly by utilizing a shared base class interface. This allows us to write reusable code and increase extensibility and adaptability in code execution.

In short, the late binding feature of C++ allows for dynamic virtual function dispatch at runtime based on the actual object type. It allows polymorphic behaviour and increases code flexibility and extensibility by allowing objects of multiple derived classes to be treated consistently through a shared base class interface.

Advantages & Disadvantages Of Virtual Function In C++

The advantages and disadvantages of using a virtual function in C++ are as follows:

Advantages Of Virtual Function in C++:

  • Enables Run-Time Polymorphism: Virtual function in C++ allows us to achieve run-time polymorphism, enabling dynamic method resolution based on the actual object type rather than the reference type. This is crucial for implementing polymorphic behavior in object-oriented programming.

  • Flexibility and Extensibility: Virtual function in C++ makes it easier to extend the functionality of existing code. We can create new derived classes and override virtual functions without modifying the existing base class, promoting code reuse and reducing maintenance.

  • Dynamic Binding: Virtual function in C++ allows for dynamic binding, meaning that the function call is resolved at runtime. This enables more flexible and adaptable code, especially in scenarios where the type of object is not known until runtime.

  • Supports Abstract Classes and Interfaces: Virtual function in C++ is essential for creating abstract classes which serve as blueprints for other classes. By declaring a virtual function as pure (virtual void func() = 0;), we can create interfaces that must be implemented by derived classes.

  • Improved Code Organization and Maintenance: A virtual function in C++ separates implementation details from the interface thus aiding in code organization and maintenance. Changes to the base class can affect derived classes without breaking the code, making code maintenance easier.

Disadvantages Of Virtual Function in C++:

  • Performance Overhead: Since virtual function in C++ relies on dynamic binding, it introduces a performance overhead compared to non-virtual functions. The overhead comes from the need to resolve the function call at runtime using the virtual table (vtable).

  • Increased Memory Usage: Each class with virtual functions typically has a vtable, and each object of such a class has a pointer to the vtable. This slightly increases memory footprint, especially in large-scale applications with many objects.

  • Lack of Inline Optimization: The compiler cannot inline a virtual function in C++ because their resolution happens at runtime. This can lead to less efficient code compared to non-virtual functions, where the compiler can optimize the code by inlining function calls.

  • Complexity in Understanding and Debugging: The use of virtual function in C++ adds complexity to the code, making it harder to understand, debug, and maintain, especially for developers unfamiliar with polymorphism and dynamic binding in C++.

  • Complex Inheritance Hierarchy: The inheritance hierarchy might become more convoluted because of the virtual function in C++. When many layers of inheritance are involved, the behaviour of virtual functions can become more complicated, demanding careful design and comprehension to ensure correct function overriding and behaviour.
  • Risk of Slicing: When passing objects by value instead of by reference or pointer, there is a risk of object slicing, where the derived class's parts are sliced off, and only the base class part is copied. This can lead to unexpected behavior when virtual functions are involved.

Overall, the virtual function in C++ provides a variety of advantages in terms of polymorphism, code organization, and flexibility. However, they do have certain performance overhead and complexity issues that must be evaluated against the specific application needs.

Conclusion

The virtual function in C++ is a powerful feature that facilitates runtime polymorphism, enabling developers to create more flexible and extensible code. By using virtual functions, you can achieve polymorphic behaviour, improve code reusability, and create a more maintainable and organized codebase. Understanding virtual functions is crucial for any C++ programmer who wants to leverage the full potential of object-oriented programming and build robust applications.

Also read: 51 C++ Interview Questions For Freshers & Experienced (With Answers)

Frequently Asked Questions:

Q. What is virtual vs. static function C++?

Virtual and static functions are two distinct ideas in C++ that have different functions. They are described in the table below:

Virtual Function In C++ Static Function In C++
  • Virtual functions are functions that are declared in a base class and can be overridden in derived classes.
  • They provide runtime polymorphism by utilizing a single base class interface, enabling objects from diverse derived classes to be treated uniformly.
  • The exact function implementation to be called at runtime is determined by the dynamic type of the object.
  • The "virtual" keyword is used in the base class to declare virtual functions, and the same function signature can be used to override them in derived classes.
  • Virtual functions need dynamic dispatch and late binding when using a virtual function table (VTABLE) and a virtual pointer (VPTR).
  • They are used when the behavior of a function in a derived class has to be specialized or customized.
  • Static functions, often known as class functions or class methods, are functions that are linked to a class rather than the class's objects.
  • They may be accessed by using the class name without requiring an object and are not dependent on specific instances of the class.
  • Because static functions are not tied to any specific object, they do not have access to a class's non-static members or data.
  • The scope resolution operator (::) is used after the class name, and they are declared using the "static" keyword.
  • Static functions are shared by all instances of the class and are unaffected by any single object's present state.

To summarize, virtual functions are utilized to enable dynamic dispatch depending on the actual object type and accomplish runtime polymorphism. Static functions, on the other hand, are associated with a class and are not dependent on specific objects. They are typically used for utilitarian chores or operations that do not need access to object-specific information.

Q. Can virtual functions be private in C++?

No, a virtual function in C++ can not be private. The access specifier (public, protected, or private) determines the visibility of class members in C++. Private members are only available from within the class and cannot be accessed from outside the class or its derived classes.

Virtual functions, on the other hand, are intended to be overridden in derived classes in order to provide polymorphic behavior. In order for derived classes to override a base class function, the function must be accessible. If a virtual function in C++ is designated as private, descendant classes cannot see or access it, nor can they override it.

Here is an illustration to show this:

Output: 

Error

Explanation:

In the above example, the function is not accessible. Hence any attempt to override the private virtual function functionName() in the derived class would result in a compilation error. As a result, for effective function overriding and polymorphic behaviour, virtual functions should be specified as public or protected so that derived classes may access them.

Q. What is a namespace in CPP?

In C++, a namespace is a declarative region that provides a scope to identifiers, such as variables, functions, and classes. It helps organize code and prevent name conflicts by allowing the same name to be used for different entities in different namespaces.

For instance, namespace A { int x; } and namespace B { int x; } can coexist without conflict. Namespaces also support nested namespaces and the use of the directive to simplify access to their contents. This organization improves code readability and maintainability by grouping related components together.

Q. Why do we use pure virtual functions in C++?

In C++, a pure virtual function is one that has no implementation yet is defined in a base class. To indicate this, the = 0 specifier is appended to the function definition. Pure virtual functions are used in the following ways:

  • Pure virtual functions in C++ are typically used to construct abstract base classes. An abstract base class is a class that cannot be instantiated on its own and is designed to serve as the basis for derived classes. When a function is defined to be purely virtual, the base class is tagged as abstract, suggesting that the function is meant to be subclassed and that derived classes must implement it.
  • Pure virtual functions define an interface or a contract that derived classes must follow. They define a set of operations that derived classes must carry out. By employing a shared base class pointer or reference, all derived classes have a common interface, allowing objects from different derived classes to be treated uniformly.
  • Pure virtual functions provide both dynamic binding and polymorphism. Because the derived classes must provide an implementation for the pure virtual function, they are allowed to change how it is overridden. As a result, objects from multiple derived classes might behave differently despite being accessible via a single base class interface.
  • Compulsory Implementation: When a function is declared pure virtual, descendant classes must provide an implementation. By demanding that every descending class implement the pure virtual function, it eliminates unintended omissions and clarifies which functions need to be overridden in derived classes.
  • Defining Abstract Ideas: Pure virtual functions can be used to construct abstract ideas or actions that may have different implementations in different derived classes. This helps define common functionality at the basic level while still allowing for specialization in derived classes.

It is worth noting that an abstract class contains at least one pure virtual function and that objects of an abstract class cannot be generated. Pure virtual functions in C++ provide a way to define abstract classes, create interfaces, enforce the implementation of derived classes, and achieve polymorphic behavior.

Q. What is polymorphism in C++?

The concept of polymorphism is a fundamental idea in C++, which enables objects of various classes to be treated as belonging to a single base class. It gives you the ability to write more flexible and generic code that can interact with objects of various types.

  • Utilizing virtual functions and function overriding allows for polymorphism. Derived classes may substitute their own implementation for one or more virtual functions that are present in the base class. It follows that, despite possibly belonging to different derived classes, objects can still be accessed and managed by using a standard interface provided by the base class.
  • The strength of polymorphism lies in its capacity to handle objects according to their runtime type. Pointers or references to the base class are used to achieve this. Based on the actual type of the object being pointed to or referenced, the appropriate derived class implementation is called when a virtual function is called through a pointer or reference.
  • Because generic functions and algorithms can operate on objects of various derived classes without requiring knowledge of their specific types. This way, polymorphism enables code reuse.
  • Additionally, it makes dynamic binding possible, in which the function call is resolved based on the actual type of the object during runtime rather than statically at compile-time.

Overall, C++'s powerful polymorphism mechanism fosters code flexibility, extensibility, and maintainability by enabling the creation of generic code that can interact fluidly with objects of various types.

Q. Why can virtual functions not be static?

While static functions in C++ serve a different function and behave differently, virtual functions are made to be used with inheritance and polymorphism. Virtual functions cannot be static for a few reasons:

  • Dynamic Dispatch: The virtual function in C++ supports dynamic dispatch, which allows for the runtime selection of the appropriate function implementation based on the actual object type. Static functions cannot support this dynamic binding because their resolution occurs at compile time. They also cannot be overridden in derived classes because they are tied to the class as a whole rather than specific objects.
  • Object Context: Virtual functions are linked to objects and called by pointers to or references to those objects. On the other hand, static functions lack access to context-specific objects because they are not connected to any specific instance of the class. 
  • Memory Allocation: A virtual function in C++ uses concepts of virtual tables (vtables) and virtual pointers (vpointers) to determine which virtual function to call. Each object that contains virtual functions has its own set of tables and object pointers that are created and maintained. This additional memory allocation for vtables and vpointers is not necessary for static functions because they are independent of any object.
  • Inheritance Hierarchy: The inheritance hierarchy excludes static functions from its scope. All instances of the class share them, and they are a part of the class itself. On the other hand, polymorphism within an inheritance hierarchy is achieved using virtual functions. They enable particular behaviors for each derived class by enabling derived classes to override the implementation of the base class.

In conclusion, a static function and a virtual function in C++ have different functions and behave differently. While static functions are independent of any particular object and function at the class level, a virtual function in C++ is used for dynamic dispatch and polymorphism. 

You might also be interested in reading the following:

  1. What is Function Prototype In C++ (Definition, Purpose, Examples)
  2. Constructor In C++ | Types, Benefits, Uses & More (With Examples)
  3. Static Data Member In C++ | An Easy Explanation (With Examples)
  4. Typedef In C++ | Syntax, Application & How To Use It (With Examples)
  5. Encapsulation In C++ | Getter/ Setter Methods & More (+Examples)
Shivani Goyal
Manager, Content

I am an economics graduate using my qualifications and life skills to observe & absorb what life has to offer. A strong believer in 'Don't die before you are dead' philosophy, at Unstop I am producing content that resonates and enables you to be #Unstoppable. When I don't have to be presentable for the job, I'd be elbow deep in paint/ pencil residue, immersed in a good read or socializing in the flesh.

TAGS
Computer Science C++ Programming Language Engineering Placements
Updated On: 13 Aug'24, 02:01 PM IST