Can someone help me please?


a) Modify the class to enable input and output of complex number via overloaded >> and << operators, respectively(you should remove the print function from the class).

b)Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.

//Fig. 11.20 : Complex.h
//Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex(double = 0.0, double = 0.0); // constructor
Complex operator+( const Complex &) const; // adition
Complex operator-( const Complex &) const; // substraction
void print() const; // output
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex

#endif
******************************************************************************************

// Fig. 11.21: Complex.cpp
// Complex class member-function definitions
#include "Complex.h" // Complex class definition
#include<iostream>

using namespace std;

// constructor
Complex::Complex(double realPart, double imiginaryPart) : real(realPart),imaginary(imiginaryPart)
{
// empty body
} // end Complex constructor

// addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real , imaginary + operand2.imaginary);
} // end function operator+

// substraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real , imaginary - operand2.imaginary);
} // end function operator-

// display a Complex object in the form: (a,b)
void Complex::print() const
{
cout << '(' << real << ", " << imaginary <<')';
} // end function
*********************************************************************************************************
// Fig. 11.22:fig11_22.cpp
// Complex class test program.
#include<iostream>
#include"Complex.h"
using namespace std;

int main()
{
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);

cout << "x: ";
x.print();
cout <<"\ny: ";
y.print();
cout <<"\nz: ";
z.print();

x = y + z;
cout<< "\n\nx = y + z:" << endl;
x.print();
cout<< " = ";
y.print();
cout<< " + ";
z.print();

x = y - z;
cout<< "\n\nx = y - z:" << endl;
x.print();
cout<< " = ";
y.print();
cout<< " - ";
z.print();
cout << endl;

system("PAUSE");
return 0;
} //end main
The instructions seemed pretty clear. What problems were you having?
Topic archived. No new replies allowed.