实现复数模板类

#include<iostream>
using namespace std;

template<typename T>
class Complex {
   
public:
	//借助函数的默认参数,可以普通构造函数和“转换构造函数”
	//精简为以下带默认参数的构造函数
	Complex(T a=0, T b=0) :m_real(a), m_imag(b){
   };
	//拷贝构造函数
	Complex(const Complex& c);

	//析构函数
	~Complex() {
   };
	
	//基本的成员函数
	T getReal() const {
    return m_real; }  
	void setReal(T r) {
    m_real = r; }    

	T getImag() const {
    return m_imag; }
	void setImag(T i) {
    m_imag = i; }

	//赋值运算符
	Complex& operator=(const Complex& b);

public:
	//类型转换函数,将当前类型转换为其它类型
	//没有必要明确指明返回值的类型,因为类型转换函数
	//要转换的目标类型为double,所以返回值类型也为double类型
	//相当于隐式地指明了返回值类型
	operator double()const {
    return m_real; }

	//以成员函数的形式定义
	Complex& operator+=(const Complex& a);
	Complex& operator-=(const Complex& a);
	Complex& operator*=(const Complex& a);
	Complex& operator/=(const Complex& a);

	//两种不同的实现方式,在没有“=”运算符时,不要返回引用类型
	//方式一
	Complex& operator+(const Complex& a);
	//方式二
	Complex operator-(const Complex& a);
	Complex operator*(const Complex& a);
	Complex operator/(const Complex& a);
	//在没有等于运算符时,不要返回引用类型
	Complex operator--();
	Complex 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值