Popular Posts

6/24/2012

Sample Operator in C++




#include<iostream.h>
#include<math.h>
class Rectangle{
private:
float l,w;
public:
Rectangle(float l1,float w1){w=w1;l=l1;}
void output();
Rectangle operator =(float s);
Rectangle operator +(Rectangle p);
float operator -(Rectangle p);
int operator > (Rectangle p);
int operator ==(Rectangle p);
float operator *();
float operator !();
float area();
};
void Rectangle::output(){
cout<<w<<"\t"<<l<<endl;
}
float Rectangle::area(){
return l*w;
}
Rectangle Rectangle::operator = (float s){
float w1,l1;
w1=sqrt(s/2);
l1=2*w;
return Rectangle (l1,w1);
}
Rectangle Rectangle::operator + (Rectangle p){
float w1,l1;
l1=l+p.l;
w1=w+p.w;
return Rectangle (l1,w1);
}
float Rectangle::operator -(Rectangle p){
return fabs(area()-p.area());
}
int Rectangle::operator > (Rectangle p){
return (area()>p.area());
}
int Rectangle::operator ==(Rectangle p){
return (area()==p.area());
}
float Rectangle:: operator *(){
return area();
}
float Rectangle::operator !(){
float d;
d=sqrt(l*l+w*w);
return d;
}
void main(){
Rectangle p1(20,10),p2(40,80),p3(30,90);
cout<<"Initialize data"<<endl;
p1.output();
p2.output();
p3.output();
cout<<"========================="<<endl;
p1=72.0; p1.output();
p3=p1+p2; p3.output();
float s1,s2,s3;
s1=p1-p2;cout<<"s1="<<s1<<endl;
s2=*p1;cout<<"Area of p1="<<s2<<endl;
s3=!p1; cout<<"Diagonal of p1="<<s3<<endl;
if (p1>p2) cout<<"Area of p1>p2:"<<endl;
else if (p1==p2) cout<<"Area of p1==p2"<<endl;
else cout<<"Area of p1<p2"<<endl;
cout<<p1.area();
cout<<*p1;
cout<<!p1;
                  }
//Leave us a command if u like this post ..!

0 comments:

Post a Comment