30、完善的复数类

1、复数类应该具有的操作
— 运算:+、-、*、/
— 比较:==,!=
— 赋值:=
— 求模:modulus

2、完善的复数类

  • 利用操作符重载
    — 统一复数和实数的运算方式
    — 统一复数和实数的比较方式
	Complex operator +(const Complex& p)
	Complex operator -(const Complex& p)
	Complex operator *(const Complex& p)
	Complex operator /(const Complex& p)
	bool operator ==(const Complex& p)
	bool operator !=(const Complex& p)
	Complex& operator =(const Complex& p)

Complex.h

#ifndef _COMPLEX_H_
#define _COMPLEX_H_
#pragma once
class Complex
{
private:
	double a;
	double b;
public:
	Complex(double a = 0, double b = 0);
	double getA();
	double getB();
	double getModulus();

	Complex operator + (const Complex& p);
	Complex operator - (const Complex& p);
	Complex operator * (const Complex& p);
	Complex operator / (const Complex& p);

	bool operator == (const Complex& p);
	bool operator != (const Complex& p);

	Complex& operator = (const Complex& p);
};
#endif

Complex.cpp

#include "Complex.h"
#include <math.h>
Complex::Complex(double a, double b)
{
	this->a = a;
	this->b = b;
}
double Complex::getA()
{
	return a;
}
double Complex::getB()
{
	return b;
}
double Complex::getModulus()
{
	return sqrt(a*a + b*b);
}
Complex Complex::operator + (const Complex& p)
{
	double pa = a + p.a;
	double pb = b + p.b;
	Complex ret(pa, pb);
	return ret;	
}
Complex Complex::operator - (const Complex& p)
{
	double pa = a - p.a;
	double pb = b - p.b;
	Complex ret(pa, pb);
	return ret;
}
Complex Complex::operator * (const Complex& p)
{
	double pa = a * p.a - b * p.b;
	double pb = a * p.b + b * p.a;
	Complex ret(pa, pb);
	return ret;
}
Complex Complex::operator / (const Complex& p)
{
	double pc = p.a * p.a + p.b * p.b;
	double pa = (a * p.a + b * p.b) / pc;
	double pb = (b * p.a - a * p.b) / pc;
	Complex ret(pa, pb);
	return ret;
}
bool Complex::operator == (const Complex& p)
{
	return (a == p.a) && (b == p.b);
}
bool Complex::operator != (const Complex& p)
{
	return (!(*this == p));
}
Complex& Complex::operator = (const Complex& p)
{
	if (*this != p)
	{
		a = p.a;
		b = p.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 = c1 * c3;
	Complex c5 = c2 / c1;

	printf("c3.a = %lf,c3.b = %lf\n", c3.getA(), c3.getB());
	printf("c4.a = %lf,c4.b = %lf\n", c4.getA(), c4.getB());
	printf("c5.a = %lf,c5.b = %lf\n", c5.getA(), c5.getB());

	Complex c6(2, 4);
	printf("c3 == c6:%d\n", c3 == c6);
	printf("c3 != c4:%d\n", c3 != c4);

	(c3 = c2) = (c1);

	printf("c1.a = %lf,c1.b = %lf\n", c1.getA(), c1.getB());
	printf("c2.a = %lf,c2.b = %lf\n", c2.getA(), c2.getB());
	printf("c3.a = %lf,c3.b = %lf\n", c3.getA(), c3.getB());
	return 0;
}

3、注意事项

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

小结

  • 复数的概念可以通过自定义类实现
  • 复数中的运算操作可以通过操作符重载实现
  • 赋值操作符只能通过成员函数实现
  • 操作符重载的本质为函数定义
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值