复数类的实现中包含了众多C++的基础知识,对学习C++有很大的帮助。其中,运算符重载中的“前置++、–和后置++、–”稍微难些,需注意理解它的参数设计和返回值。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Complex
{
private:
double _real;
double _image;
public://四个默认成员函数
//构造函数
Complex(double real = 0.0, double image = 0.0)
:_real(real), _image(image)
{
}
//拷贝构造函数
Complex(const Complex& c)
{
_real = c._real;
_image = c._image;
}
//析构函数
~Complex()
{
}
//赋值运算符的重载
Complex operator = (const Complex& c)
{
this->_real = c._real;
this->_image = c._image;
return *this;
}
public://复数“==”、“+”、“-”、“+=”、“-=”的情况
//判断相等
bool operator == (const Complex& d)
{
return ((_real == d._real) && (_image == d._image));
}
//两个复数相加
Complex operator+(const Complex& d)
{
Complex tmp;
tmp._real = _real + d._real;
tmp._image = _image + d._image;
return tmp;
}
//两个复数相减
Complex operator-(const Complex& d)
{
Complex tmp;
tmp._real = _real - d._real;
tmp._image = _image - d._image;
return tmp;
}
//复数对象-=d
Complex& operator+=(const Complex& d)
{
_real += d._real;
_image += d._image;
return *this;
}
//复数对象-=d
Complex& operator-=(const Complex& d)
{
_real -= d._real;
_image -= d._image;
return *this;
}
public://复数“前置++、--”和“后置++、--”
//前置++
/*Complex operator++()
{
++_real;
++_image;
return *this;
}*/
Complex& operator++()
{
this->_real++;
this->_image++;
return *this;
}
//前置--
/*Complex operator--()
{
--_real;
--_image;
return *this;
}*/
Complex& operator--()
{
this->_real--;
this->_image--;
return *this;
}
//后置++
Complex operator++(int)
{
Complex tmp = *this;
_real++;
_image++;
return tmp;
}
//后置--
Complex operator--(int)
{
Complex tmp = *this;
_real--;
_image--;
return tmp;
}
//打印复数
void Display()
{
cout << _real << "+" << _image << "i" << endl;
}
};
//测试函数
void Test1()
{
cout << "初始化一个复数类" << endl;
Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1
c1.Display();
Complex c2(c1);//调用拷贝构造函数,新建复数类c2
c2.Display();
cout << endl;
}
void Test2()
{
cout << "初始化一个复数类" << endl;
Complex c1(1.0, 2.0);//调用构造函数,新建复数类c1
c1.Display();
Complex c2(c1);//调用拷贝构造函数,新建复数类c2
c2.Display();
//测试operator==
cout << endl << "测试operator==" << endl;
int ret = c1 == c2;
if (ret)
{
cout << "c1 == c2" << endl;
}
else
{
cout << "c1 != c2" << endl;
}
//测试operator+
cout << endl << "测试operator+" << endl;
Complex c3 = c1 + c2;
c3.Display();
//测试operator-
cout << endl << "测试operator-" << endl;
Complex c4 = c1 - c2;
c4.Display();
//测试opertor+=
cout << endl << "测试operator+=" << endl;
c1 += c2;
c1.Display();
//测试opertor-=
cout << endl << "测试operator-=" << endl;
c1.operator-= (c2);
c1.Display();
cout << endl;
}
//测试前置++、--和后置++、--
void Test3()
{
cout << "初始化一个复数类" << endl;
Complex c1(1.0, 2.0);
c1.Display();
//前置++
cout << endl << "测试前置++" << endl;
Complex c2 = ++c1;
c2.Display();
//前置--
cout << endl << "测试前置--" << endl;
Complex c3 = --c1;
c3.Display();
c1.Display();
//后置++
cout << endl << "测试后置++" << endl;
Complex c4 = c1++;
c4.Display();
c1.Display();
//后置--
cout << endl << "测试后置--" << endl;
Complex c5 = c1--;
c5.Display();
c1.Display();
cout << endl;
}
int main()
{
Test1();
Test2();
Test3();
system("pause");
return 0;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18212.html