【C++】 31_完善的复数类

27次阅读

共计 2454 个字符,预计需要花费 7 分钟才能阅读完成。

完善的复数类

复数类应该具有的操作

运算:+, -, *, /
比较:==, !=
赋值:=
求模:modulus

利用操作符重载

统一复数与实数的运算方式
统一复数与实数的比较方式

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);
编程实验:复数类的实现
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);
}

// 为了实现循环赋值,将自身引用返回
Complex& Complex::operator = (const Complex& c)
{
// 若意图自己给自己赋值,则跳过
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;
}
输出:
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

注意事项

C++ 规定赋值操作符(==)只能重载为成员函数
操作符重载不能改变原操作符的优先级
操作符不能改变操作数的个数
操作符不应改变操作符的原有语义

小结

复数的概念可以通过自定义类实现
复数中的运算操作符可以通过操作符重载实现
赋值操作符只能通过成员函数实现
操作符重载的本质为函数定义

以上内容参考狄泰软件学院系列课程,请大家保护原创!

正文完
 0