GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.geeksforgeeks.org — Polymorphism in C++ - GeeksforGeeks Polymorphism in C++Last Updated : 31 Aug, 2026 The word polymorphism means having many forms. In C++, polymorphism concept can be applied to functions and operators. A single function name can work differently in different situations. Similarly, an operator works different when used in different context.
Output Dog barks Explanation: The Dog class provides its own implementation of the sound() function. When d.sound() is called, the Dog class version is executed, showing how the same function name can have different behavior in different classes. Types of PolymorphismIn C++, polymorphism is mainly divided into two types ![]() Compile-Time PolymorphismCompile-time polymorphism is also known as static polymorphism or early binding. In this type of polymorphism, the compiler decides which function or operator to call during compilation. It is achieved using: 1. Function Overloading Function Overloading allows multiple functions to have the same name but different parameter lists.
Output Integer Sum = 12 Float Sum = 11.5 Explanation: The class contains two add() functions with different parameter types. The compiler selects the appropriate function based on the arguments passed during the function call. 2. Operator Overloading Operator overloading allows operators such as +, -, and * to work with user-defined types.
Output 12 + i9 Explanation: The overloaded + operator adds the real and imaginary parts of two Complex objects. When c1 + c2 is used, the overloaded operator function is called automatically. Runtime PolymorphismRuntime polymorphism is also known as dynamic polymorphism or late binding. The decision about which function to execute is made while the program is running.
![]() In above Diagram:
Even though all animals use the same function name speak(), the behavior changes according to the object. 1. Function Overriding Function Overriding occurs when a derived class provides its own implementation of a virtual function defined in the base class.
Output Derived class function Explanation
Advantages of PolymorphismPolymorphism makes C++ programs more flexible and easier to maintain.
Compile-Time Vs Runtime PolymorphismThe major difference between the compile-time and runtime polymorphism is:
Comment ExploreStandard Template Library(STL) Practice & Problems | ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |