Function Overloading - Polymorphism Cpp / C++


This Cpp/ C++  Program is for performing Function Overloading / Compile Time Polymorphism. This is a part of Mumbai University MCA Colleges C++ / CPP Programs MCA. 


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();
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs and Simple Programs. Happy Programming and Studying.

No comments:

Post a Comment