OOP/CPP Class for all Arithmetic Operator overloading

#include<iostream>
using namespace std;
class Float
{
  float f1,f2;
  public:
void set(float x,float y)
        {f1=x;f2=y;}
Float operator + (Float ob);
Float operator – (Float ob);
Float operator * (Float ob);
Float operator / (Float ob);
void display()
{
     cout<<“f1 = “<<f1<<”      “<<“f2 = “<<f2<<“\n”;
     }
};
Float Float :: operator + (Float ob)
{
  Float temp;
  temp.f1 = f1 + ob.f1;
  temp.f2 = f2 + ob.f2;
  return temp;
}
Float Float :: operator – (Float ob)
{
  Float temp;
  temp.f1 = f1 – ob.f1;
  temp.f2 = f2 – ob.f2;
  return temp;
}
Float Float :: operator * (Float ob)
{
  Float temp;
  temp.f1 = f1 * ob.f1;
  temp.f2 = f2 * ob.f2;
  return temp;
}
Float Float :: operator / (Float ob)
{
  Float temp;
  temp.f1 = f1 / ob.f1;
  temp.f2 = f2 / ob.f2;
  return temp;
}
main()
{
  Float ob1,ob2,ob3,ob4,ob5,ob6;
  ob1.set(30.5,40.6);
  ob2.set(20.8,30.4);
  ob3=ob1+ob2;
  ob4=ob1-ob2;
  ob5=ob1*ob2;
  ob6=ob1/ob2;
  ob1.display();
  ob2.display();
  cout<<“After Operator Overloading……….”<<“\n\n”;
  ob3.display();
  ob4.display();
  ob5.display();
  ob6.display();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>