1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include<iostream>
#include<iomanip>
using namespace std;
class ComplexNum
{
public:
ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard
ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators
//overloaded operators
ComplexNum& operator = (const ComplexNum& that) = default;
ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); }
ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); }
ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); }
ComplexNum& operator ++() { return square(*this); } //called for ++num
ComplexNum operator ++(int) { return square(*this); } //called for num++
ostream& print(ostream& stm = cout) const;
private:
float real; //float data member for real number (to be entered in by user)
float imaginary; //float data member for imaginary number (to be entered in by user)
//non-member overloaded operators
//a is passed by value
friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; }
friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; }
friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; }
friend ComplexNum operator++(ComplexNum a) { return a++; }
friend ComplexNum operator++(ComplexNum a) { return ++a; }
friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); }
};
ComplexNum::ComplexNum(float a, float b)
{
real = a;
imaginary = b;
}
ComplexNum& ComplexNum::getComplexNum()
{
ComplexNum keyboard;
cout << "Enter real part of complex number: ";
cin >> real;
cout << "Enter imaginary part of complex number: ";
cin >> imaginary;
return keyboard;
}
ComplexNum& ComplexNum::square(ComplexNum a)
{
this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
this->imaginary = (2 * (a.real * a.imaginary));
return *this;
}
ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
this->real = a.real + b.real;
this->imaginary = a.imaginary + b.imaginary;
return *this;
}
ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
this->real = a.real - b.real;
this->imaginary = a.imaginary - b.imaginary;
return *this;
}
ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
return *this;
}
ostream& ComplexNum::print(ostream& stm) const
{
return stm << "(" << noshowpos << real << showpos << imaginary << "i)";
}
int main()
{
ComplexNum a, b;
cout << "First Complex Number:" << endl;
a.getComplexNum();
cout << endl;
cout << "Second Complex Number:" << endl;
b.getComplexNum();
cout << endl;
cout << fixed << setprecision(2)
<< "a == " << a << '\n'
<< "b == " << b << '\n'
<< "a+b == " << a + b << '\n'
<< "a-b == " << a - b << '\n'
<< "a*b == " << a*b << '\n'
<< "a*a == " << a*a << '\n'
<< "b*b == " << b*b << '\n';
cout << "a*a (using postincrement) ==" << a++ << '\n'; //works fine
cout << "a*a (using preincrement) ==" << ++a << '\n'; //gives me an error...why?
cout << endl;
system("PAUSE");
}
|