[C + +] 31 perfect complex class

Keywords: C++ Programming

Perfect complex class

  • Operation that complex class should have

    • Operation: +, -, */
    • Comparison: = ==
    • Assignment: =
    • Modulus: modulus

  • Using operator overloading

    • Unified operation of complex number and real number
    • The comparison of unified complex number and real number

Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
    
bool operator == (const Complex& c);
bool operator != (const Complex& c);
    
Complex& operator = (const Complex& c);

Programming experiment: the realization of complex class

Complex.h

#ifndef _COMPLEX_H_
#define _COMPLEX_H_

class Complex
{
private:
    double a;
    double b;

public:
    Complex(int a = 0, int b = 0);
    int getA();
    int getB();
    int getModulus();
    
    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);
    
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    
    Complex& operator = (const Complex& c);
};

#endif

Complex.cpp

#include "Complex.h"
#include <math.h>

Complex::Complex(int a, int b)
{
    this->a = a;
    this->b = b;
}

int Complex::getA()
{
    return a;
}

int Complex::getB()
{
    return b;
}

int Complex::getModulus()
{
    return sqrt(a * a + b * b);
}

Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.a;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    
    return ret;
}

Complex Complex::operator / (const Complex& c)
{
    double nm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / nm;
    double nb = (b * c.a - a * c.b) / nm;
    Complex ret(na, nb);
    
    return ret;
}

bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}

bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}

// In order to implement circular assignment, return the self reference
Complex& Complex::operator = (const Complex& c)
{
    // Skip if you intend to assign a value to yourself
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }
    
    return *this;
}

main.cpp

#include <stdio.h>
#include "Complex.h"

int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    Complex c3 = c2 - c1;
    Complex c4 = c2 + c1;
    Complex c5 = c2 * c1;
    Complex c6 = c2 / c1;
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    printf("c4.a = %d, c4.b = %d\n", c4.getA(), c4.getB());
    printf("c5.a = %d, c5.b = %d\n", c5.getA(), c5.getB());
    printf("c6.a = %d, c6.b = %d\n", c6.getA(), c6.getB());

    Complex c7(1, 2);
    
    printf("c1 == c7 : %d\n", c1 == c7);
    printf("c2 != c7 : %d\n", c2 != c7);

    (c3 = c2) = c1;
    
    printf("c1.a = %d, c1.b = %d\n", c1.getA(), c1.getB());
    printf("c2.a = %d, c2.b = %d\n", c2.getA(), c2.getB());
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    

    return 0;
}
Output:
c3.a = 2, c3.b = 4
c4.a = 4, c4.b = 7
c5.a = -9, c5.b = 12
c6.a = 3, c6.b = 0
c1 == c7 : 1
c2 != c7 : 1
c1.a = 1, c1.b = 2
c2.a = 3, c2.b = 6
c3.a = 1, c3.b = 2

Matters needing attention

  • C + + specifies that the assignment operator (= =) can only be overloaded as a member function
  • Operator overloading cannot change the priority of the original operator
  • Operators cannot change the number of operands
  • Operators should not change their original semantics

Summary

  • The concept of complex number can be realized by custom class
  • Operators in complex numbers can be implemented by operator overloading
  • Assignment operators can only be implemented through member functions
  • The essence of operator overloading is function definition

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Posted by Intelly XAD on Fri, 06 Dec 2019 13:43:57 -0800