C++实现复数类

Complex类的成员变量有实部与虚部:

                                       protected: 

                                                       double _real; 

                                                       double _image;

C++中的Complex类的基本函数,包括四个默认成员函数,比较运算符重载函数,赋值运算符重载函数,以及前置++与后置++的重载。

四个默认成员函数是(1)构造函数:在定义对象时初始化成员变量,函数名与类名相同,无返回值(void也不能有),对象实例化时系统自动调用

                                  (2)拷贝构造函数:是一种特殊的构造函数,是对构造函数的重载,用同类对象初始化成员变量

                                  (3)析构函数:函数名是在类名前加~,无参数无返回值,函数作用是做一些清理工作,在对象生命周期结束时系统自动调用

                                  (4)赋值运算符重载:operator+合法的运算符构成运算符重载

代码实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double real = 8.0, double image = 6.0)       //构造函数
:_real(real)
, _image(image)
{
cout <<" Complex(double real, double image)" << endl;
}
Complex(const Complex& d)          //拷贝函数
{
cout << "Complex(const Complex& d)" << endl;
_real = d._real;
_image = d._image;
}
~Complex()       //析构函数
{
cout << "~Complex() " << endl;
_real = 0.0;
_image = 0.0;
}
Complex& operator=(const Complex& d)         //赋值运算符重载
{
cout << "=" << endl;
if (this != &d)
{
_real = d._real;
_image = d._image;


}
return *this;
}
void Display()const                //打印复数
{
cout << "_real:"<< _real;
cout << "      _image:" << _image << endl;
}

bool operator==(const Complex& d)             //==
{
cout << "==" << endl;
return this->_real == d._real
&& this->_image == d._image;
}
bool operator!=(const Complex& d)             //!=
{
cout << "!=" << endl;
return this->_real != d._real
|| this->_image == d._image;
}
//复数只有当虚部为0时,即_image=0时,才可以比较大小,这时比较的是实部即_real的大小
bool operator>(const Complex& d)          //>
{
if (this->_image != 0 || d._image != 0)
{
cout << "无法比较       " ;
return false;
}


else
{
return this->_real > d._real;
}
}
bool operator<(const Complex& d)           //<
{
if (this->_image != 0 || d._image != 0)
{
cout << "无法比较      ";
return false;
}
else
{
return this->_real < d._real;
}
}
bool operator<=(const Complex& d)           //<=
{
if (this->_image != 0 || d._image != 0)
{
cout << "无法比较      ";
return false;
}
else
{
return this->_real <= d._real;
}
}
bool operator>=(const Complex& d)           //>=
{
if (this->_image != 0 || d._image != 0)
{
cout << "无法比较        ";
return false;
}
else
{
return this->_real >= d._real;
}
}
Complex operator+ (const Complex& d)           //+
{
cout << "+" << endl;
Complex ret;
ret._real = (this->_real + d._real);
ret._image = (this->_image + d._image);
return ret;
}
Complex& operator+=(const Complex& d)          //+=
{
cout << "+=" << endl;
this->_real += d._real;
this->_image += d._image;
return *this;
}
Complex& operator++()            //前置++
{
cout << "前置++" << endl;
this->_real += 1;
return *this;
}
Complex operator++(int)         //后置++
{
cout << "后置++" << endl;
Complex tmp(*this);
this->_real += 1;
return tmp;
}




protected:
double _real;
double _image;
};


1、测试用例Test()------测试构造函数,以及==和!=以及+运算符的重载

[csharp]  view plain  copy
  1. void Test()  
  2. {   
  3.     Complex d1;  
  4.     Complex d2(4.0,6.6);  
  5.   
  6.     d1.Display();  
  7.     d2.Display();  
  8.     //d1 = d2;  
  9.     //d1.Display();  
  10.     Complex d3;  
  11.     d3 = d1 + d2;  
  12.     cout << d1.operator==(d2) << endl;  
  13.     cout << d1.operator!=(d2) << endl;  
  14.     d3.Display();  
  15. }  

析构函数是清理工作,当生命周期结束时系统自动调用。

2、测试用例Test1()------------测试+=运算符

[csharp]  view plain  copy
  1. void Test1()  
  2. {  
  3.     Complex d1;  
  4.     Complex d2(4.0, 9.6);  
  5.     d1.Display();  
  6.     d2.Display();  
  7.     d1 += d2;  
  8.     d1.Display();  
  9.     d2.Display();  
  10. }  

3、测试用例Test2()---------测试前置++与后置++

[csharp]  view plain  copy
  1. void Test2()  
  2. {  
  3.     Complex d1;  
  4.     Complex d2(0, 0);  
  5.     d2 = ++d1;  
  6.     d2.Display();  
  7.     d1.Display();  
  8.     Complex d3(1.0, 2.0);  
  9.     d3 = d1++;  
  10.     d3.Display();  
  11.     d1.Display();  
  12.   

4、测试用例Test3()--------测试>/>=/</<=比较运算符

[csharp]  view plain  copy
  1. void Test3()  
  2. {  
  3.     Complex d1(7,0);  
  4.     Complex d2(6, 0);  
  5.     Complex d3;  
  6.     Complex d4(6, 0);  
  7.     cout << d1.operator>(d2) << endl;  
  8.     cout << d1.operator<(d2) << endl;  
  9.     cout << d3.operator<=(d4) << endl;  
  10.     cout << d2.operator>=(d4) << endl;  
  11. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值