the three files i have are -----complex.h----- #ifndef COMPLEX_H #define COMPLEX
ID: 656601 • Letter: T
Question
the three files i have are
-----complex.h-----
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex(double = 0.0 , double = 0.0);
// Complex operator=(const Complex &);
Complex operator+(const Complex & ) const;
Complex operator-(const Complex & ) const;
Complex operator*(const Complex & ) const;
Complex operator/(const Complex & ) const;
Complex operator||(const Complex & ) const;
void print() const;
protected:
private:
double real;
double imaginary;
};
#endif
-----complex.cpp-----
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex( double realPart, double imaginaryPart)
: real( realPart ),
imaginary( imaginaryPart )
{
// empty body
} // end Complex constructor
Complex Complex::operator+( const Complex &operand2) const
{
return Complex( real + operand2.real,
imaginary + operand2.imaginary);
}
Complex Complex::operator-( const Complex &operand2) const
{
return Complex( real - operand2.real,
imaginary - operand2.imaginary);
}
Complex Complex::operator*( const Complex &operand2) const
{
return Complex( real * operand2.real,
imaginary * operand2.imaginary);
}
Complex Complex::operator/( const Complex &operand2) const
{
return Complex( real / operand2.real,
imaginary / operand2.imaginary);
}
Complex Complex::operator||( const Complex &operand2) const
{
return Complex( real || operand2.real,
imaginary || operand2.imaginary);
}
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
}
------Complextest-----
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex c1(0,0);
Complex c2(1,2);
Complex c3(8,2);
Complex c4(5,-3);
cout << "HW#8 Complex Numbers in Engineering";
cout << endl;
cout << "Author: ";
cout << endl;
cout << "Initial Values" << endl;
cout << "c1: ";
c1.print();
cout << " c2: ";
c2.print();
c1 = c2;
cout << " Assignment operator";
cout << " c1 = c2";
cout << " c1: ";
c1.print();
cout << endl;
cout << "Initial Values" << endl;
cout << "c3: ";
c3.print();
cout << " c4: ";
c4.print();
c1 = c4+c3;
cout << " Overloaded + operator" << endl;
cout << "c4 + c3: ";
c1.print();
c1 = c4-c3;
cout << " Overloaded - operator" << endl;
cout << "c4 - c3: ";
c1.print();
c1 = c4*c3;
cout << " Overloaded x operator" << endl;
cout << "c4 x c3: ";
c1.print();
c1 = c3/c4;
cout << " Overloaded / operator" << endl;
cout << "c3 / c4: ";
c1.print();
cout << " = ";
c3.print();
cout << " / ";
c4.print();
c1 = c4||c3;
cout << " Overloaded || operator" << endl;
cout << "c1 = c2||c3||c4: ";
c1.print();
cout << " = ";
c2.print();
cout << " || ";
c3.print();
cout << " || ";
c4.print();
cin.get();
cin.get();
return 0;
}
Explanation / Answer
-----complex.h-----
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex(double = 0.0 , double = 0.0);
// Complex operator=(const Complex &);
Complex operator+(const Complex & ) const;
Complex operator-(const Complex & ) const;
Complex operator*(const Complex & ) const;
Complex operator/(const Complex & ) const;
Complex operator||(const Complex & ) const;
void print() const;
protected:
private:
double real;
double imaginary;
};
#endif
-----complex.cpp-----
#include <iostream>
#include "Complex.h"
using namespace std;
Complex::Complex( double realPart, double imaginaryPart)
: real( realPart ),
imaginary( imaginaryPart )
{
// empty body
} // end Complex constructor
Complex Complex::operator+( const Complex &operand2) const
{
return Complex( real + operand2.real,
imaginary + operand2.imaginary);
}
Complex Complex::operator-( const Complex &operand2) const
{
return Complex( real - operand2.real,
imaginary - operand2.imaginary);
}
Complex Complex::operator*( const Complex &operand2) const
{
return Complex( real * operand2.real,
imaginary * operand2.imaginary);
}
Complex Complex::operator/( const Complex &operand2) const
{
return Complex( real / operand2.real,
imaginary / operand2.imaginary);
}
Complex Complex::operator||( const Complex &operand2) const
{
return Complex( real || operand2.real,
imaginary || operand2.imaginary);
}
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
}
------Complextest-----
#include <iostream>
#include "Complex.h"
using namespace std;
int main()
{
Complex c1(0,0);
Complex c2(1,2);
Complex c3(8,2);
Complex c4(5,-3);
cout << "HW#8 Complex Numbers in Engineering";
cout << endl;
cout << "Author: ";
cout<<"Dr. Jeffery Gray";
cout << endl;
cout << "Initial Values" << endl;
cout << "c1: ";
c1.print();
cout << " c2: ";
c2.print();
c1 = c2;
cout << " Assignment operator";
cout << " c1 = c2";
cout << " c1: ";
c1.print();
cout << endl;
cout << "Initial Values" << endl;
cout << "c3: ";
c3.print();
cout << " c4: ";
c4.print();
c1 = c4+c3;
cout << " Overloaded + operator" << endl;
cout << "c4 + c3: ";
c1.print();
c1 = c4-c3;
cout << " Overloaded - operator" << endl;
cout << "c4 - c3: ";
c1.print();
c1 = c4*c3;
cout << " Overloaded x operator" << endl;
cout << "c4 x c3: ";
c1.print();
c1 = c3/c4;
cout << " Overloaded / operator" << endl;
cout << "c3 / c4: ";
c1.print();
cout << " = ";
c3.print();
cout << " / ";
c4.print();
c1 = c4||c3;
cout << " Overloaded || operator" << endl;
cout << "c1 = c2||c3||c4: ";
c1.print();
cout << " = ";
c2.print();
cout << " || ";
c3.print();
cout << " || ";
c4.print();
cin.get();
cin.get();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.