Question: Complex number class Design a class in C++ that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the
Complex number class Design a class in C++ that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the C++ you will need to implement the following functions for each operation: op: Complex Complex ? Complex op: Complex double ? Complex op: double Complex ? Complex Where op is one of +, -, *, or /. In addition, you will need to overload the stream insertion operator << to print objects of this type. A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better.
here is the sample code
/* * * C++ version * */ /* rational.h */ #ifndef RATIONAL_H #define RATIONAL_H #includeusing std::ostream; struct rational { rational(int = 0, int = 1); rational operator+(const rational &) const; rational operator-(const rational &) const; rational operator*(const rational &) const; rational operator/(const rational &) const; rational operator+(int) const; rational operator-(int) const; rational operator*(int) const; rational operator/(int) const; friend rational operator+(int, const rational &); friend rational operator-(int, const rational &); friend rational operator*(int, const rational &); friend rational operator/(int, const rational &); friend ostream &operator<<(ostream &, const rational &); private: int den; int num; }; #endif /* RATIONAL_H */ /* rational.cc */ #include #include "rational.h" rational::rational(int num, int den) : num(num), den(den) {} rational rational::operator+(const rational &o) const { return rational(num * o.den + o.num * den, den * o.den); } rational rational::operator+(int n) const { return rational(num + n * den, den); } rational rational::operator-(const rational &o) const { return rational(num * o.den - o.num * den, den * o.den); } rational rational::operator-(int n) const { return rational(num - n * den, den); } rational rational::operator*(const rational &o) const { return rational(num * o.num, den * o.den); } rational rational::operator*(int n) const { return rational(num * n, den); } rational rational::operator/(const rational &o) const { return rational(num * o.den, den * o.num); } rational rational::operator/(int n) const { return rational(num, den * n); } rational operator+(int n, const rational &o) { return o + n; } rational operator-(int n, const rational &o) { return rational(n) - o; } rational operator*(int n, const rational &o) { return o * n; } rational operator/(int n, const rational &o) { return rational(n) / o; } ostream &operator<<(ostream &out, const rational &o) { out << '(' << o.num << " / " << o.den << ')'; return out; } /* main.cc */ #include #include "rational.h" using std::cout; using std::endl; int main(void) { rational a(1, 2); rational b(1, 3); int i = 5; cout << a << " + " << b << " = " << a + b << endl; cout << a << " - " << b << " = " << a - b << endl; cout << a << " * " << b << " = " << a * b << endl; cout << a << " / " << b << " = " << a / b << endl; cout << a << " + " << i << " = " << a + i << endl; cout << a << " - " << i << " = " << a - i << endl; cout << a << " * " << i << " = " << a * i << endl; cout << a << " / " << i << " = " << a / i << endl; cout << i << " + " << a << " = " << i + a << endl; cout << i << " - " << a << " = " << i - a << endl; cout << i << " * " << a << " = " << i * a << endl; cout << i << " / " << a << " = " << i / a << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
