A complex number is of the form a+ib. Let x = a +ib and y =c + id. The operation
ID: 3906386 • Letter: A
Question
A complex number is of the form a+ib. Let x = a +ib and y =c + id.
The operation of adding two complex numbers is: x +y = (a +c) + i(b+ d).
The operation of subtracting two complex number is: x-y = (a -c) - i(b-d).
The operation of multiplying two complex number is:x *y = (ac - bd) + i(bc +ad) .
The operation of dividing two complex number is: x/y = [(ac+bd)/ (c^2 + d^2)] + [i(bc-ad)/(c^2 + d^2)].
Write a class called Complex for adding, subtracting, multiplying, and dividing two complex numbers. These member functions shall accept one (NOT two) complex number and return the result via the return type. The class shall have a single complex number as its data member.
Include one or more constructors as appropriate. Also include the set, get, and display member functions.
Use a header file called Complex.h, an implementation file called Complex.cpp, and an application file called Application.cpp. Test the Complex class in the main function.
Use pass-by-reference to eliminate the overhead of copying the argument to the parameter. If a member function will not modify the data member, make the function constant so that data members will not be accidentally modified in the function body. If a parameter is pass-by-reference and the function will not modify the parameter, use constant data to ensure that the parameter will not be accidentally modified in the function body.
Explanation / Answer
Complex.h
***********************
#ifndef Complex
#define Complex
#include <iostream>
class ComplexNumber {
public:
/**
* Returns a constructed ComplexNumber with values set to 0.
*/
ComplexNumber();
/**
* Returns a constructed C ComplexNumber with given values.
*/
ComplexNumber(float a, float b);
/**
* Returns a copy of this ComplexNumber.
*/
ComplexNumber(const ComplexNumber& rhs);
/**
* Overrides the + operator and adds two ComplexNumbers and returns the result.
*/
ComplexNumber operator+(const ComplexNumber& other);
/**
* Overrides the - operator and subtracts two ComplexNumbers and returns the result.
*/
ComplexNumber operator-(const ComplexNumber& other);
/**
* Overrides the * operator and multiplies two ComplexNumbers and returns the result.
*/
ComplexNumber operator*(const ComplexNumber& other);
/**
* Overrides the / operator and divides two ComplexNumbers and returns the result.
*/
ComplexNumber operator/(const ComplexNumber& other);
/**
* Returns the a value of this ComplexNumber.
*/
double getA() const;
/**
* Sets the a value of this ComplexNumber.
*/
void setA(float _a);
/**
* Returns the b value of this ComplexNumber.
*/
double getB() const;
/**
* Sets the b value of this ComplexNumber.
*/
void setB(float _b);
/**
* Overrides the << operator and returns a string representing this ComplexNumber
*/
friend std::ostream& operator<<(std::ostream& os, ComplexNumber const& cn);
private:
float a;
float b;
};
#endif
Complex.cpp
************************
#include "Complex.h"
#include <iostream>
ComplexNumber::ComplexNumber() {
a = 0;
b = 0;
}
ComplexNumber::ComplexNumber(float _a, float _b) {
a = _a;
b = _b;
}
ComplexNumber::ComplexNumber(const ComplexNumber &rhs) {
a = rhs.a;
b = rhs.b;
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& other) {
ComplexNumber cn;
cn.a = this->a + other.getA();
cn.b = this->b + other.getB();
return cn;
}
ComplexNumber ComplexNumber::operator-(const ComplexNumber& other) {
ComplexNumber cn;
cn.a = this->a - other.getA();
cn.b = this->b - other.getB();
return cn;
}
ComplexNumber ComplexNumber::operator*(const ComplexNumber& other) {
ComplexNumber cn;
cn.a = (this->a * other.getA()) - (this->b * other.getB());
cn.b = (this->b * other.getA()) + (this->a * other.getB());
return cn;
}
ComplexNumber ComplexNumber::operator/(const ComplexNumber& other) {
ComplexNumber cn;
float denominator = ((other.getA() * other.getA()) + (other.getB() * other.getB()));
cn.a = ((this->a * other.getA()) + (this->b * other.getB())) / denominator;
cn.b = ((this->b * other.getA()) - (this->a * other.getB())) / denominator;
return cn;
}
double ComplexNumber::getA() const {
return a;
}
void ComplexNumber::setA(float _a) {
a = _a;
}
double ComplexNumber::getB() const {
return b;
}
void ComplexNumber::setB(float _b) {
b = _b;
}
std::ostream& operator<<(std::ostream& os, const ComplexNumber& cn)
{
os << cn.getA() << " + " << cn.getB() << "i";
return os;
}
Application.cpp
*************************
#include "Complex.h"
#include <iostream>
/**
* Prints success message.
*/
void printSuccess() {
std::cout << "SUCCESS" << std::endl;
}
/**
* Prints failure message.
*/
void printFailure() {
std::cout << "FAILURE" << std::endl;
}
/**
* Tests overloaded add operator function using ComplexNumber entities with
* different values.
*/
void testAdd() {
ComplexNumber cn1(1, 1);
ComplexNumber cn2(1, 1);
ComplexNumber cn = cn1 + cn2;
std::cout << cn << std::endl;
if (cn.getA() == 2 && cn.getB() == 2) {
printSuccess();
} else {
printFailure();
}
ComplexNumber cn3(4, 8);
ComplexNumber cn4(2, 4);
cn = cn3 + cn4;
std::cout << cn << std::endl;
if (cn.getA() == 6 && cn.getB() == 12) {
printSuccess();
} else {
printFailure();
}
}
/**
* Tests overloaded subtract operator function using ComplexNumber entities with
* different values.
*/
void testSubtract() {
ComplexNumber cn1(1, 1);
ComplexNumber cn2(1, 1);
ComplexNumber cn = cn1 - cn2;
std::cout << cn << std::endl;
if (cn.getA() == 0 && cn.getB() == 0) {
printSuccess();
} else {
printFailure();
}
ComplexNumber cn3(4, 8);
ComplexNumber cn4(2, 4);
cn = cn3 - cn4;
std::cout << cn << std::endl;
if (cn.getA() == 2 && cn.getB() == 4) {
printSuccess();
} else {
printFailure();
}
}
/**
* Tests overloaded multiply operator function using ComplexNumber entities with
* different values.
*/
void testMultiply() {
ComplexNumber cn1(1, 1);
ComplexNumber cn2(1, 1);
ComplexNumber cn = cn1 * cn2;
std::cout << cn << std::endl;
if (cn.getA() == 0 && cn.getB() == 2) {
printSuccess();
} else {
printFailure();
}
ComplexNumber cn3(2, 2);
ComplexNumber cn4(4, 2);
cn = cn3 * cn4;
std::cout << cn << std::endl;
if (cn.getA() == 4 && cn.getB() == 12) {
printSuccess();
} else {
printFailure();
}
}
/**
* Tests overloaded divide operator function using ComplexNumber entities with
* different values.
*/
void testDivide() {
ComplexNumber cn1(1, 1);
ComplexNumber cn2(1, 1);
ComplexNumber cn = cn1 / cn2;
std::cout << cn << std::endl;
if (cn.getA() == 1 && cn.getB() == 0) {
printSuccess();
} else {
printFailure();
}
ComplexNumber cn3(2, 6);
ComplexNumber cn4(2, 2);
cn = cn3 / cn4;
std::cout << cn << std::endl;
if (cn.getA() == 2 && cn.getB() == 1) {
printSuccess();
} else {
printFailure();
}
}
/**
* Called at program startup
*
* @param argc the number of arguments
* @param argv the array of arguments
*/
int main(int argc, char *argv[]) {
testAdd();
testSubtract();
testMultiply();
testDivide();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.