Function Overloading - Polymorphism Cpp / C++

Ram Pothuraju
This Cpp/ C++  Program is for performing Function Overloading / Compile Time Polymorphism. 



The below program performs function overloading to find the sum of 2 numbers.
The function is overloaded to calculate the sum of float, int, double etc.

#include<iostream.h>
#include<conio.h>

class Add
{
 float sum1;
 public :
  void sum(int,int);
  void sum(float,int,int);
  void sum(float,float);
  void sum(double,int);
};

void Add :: sum(int a,int b)
{
 sum1=a+b;
 cout<<endl<<"Sum : "<<sum1;
}

void Add :: sum(float a,int b,int c)
{
 sum1=a+b+c;
 cout<<endl<<"Sum : "<<sum1;
}

void Add :: sum(float a,float b)
{
 sum1=a+b;
 cout<<endl<<"Sum : "<<sum1;
}

void Add :: sum(double a,int b)
{
 sum1=a+b;
 cout<<endl<<"Sum : "<<sum1;
}

void main()
{
 Add a;
 clrscr();
 a.sum(10,2);
 a.sum(2.5,10,5);
 a.sum(3.50,3.50);
 a.sum(11.22,2);
 getch();
}

Tags

Post a Comment

0Comments

Post a Comment (0)