C++ Class for Complex Numbers with Demo Application
Submitted by DoctorSpeedCode on Friday, August 8, 2014 - 04:16.
Today, we will see a real and simple C++ class for complex numbers. We will implement:
1) Addition (+)
2) Subtraction (-)
3) Multiplication (*)
4) Division (/)
The class header, called MyComplexNumber.h will be declared like this:
The only public members are:
- the constructors
- get functions (to read real/imaginary part)
- toString function for beautiful output
Now, the next step is understanding how this all works. First, we have created a helper function to convert double to string - you can use this in other projects too!
It is obvious how we have used a stringstream to make it quick, and simple. Also, this doesn't leave junk in the memory.
Next thing, we need to understand how overloading the operators works. The operator= is different in the code, and is like this:
It is a simple one - only uses the 2nd constructor to return the left-hand side.
The other operators are declared differently. The rules used for computation are the mathematical formulas, so there is nothing about C++ techniques, just about mathematics.
As you can see, we have used the copy constructor in each case for simplicity. Then, being friend functions, we can access private members (real, imaginary). For * and /, we could've used this:
The full listing for MyComplexNumber.cpp is here:
Then, we created a small demo program to demonstrate how accurately and perfectly this program outputs results! You can change the toString() function, if you want to use lass digits after the '.'. Here is the main.cpp source code listing:
From the above example, the output will show:
2 + 3i
4 - 5i
(2 + 3i) + (4 - 5i) = 6 - 2i
(2 + 3i) - (4 - 5i) = -2 + 8i
(2 + 3i) * (4 - 5i) = 23 + 2i
(2 + 3i) / (4 - 5i) = -0.170732 + 0.536585i
Have fun with operator overloading and solving complex number problems faster!
- #ifndef MYCOMPLEXNUMBER_H
- #define MYCOMPLEXNUMBER_H
- #include <string>
- using namespace std;
- class MyComplexNumber {
- double real;
- double imaginary;
- MyComplexNumber operator=(const MyComplexNumber&);
- friend MyComplexNumber operator+(const MyComplexNumber&, const MyComplexNumber&);
- friend MyComplexNumber operator-(const MyComplexNumber&, const MyComplexNumber&);
- friend MyComplexNumber operator*(const MyComplexNumber&, const MyComplexNumber&);
- friend MyComplexNumber operator/(const MyComplexNumber&, const MyComplexNumber&);
- public:
- MyComplexNumber();
- MyComplexNumber(double, double);
- MyComplexNumber(const MyComplexNumber&);
- double getReal() const;
- double getImaginary() const;
- string toString() const;
- };
- #endif /* MYCOMPLEXNUMBER_H */
- string DoubleToString(double val) {
- stringstream ss;
- ss << val;
- return ss.str();
- }
- MyComplexNumber MyComplexNumber::operator=(const MyComplexNumber& rhs) {
- MyComplexNumber res(rhs.getReal(), rhs.getImaginary());
- return res;
- }
- MyComplexNumber operator+(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber add(lhs);
- add.real += rhs.real;
- add.imaginary += rhs.imaginary;
- return add;
- }
- MyComplexNumber operator-(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber minus(lhs);
- minus.real -= rhs.real;
- minus.imaginary -= rhs.imaginary;
- return minus;
- }
- MyComplexNumber operator*(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber multiply(lhs);
- multiply.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
- multiply.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
- return multiply;
- }
- MyComplexNumber operator/(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber divide(lhs);
- double squareSum = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary;
- divide.real = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / squareSum;
- divide.imaginary = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / squareSum;
- return divide;
- }
- MyComplexNumber x;
- #include <string>
- #include <sstream>
- #include "MyComplexNumber.h"
- using namespace std;
- string DoubleToString(double val) {
- stringstream ss;
- ss << val;
- return ss.str();
- }
- MyComplexNumber::MyComplexNumber() {
- real = 0;
- imaginary = 0;
- }
- MyComplexNumber::MyComplexNumber(double r, double i) {
- real = r;
- imaginary = i;
- }
- MyComplexNumber::MyComplexNumber(const MyComplexNumber &other) {
- real = other.getReal();
- imaginary = other.getImaginary();
- }
- string MyComplexNumber::toString() const {
- string s = "";
- s += DoubleToString(real);
- s += " ";
- if (imaginary > 0) {
- s += "+ " + DoubleToString(imaginary) + "i";
- } else if (imaginary < 0) {
- s += "- " + DoubleToString(-imaginary) + "i";
- }
- return s;
- }
- double MyComplexNumber::getReal() const {
- return real;
- }
- double MyComplexNumber::getImaginary() const {
- return imaginary;
- }
- MyComplexNumber operator+(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber add(lhs);
- add.real += rhs.real;
- add.imaginary += rhs.imaginary;
- return add;
- }
- MyComplexNumber operator-(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber minus(lhs);
- minus.real -= rhs.real;
- minus.imaginary -= rhs.imaginary;
- return minus;
- }
- MyComplexNumber operator*(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber multiply(lhs);
- multiply.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
- multiply.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
- return multiply;
- }
- MyComplexNumber operator/(const MyComplexNumber& lhs, const MyComplexNumber& rhs) {
- MyComplexNumber divide(lhs);
- double squareSum = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary;
- divide.real = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / squareSum;
- divide.imaginary = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / squareSum;
- return divide;
- }
- MyComplexNumber MyComplexNumber::operator=(const MyComplexNumber& rhs) {
- MyComplexNumber res(rhs.getReal(), rhs.getImaginary());
- return res;
- }
- #include <iostream>
- #include "MyComplexNumber.h"
- using namespace std;
- int main(int argc, char** argv) {
- MyComplexNumber a(2, 3), b(4, -5);
- cout << a.toString() << endl;
- cout << b.toString() << endl;
- //Test +
- MyComplexNumber c = a + b;
- cout << "(" << a.toString() << ") + ";
- cout << "(" << b.toString() << ") = ";
- cout << c.toString() << endl;
- //Test -
- MyComplexNumber d = a - b;
- cout << "(" << a.toString() << ") - ";
- cout << "(" << b.toString() << ") = ";
- cout << d.toString() << endl;
- //Test *
- MyComplexNumber e = a * b;
- cout << "(" << a.toString() << ") * ";
- cout << "(" << b.toString() << ") = ";
- cout << e.toString() << endl;
- //Test /
- MyComplexNumber f = a / b;
- cout << "(" << a.toString() << ") / ";
- cout << "(" << b.toString() << ") = ";
- cout << f.toString() << endl;
- return 0;
- }
Add new comment
- 236 views