Question: Using standard C++ , please provide operator overloading functions for a complex number program. Here is the problem in detail: The code is attached below
Using standard C++, please provide operator overloading functions for a complex number program. Here is the problem in detail:
The code is attached below for a complex number class. Provide operator overloading functions to facilitate the following operations on any complex numbers A, B and C:
A == B; overload Boolean operator == to test if A and B are equal
C = A*B; overload multiplication operator *
A = B; overload assignment operator =
cin >> A >> B; overload stream operator >> to take user input for real and imaginary part of the number (You might want to create a friend function to facilitate this)
Also, write a static function for the complexNumber class which initializes numCount variable. Your function takes an integer argument provided by user. Assign this argument to numCount variable.
_______________________________________________
#include
using namespace std;
class complexNumber {
private:
static int numCount; int Re, Imag;
public:
complexNumber(){numCount++;} // constructor with no argument
complexNumber(int Real, int Imaginary){numCount++; cout << "constructor with argument is called" << endl;
Re = Real; Imag = Imaginary;}// constructor with arguments
~complexNumber(){cout << "destructor is called" < complexNumber (complexNumber &anycomplex) {numCount++; cout < void printComplex(){ cout <<"number of active complex numbers= " << numCount << " and present number.....= " << Re <<" + j" << Imag << endl;} complexNumber operator+(complexNumber &b){complexNumber temp; cout << "overloaded + iscalled" << endl; temp.Re = Re + b.Re; temp.Imag = Imag + b.Imag; return temp;} complexNumber operator++(){complexNumber temp; cout << "overloaded ++(pre) called" << endl; temp.Re = ++Re; temp.Imag = ++Imag; return temp;} complexNumber operator++(int){complexNumber temp; cout << "overloaded ++(post) called" << endl; temp.Re = Re++; temp.Imag = Imag++; return temp;} friend ostream & operator<< (ostream &out, complexNumber &somecomplex){cout << "overloaded << called" < }; int complexNumber::numCount = 0 ; void printC(complexNumber a) {a.printComplex();} int main (){ complexNumber A, B(1,2), C(2,3); B.printComplex(); ++ B; printC(C); A = B + C; A = B++; A.printComplex(); B.printComplex(); cout << A << B; cin.get(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
