4th sem OOPC Write the following programs using OPERATOR OVERLOADING in CPP language.
1. Write a program to enter two integers, two float number, two double numbers and find and display the greater of them using function overloading technique.
#include<iostream>
#include<conio.h>
using namespace std;
class over{
public:
int get(int,int);
float get(float,float);
double get(double,double);
};
int over::get(int a,int b){
if(a>b){
return a;
}else{
return b;
}
}
float over::get(float a,float b){
if(a>b){
return a;
}else{
return b;
}
}
double over::get(double a,double b){
if(a>b){
return a;
}else{
return b;
}
}
int main(){
int n1,n2,t;
float x,y;
double p,q;
over obj;
//clrscr();
cout<<"\nEnter two integer number: "<<endl;
cin>>n1>>n2;
t=obj.get(n1,n2);
cout<<"Maximum number is: "<<t;
cout<<"\nEnter two float number: "<<endl;
cin>>x>>y;
float temp=obj.get(x,y);
cout<<"Maximum number is: "<<temp;
cout<<"\nEnter two double value"<<endl;
cin>>p>>q;
double pq = obj.get(p,q);
cout<<"Maximum number is: "<<pq;
return 0;
}
2. Write a program that uses area () function for calculation area of triangle or a rectangle or a square using function overloading technique (without the use of classes).
3. Write a program to demonstrate the operation of operator overloading function.
#include<iostream>
#include<conio.h>
using namespace std;
class over{
public:
int get(int,int);
float get(float,float);
double get(double,double);
};
int over::get(int a,int b){
if(a>b){
return a;
}else{
return b;
}
}
float over::get(float a,float b){
if(a>b){
return a;
}else{
return b;
}
}
double over::get(double a,double b){
if(a>b){
return a;
}else{
return b;
}
}
int main(){
int n1,n2,t;
float x,y;
double p,q;
over obj;
//clrscr();
cout<<"\nEnter two integer number: "<<endl;
cin>>n1>>n2;
t=obj.get(n1,n2);
cout<<"Maximum number is: "<<t;
cout<<"\nEnter two float number: "<<endl;
cin>>x>>y;
float temp=obj.get(x,y);
cout<<"Maximum number is: "<<temp;
cout<<"\nEnter two double value"<<endl;
cin>>p>>q;
double pq = obj.get(p,q);
cout<<"Maximum number is: "<<pq;
return 0;
}
2. Write a program that uses area () function for calculation area of triangle or a rectangle or a square using function overloading technique (without the use of classes).
#include<iostream>
#include<conio.h>
using namespace std;
float area(float a,float b){
float an;
/*for arera of triangle*/
an = (a*b)/2;
return an;
}
int area(int a){
int an1; /*For area of the square*/
an1=a*a;
return an1;
}
int area(int a,int b){
int an2;
/*For area of rectangle*/
an2 = a*b;
return an2;
}
int main(){
int v,w,x;
float y,z;
cout<<"Enter the value two value for triangle : "<<endl;
cin>>y>>z;
cout<<area(y,z)<<endl;
cout<<"Enter the value value for square : "<<endl;
cin>>v;
cout<<area(v)<<endl;
cout<<"Enter the value two value for rectangle : "<<endl;
cin>>w>>x;
cout<<area(w,x)<<endl;
return 0;
}
3. Write a program to demonstrate the operation of operator overloading function.
#include<iostream>
#include<conio.h>
using namespace std;
class over{
int a,b;
public:
over(){
a=5;
b=3;
}
int putdata(void){
cout<<"\nA= "<<a<<"\nB= "<<b<<endl;
}
int operator+(){
int t;
t=a+b;
return t;
}
};
int main(){
over k;
cout<<"Before operator overloadding.."<<endl;
k.putdata();
int p = +k;
cout<<"total of a+b= "<<p;
}
}
Wow sooo nice and well programming
ReplyDelete