24 Nov 2019

  • November 24, 2019
  • Amitraj
1) Compile time Polymorphism:-

Linking of function call to a function definition which is known
as compile time polymorphism or static binding or static linking
or early binding. Compile time Polymorphism Example
In this example, we have two functions with same name but different
number of arguments.Based on how many parameters we pass during
function call determines which function is to be called,this is why it
is considered as an example of polymorphism because in different
conditions the output is different. Since, the call is determined during
compile time thats why it is called compile time polymorphism.


// C++ program to demonstrate compile time polymorphism. #include <iostream> using namespace std; class Add { public: int sum(int num1, int num2) { return num1+num2; } int sum(int num1, int num2, int num3){ return num1+num2+num3; } }; int main() { Add obj; //This will call the first function cout<<"Output: "<<obj.sum(10, 20)<<endl; //This will call the
// second function cout<<"Output: "<<obj.sum(11, 22, 33); return 0; } INPUT/OUTPUT: Output: 30 Output: 66

Related Posts:

  • Virtual Destructors in C++ Virtual Destructors in C++ A virtual destructor however, is pretty much feasible in C++. in fact, its use is often promoted in certain situations. One such situation is when we need to make sure that the different dest… Read More
  • Why A Constructor can not be Virtual Why A Constructor can not be Virtual "A constructor can not be virtual". There are some valid reasons that justify this statement. -> First, to create an object the constructor of the object class must be of the same ty… Read More
  • Templates in C++ Templates in C++ -> Templates is one of the features added to C++ Recently. it is a new concept which enable us to define Generic classes and functions and thus provides support for Generic programming. -> Generic pr… Read More
  • Abstract Class and Pure Virtual Function in C++ Abstract Class and Pure Virtual Function in C++ -> An Abstract class is one that is not used to create objects.An abstract class is designed only to act as a base class. -> Abstract Class is a class which contains at… Read More
  • Virtual Function in C++ C++ Virtual Function:- -> When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual precending its normal declaration. when a fun… Read More

Translate

Popular Posts