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

Translate

Popular Posts