Monday, August 15, 2011

Function Overloading


In this practical session we were introduced to function overloading in OOP. Using the concept of overloading we can design a group of functions with one name but different argument lists.   



#1. Problem statement:
We have to calculate the average of three numbers using the idea of function overloading.

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

int avg(int a,int b,int c);
double avg(double a,double b,double c);
double avg(int a,double b,double c);
double avg(double a,double b,int c);

void main(){
      float x,y,z;
      clrscr();
      avg(1,2,3);
      avg(6.5,7.5,5.5);
      avg(3,6.5,5.0);
      avg(6.5,5.0,3);
      getch();
}

int avg(int a,int b,int c){
      int i;
      i=(a+b+c)/3;
      cout<<"the avg of 1,2,3 is="<<i<<endl;
      return i;
}
double avg(double a,double b,double c){
      float j;
      j=(a+b+c)/3;
      cout<<"the avg of 6.5,7.5,5.5 is="<<j<<endl;
      return j;
}
double avg(int a,double b,double c){
      float k;
      k=(a+b+c)/3;
      cout<<"the avg of 3,6.5,5.0 is="<<k<<endl;
      return k;
}
double avg(double a,double b,int c){
      float l;
      l=(a+b+c)/3;
      cout<<"the avg of 6.5,5.0,3 is="<<l<<endl;
      return l;
}
Remark:
I solved the program successfully,


#2.Problem statement:
We have to add two numbers and devide the result by the third one.

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

int avg(int a,int b,int c);
double avg(double a,double b,double c);
double avg(int a,double b,double c);
double avg(double a,double b,int c);

void main(){
      float x,y,z;
      clrscr();
      avg(1,2,3);
      avg(6.5,7.5,5.5);
      avg(3,6.5,5.0);
      avg(6.5,5.0,3);
      getch();
}

int avg(int a,int b,int c){
      int i;
      i=(a+b)/c;
      cout<<"the result of adding 1 & 2 then dividing by 3 is="<<i<<endl;
      return i;
}
double avg(double a,double b,double c){
      float j;
      j=(a+b)/c;
      cout<<"the result of adding 6.5 & 7.5 then dividing by 5.5 is="<<j<<endl;
      return j;
}
double avg(int a,double b,double c){
      float k;
      k=(a+b)/c;
      cout<<"the result of adding 3 & 6.5 then dividing by 5 is="<<k<<endl;
      return k;
}
double avg(double a,double b,int c){
      float l;
      l=(a+b)/c;
      cout<<"the result of adding 6.5 & 5.0 then dividing by 3 is="<<l<<endl;
      return l;
}
Remark:
I solved the program successfully,


I solved the problems passing the actual arguments

No comments:

Post a Comment

Vision