I am writing a program for class, but I am stuck on one part--overloading the as
ID: 3631796 • Letter: I
Question
I am writing a program for class, but I am stuck on one part--overloading the assignment operator. According to the prompt I need to use overloaded functions to add/sub/mult/divide imaginary numbers (I have all of the overloaded funcs written except "="). Please, at least give me some direction as to where to go next...The following is my .h and .cpp files excluding function main
// Complex.cpp
// Complex class member-function definitions.
#include <iomanip>
#include "Complex.h" // Complex class definition
using namespace std;
// Constructor
Complex::Complex( double RPart, double iPart )
: R( RPart ),
i( iPart )
{
// empty body
} // end Complex constructor
--------
// addition operator
Complex Complex::operator+( const Complex &op2 ) const
{
return Complex( R + op2.R,
i + op2.i );
} // end function operator+
--------
// subtraction operator
Complex Complex::operator-( const Complex &op2 ) const
{
return Complex( R - op2.R,
i - op2.i );
} // end function operator-
--------
//Use “x = y * z;”
Complex Complex::operator*( const Complex &op2 ) const
{
return Complex(R * op2.R - i * op2.i,
R * op2.i + i * op2.R);
}
---------
//Use “x = y / z;”
Complex Complex::operator/( const Complex &op2 ) const
{
return Complex( ((op2.R * R) + (op2.i * i)) / ((op2.R * op2.R) + (op2.i * op2.i)),
((op2.R * i) - (op2.i * R)) / ((op2.R * op2.R) + (op2.i * op2.i)) );
}
--------
Complex Complex::operator =(const Complex &herp) const
{
herp = herp.derp;
return *this;
}
--------
bool Complex::operator ==( const Complex &op2) const
{
if ((R == op2.R) && (i == op2.i)){
return true;
}
else{
return false;
}
}
--------
bool Complex::operator !=( const Complex &op2) const
{
if ((R != op2.R) || (i == op2.i)){
return true;
}
else{
return false;
}
}
istream &operator>>(istream &in, Complex &ob2)
{
cout << " Enter the real number: ";
in >> ob2.R;
cout << " Enter the imaginary number: ";
in >> ob2.i;
return in;
}
------
ostream &operator<<(ostream &out, const Complex &ob2)
{
out << "(" << ob2.R << ", " << ob2.i<< "i)";
return out;
}
-------------------------------------------------------------------
// Complex.h
// Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator +( const Complex & ) const; // addition
Complex operator -( const Complex & ) const; // subtraction
//void print() const; // output
Complex operator * ( const Complex & ) const; // multiplication
Complex operator / ( const Complex & ) const; // division
Complex operator = ( const Complex & ) const;
bool operator == ( const Complex & ) const;
bool operator != ( const Complex & ) const;
friend istream &operator >> (istream &, Complex & );
friend ostream &operator << (ostream &, const Complex & );
private:
double R; // real part
double i; // imaginary part
double derp;
}; // end class Complex
#endif
Explanation / Answer
please rate - thanks
I also did <<
hope you don't mind
#include <iomanip>
using namespace std;
#include <iostream>
using namespace std;
class Complex
{
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator +( const Complex & ) const; // addition
Complex operator -( const Complex & ) const; // subtraction
//void print() const; // output
Complex operator * ( const Complex & ) const; // multiplication
Complex operator / ( const Complex & ) const; // division
Complex operator = ( const Complex & ) ; //const;
bool operator == ( const Complex & ) const;
bool operator != ( const Complex & ) const;
friend istream &operator >> (istream &, Complex & );
friend ostream &operator << (ostream &, const Complex & );
private:
double R; // real part
double i; // imaginary part
double derp;
}; // end class Complex
// Constructor
Complex::Complex( double RPart, double iPart )
: R( RPart ),
i( iPart )
{
// empty body
} // end Complex constructor
//--------
// addition operator
Complex Complex::operator+( const Complex &op2 ) const
{
return Complex( R + op2.R,
i + op2.i );
} // end function operator+
//--------
// subtraction operator
Complex Complex::operator-( const Complex &op2 ) const
{
return Complex( R - op2.R,
i - op2.i );
} // end function operator-
//--------
//Use “x = y * z;”
Complex Complex::operator*( const Complex &op2 ) const
{
return Complex(R * op2.R - i * op2.i,
R * op2.i + i * op2.R);
}
//---------
//Use “x = y / z;”
Complex Complex::operator/( const Complex &op2 ) const
{
return Complex( ((op2.R * R) + (op2.i * i)) / ((op2.R * op2.R) + (op2.i * op2.i)),
((op2.R * i) - (op2.i * R)) / ((op2.R * op2.R) + (op2.i * op2.i)) );
}
//--------
Complex Complex::operator =(const Complex &herp) //const
{
R=herp.R;
i=herp.i;
}
//--------
bool Complex::operator ==( const Complex &op2) const
{
if ((R == op2.R) && (i == op2.i)){
return true;
}
else{
return false;
}
}
//--------
bool Complex::operator !=( const Complex &op2) const
{
if ((R != op2.R) || (i == op2.i)){
return true;
}
else{
return false;
}
}
istream &operator>>(istream &in, Complex &ob2)
{
cout << " Enter the real number: ";
in >> ob2.R;
cout << " Enter the imaginary number: ";
in >> ob2.i;
return in;
}
ostream &operator <<(ostream &strm, const Complex &c)
{cout<<"("<<c.R<<","<< c.i<<"i)"<<endl;
}
int main()
{Complex a,b,c;
cin>>a;
cin>>b;
cout<<a;
cout<<b;
c=a;
cout<<c;
c=a+b;
cout<<c;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.