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 115
|
#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
void getComplexNum(); //get real and imaginary numbers from keyboard
void sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
void diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
void prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
void squarea(ComplexNum a); //method to change each complex number to its square
void squareb(ComplexNum b); //method to change each complex number to its square
void printComplexNum(); //print sum, diff, prod, square
void formComplexNum(); //and "a+bi" form
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)
};
ComplexNum::ComplexNum(float a, float b)
{
real = a;
imaginary = b;
}
void ComplexNum::getComplexNum()
{
cout << "Enter real part of complex number: ";
cin >> real;
cout << "Enter imaginary part of complex number: ";
cin >> imaginary;
}
void ComplexNum::sum(ComplexNum a, ComplexNum b)
{
this->real = a.real + b.real;
this->imaginary = a.imaginary + b.imaginary;
cout << "The addition of the two Complex Numbers is: ";
}
void ComplexNum::diff(ComplexNum a, ComplexNum b)
{
this->real = a.real - b.real;
this->imaginary = a.imaginary - b.imaginary;
cout << "The difference of the two Complex Numbers is: ";
}
void 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);
cout << "The product of the two Complex Numbers is: ";
}
void ComplexNum::squarea(ComplexNum a)
{
this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
this->imaginary = (2 * (a.real * a.imaginary));
cout << "The square of Complex Number 1 is: ";
}
void ComplexNum::squareb(ComplexNum b)
{
this->real = (b.real * b.real) - (b.imaginary * b.imaginary);
this->imaginary = (2 * (b.real * b.imaginary));
cout << "The square of Complex Number 2 is: ";
}
void ComplexNum::printComplexNum()
{
cout << "(" << real << "(+)" << imaginary << "i" << ")" << endl;
}
void ComplexNum::formComplexNum()
{
cout << "Form '(a+bi)': " << "(" <<real << "(+)" << imaginary << "i" << ")" << endl;
}
int main()
{
ComplexNum a, b, c, d, e, f, g;
cout << "First Complex Number:" << endl;
a.getComplexNum();
a.formComplexNum();
cout << endl;
cout << "Second Complex Number:" << endl;
b.getComplexNum();
b.formComplexNum();
cout << endl;
c.sum(a, b);
c.printComplexNum();
d.diff(a, b);
d.printComplexNum();
e.prod(a, b);
e.printComplexNum();
f.squarea(a);
f.printComplexNum();
g.squareb(b);
g.printComplexNum();
cout << endl;
return 0;
}
|