Question: CSC421 Assignment 1 (50 Points) Read the following program named myComplex.cpp carefully and implement the overloaded operators +, -, *, /, and the friend ostream
CSC421 Assignment 1
(50 Points)
Read the following program named myComplex.cpp carefully and implement the overloaded operators +, -, *, /, and the friend ostream operator << of the Complex class as indicated by the comments. Compile, debug and test the program using the following command lines before your submission:
c++ -o myComplex myComplex.cpp
./myComplex
// Student Name
// Student ID
// myComplex.cpp
#include
#include
#include
using namespace std;
class Complex { // complex number class
float re; // real part
float im; // imagination part
public:
Complex(float r=0.0, float i=0.0){re=r; im=i;} // constructor
Complex(const Complex& c){re=c.re; im=c.im;} // copy constructor
void operator =(const Complex& c){re=c.re; im=c.im;} // assignment
Complex operator -()const{return Complex(-re, -im);} // unary negation
Complex operator +(const Complex&) const; // addition operator
Complex operator -(const Complex&) const; // subtraction operator
Complex operator *(const Complex&) const; // multiplication operator
Complex operator /(const Complex&) const; // division operator
friend ostream& operator <<(ostream&, const Complex&); // ostream operator
};
// addition operator, to be implemented
Complex Complex::operator +(const Complex& c) const {}
//subtraction operator, to be implemented
Complex Complex::operator -(const Complex& c) const {}
// multiplication operator, to be implemented
Complex Complex::operator *(const Complex& c) const {}
// division operator, to be implemented
Complex Complex::operator /(const Complex& c) const {}
// ostream operator, to be implemented
ostream& operator <<(ostream& os, const Complex& c) {}
int main(){
Complex x[8], y[8], s[8], d[8], p[8], q[8];
for(int i=0; i<8; i++){
x[i] = Complex(i+1,i+1);
y[i] = Complex(i+2, i+2);
s[i] = x[i]+y[i];
d[i] = x[i]-y[i];
p[i] = x[i]*y[i];
q[i] = x[i]/y[i];
cout<<"i="<
cout << "x=" < cout << "x+y="< cout << "x-y="< cout << "x*y="< cout << "x/y="< } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
