Saturday, 11 January 2014

Program Of Complex number

A program in C++ to create Class called complex and implement addition and subtraction of complex numbers by overloading the functions add and subtract which returns the complex number. Also overload the operator “<<” to display a complex number.


#include<iostream.h>
#include<conio.h>
class Complex
{private:int real;
            int imag;
 public:void input();
            void add(Complex,Complex);
            void add(int);
            void sub(Complex,Complex);
            void sub(int);
            void display();
};
void main()
{clrscr();
 Complex c1,c2,c3;
 c1.input();
 c1.display();
 c2.input();
 c2.display();
 cout<<"\n\nBEFORE ADDING THE CONSTANT===>\n";
 c3.add(c1,c2);
 c3.display();
 cout<<"\n\nAFTER ADDING THE CONSTANT===>\n";
 c3.add(10);
 c3.display();
 getch();
 c1.input();
 c1.display();
 cout<<"BEFORE SUBSTRACTING THE CONSTANT\n";
 c3.sub(c1,c2);
 c3.display();
 cout<<"AFTER SUBSTRACTING THE CONSTANT\n";
 c2.input();
 c2.display();
 c3.sub(10);
 c3.display();
 getch();
}
void Complex::input()
{cout<<"\n\nENTER THE REAL NUMBERS\t";
 cin>>real;
 cout<<"\nENTER THE IMAGINARY NUMBERS\t";
 cin>>imag;
}
void Complex::add(Complex S,Complex R)
{real=S.real+R.real;
 imag=S.imag+R.imag;
}
void Complex::add(int n)
{real=real+n;
}
void Complex::sub(Complex S,Complex R)
{real=S.real-R.real;
 imag=S.imag-R.imag;
}
void Complex::sub(int n)
{real=real-n;
}
void Complex::display()
{cout<<"\n\nTHE REAL NUMBER IS "<<real;
 cout<<"\nTHE IMAGINARY NUMBER IS "<<imag;
 cout<<"\nTHE COMPLEX NUMBER IS "<<real<<"+"<<imag<<"j";
}



No comments:

Post a Comment