OPERATOR OVERLOADING INCLUDING BINARY OPERATOR


SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class complex
{
  float x,y;
  public:
    complex()
    { }
    complex(float real,float img)
    {
      x=real;y=img;
    }
    friend complex operator+(complex,complex);
    void display();
};
complex operator+(complex a,complex b)
{
  complex c;
  c.x=a.x+b.x;
  c.y=a.y+b.y;
  return c;
}
void complex::display()
{
  cout<<x<<"+j"<<y<<"\n";
}
void main()
{
  complex c1(2.1,3.5);
  complex c2(1.6,2.7);
  complex c3;
  clrscr();
  c3=c1+c2;
  cout<<"\n C1:\t"<<c1.display();
  cout<<"\n C2:\t"<<c2.display();
  cout<<"\n C3:\t"<<c3.display();
  getch();
}
 
OUTPUT:

 C1:    2.1+j3.5

 C2:    1.6+j2.7

 C3:    3.7+j6.2
Previous
Next Post »

Still not found what you are looking for? Try again here.