Create a C++ class Sumdata to perform following functions: int sum( int, int) - returns the addition of two integer arguments. float sum(flaot, float, float) - returns the addition of three float arguments. int sum( int [ ] ,int) - returns the sum of all elements in an array of size 'n'.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<iostream.h>
#include<conio.h>
class sumdata
{
   int a,b,s,i;
   public:
   int sum(int x,int y) //function overloading
   {
      a=x;
      b=y;
      return(a+b);
   }
   float sum (float x,float y,float z) //function overloading
   {
    return(x+y+z);
   }
   int sum(int a[],int n) //function overloading
   {
    s=a[0];
       for(i=1;i<n;i++)
       {
        s=s+a[i];
       }
       return (s);
   }
};
int main()
{
   int a[10],n,i,x,y;
   float r,v,k;
   sumdata p; // object created
   cout<<"\nEnter 1st integer number : ";
   cin>>x;
   cout<<"\nEnter 2nd integer number : ";
   cin>>y;
   cout<<"\nsum of 2 integer numbers : "<<p.sum(x,y)<<"\n";
   cout<<"\nEnter 1st Float number : ";
   cin>>r;
   cout<<"\nEnter 2nd Float number : ";
   cin>>v;
   cout<<"\nEnter 3rd Float number : ";
   cin>>k;
   cout<<"\nsum of 3 Float numbers : "<<p.sum(r,v,k)<<"\n";
   cout<<"\nEnter how many elements in an array: ";
   cin>>n;
   for(i=0;i<n;i++)
   {
    cout<<"\nEnter "<<i+1<<" element : ";
      cin>>a[i];
   }
   cout<<"\nsum of all array elements : "<<p.sum(a,n);
   getch();
   return(0);
}

0 Comments