实现一个复数类,实现以下成员函数
//class Complex
//{
//public:
// // 带缺省值的构造函数
// Complex(double real = 0, double image = 0);
// // 析构函数
// ~Complex();
// // 拷贝构造函数
// Complex(const Complex& d);
// // 赋值运算符重载
// Complex& operator= (const Complex& d);
// void Display();
//
//public:
// Complex& operator++();
// Complex operator++(int);
// Complex& operator--();
// Complex operator--(int); //后置--
//
// Complex operator+(const Complex& c);
// Complex operator-(const Complex& c);
//
// Complex& operator-=(const Complex& c);
// Complex& operator+=(const Complex& c);
//
// Complex operator*(const Complex& c);
// Complex operator/(const Complex& c);
//
//private:
// double _real; // 实部
// double _image; // 虚部
//};
//
//void TestComplex()
//{
// Complex c1(2.2, 1.1);
//}
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double real = 0, double image = 0);
~Complex();
Complex(const Complex& d);
Complex& operator++();
Complex operator++(int);
Complex& operator--();
Complex operator--(int); //后置--
Complex& operator= (const Complex& d);
Complex operator+(const Complex& c);
Complex operator-(const Complex& c);
Complex& operator+=(const Complex& c);
Complex& operator-=(const Complex& c);
Complex operator*(const Complex& c);
Complex operator/(const Complex& c);
void Display();
private:
double _real; // 实部
double _image; // 虚部
};
Complex::Complex(double real, double image)
{
cout << "构造函数:" << endl;
_real = real;
_image = image;
}
Complex::~Complex()
{
cout << "析构函数:" << endl;
}
Complex::Complex(const Complex& d)
{
cout << "拷贝构造函数:" << endl;
_real = d._real;
_image = d._image;
}
Complex& Complex::operator= (const Complex& d)
{
cout << "赋值运算符:" << endl;
_real = d._real;
_image = d._image;
return *this;
}
void Complex::Display() //显示
{
cout << _real;
if (_image > 0)
{
cout << "+";
}
cout<< _image << "i" << endl;
}
Complex& Complex::operator++()
{
cout << "前置++:" << endl;
++_real;
++_image;
return *this;
}
Complex Complex::operator++(int)
{
cout << "后置++:" << endl;
Complex tmp(*this);
_real++;
_image++;
return tmp;
}
Complex& Complex::operator--()
{
cout << "前置--:" << endl;
--_real;
--_image;
return *this;
}
Complex Complex::operator--(int)
{
cout << "后置--:" << endl;
Complex tmp(*this);
_real--;
_image--;
return tmp;
}
Complex Complex::operator+(const Complex& c)
{
cout << "加法运算符重载:" << endl;
Complex tmp;
tmp._real = _real + c._real;
tmp._image = _image + c._image;
return tmp;
}
Complex Complex::operator-(const Complex& c)
{
cout << "减法运算符重载:" << endl;
Complex tmp;
tmp._real = _real -c._real;
tmp._image = _image - c._image;
return tmp;
}
Complex& Complex::operator+=(const Complex& c)
{
cout << "+=运算符重载:" << endl;
/*_real = _real + c._real;
_image = _image + c._image;
return *this;*/
return (*this) = (*this + c);
}
Complex& Complex::operator-=(const Complex& c)
{
cout << "-=运算符重载:" << endl;
/*_real = _real - c._real;
_image = _image - c._image;
return *this;*/
return (*this) = (*this - c);
}
Complex Complex::operator*(const Complex& c)
{
cout << "*运算符重载:" << endl;
Complex tmp;
tmp._real = _real*c._real - _image*c._image;
tmp._image = _image*c._real + _real*c._image;
return tmp;
}
Complex Complex::operator/(const Complex& c)
{
cout << "/运算符重载:" << endl;
Complex tmp;
double t = c._real*c._real + c._image*c._image;
tmp._real = (_real*c._real+ _image*c._image)/t;
tmp._image = (_image*c._real - _real*c._image)/t;
return tmp;
}
int main()
{
Complex op1(2.2, 1.1);
Complex op2(1.5, 2.6);
op1.Display();
op1 += op2;
op1.Display();
/*op1.Display();
Complex op2(op1);
op2.Display();
Complex op3;
op3 = op1;
op3.Display();
Complex op4;
op4=op1.operator+(op1);
op4.Display();
Complex op5;
op5 = op4.operator-(op1);
op5.Display();
Complex op6;
Complex op7(5.2, 3.6);
op1 = op1.operator-=(op7);
op1.Display();
Complex op8(2.3, 4.6);
Complex op9(3.6,2.8);
Complex op10;
op10 = op8.operator*(op9);
op10.Display();
op10 = op8.operator/(op9);
op10.Display();*/
//op1 = op1.operator--();
/*op1.Display();
Complex op2;
op2 = op1++;
op2.Display();
op1.Display();*/
system("pause");
return 0;
}
复数类
最新推荐文章于 2022-11-25 16:00:27 发布