What you will learn Implementing classes File I/O More operator overloading Erro
ID: 3743215 • Letter: W
Question
What you will learn Implementing classes File I/O More operator overloading Error checking Coding exercise Create a class called complexNumber that stores a complex number of the form a+bi, where i is v-1, a is the real and b is the imaginary part of the number. You should be able to get the real and imaginary parts of the number. a and b can be negative, e.g., "3+4i", "-3+4i", "3-4i", and "-3- 4i" 1. 2. Implement the ability to add, subtract, and multiply two complexNumber objects and save the 3. Overload the operators>andExplanation / Answer
class complexNumber {
private :
int real;
int imag;
public:
void setReal( int real ) {
this.real = real;
}
void setComplex( int imag ) {
this.complex = imag;
}
complexNumber operator+(const complexNumber & b) {
complexNumber complexNum;
complexNum.real = this.real + b.real;
complexNum.imag = this.imag + b.imag;
return complexNum;
}
complexNumber operator-(const complexNumber & b) {
complexNumber complexNum;
complexNum.real = this.real - b.real;
complexNum.imag = this.imag - b.imag;
return complexNum;
}
complexNumber operator*(const complexNumber & b) {
complexNumber complexNum;
complexNum.real = this.real * b.real - this.imag * b.imag;
complexNum.imag = this.real*b.imag + this.imag*b.real;
return complexNum;
}
friend ostream & operator << (ostream &out, const complexNumber &c);
friend istream & operator >> (istream &in, complexNumber &c);
}
ostream & operator << (ostream &out, const complexNumber &c)
{
out << c.real;
if (c.imag < 0){
out << "-";
} else {
out << "+";
}
out << "i" << c.imag << endl;
return out;
}
istream & operator >> (istream &in, complexNumber &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginory Part ";
in >> c.imag;
return in;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.