EXPERIMENT 4:Program to Understand Operator Overloading Concept in C++
EXP: write a program in c++ to Add to Complex no using operator overloading.
#include<iostream>
#include<string.h>
using namespace std;
class complex
{ int real,img;
public:
input()
{
cout<<“\nEnter Real part:-“;
cin>>real;
cout<<“Enter imaginary part:-“;
cin>>img;
}
display()
{cout<<endl<<real<<“+”<<img<<“i”;
}
complex operator-(complex t)
{
complex temp;
temp.real=real+t.real;
temp.img=img+t.img;
return temp;
}
};
int main()
{
complex n1,n2,n3;
n1.input();
n2.input();
n1.display();
n2.display();
cout<<“\n—–“;
n3=n1-n2;
n3.display();
return 0;
}
OUTPUT: Enter Real part 5
Enter Imaginary part 2
Enter Real part 7
Enter Imaginary part 3
5+2i
7+3i
12+5i.
EXPERIMENT 5. Program to Understand Function Overloading Concept in C++.
Function overloading:Function overloading is a concept in oops.Defining multiple function with same name is known as function overloading.
EXP: write a program to find area circle and rectangle using functuion over loading.
#include<iostream>
using namespaces std;
AREA( int x,int y) // function defination
{ int area; //local variable
area=x*y;
cout<<”\n Area of rectangle=”<<area;
}
AREA(int x) //function defination
{ float area;
area=3.14*x*x;
cout<<”Area of circle=”<<area;}
int main()
{ int r,l,b,area;
cout<<”\nEnter length and breath of rectangle”;
cin>>l>>b;
cout<<”\nEnter radius of circle“ ;
cin>>r;
AREA(l,b); // fun call
AREA(r); //fun call
return 0;}
OUTPUT: Enter length and breath of rectangle
5
8
Enter radius of circle
7
Area of Rectangle=40
Area of Circle=153.86