运算符重载(复数类(Complex))
2017-12-06 20:23
113 查看
环境:win10,vs2013
复数运算法则
运算后结果仍然是一个复数
Complex.h
Complex.cpp
test.cpp
运行结果:
复数运算法则
运算后结果仍然是一个复数
1.加法 两个复数的和依然是复数,它的实部是原来两个复数实部的和,它的虚部是原来两个复数虚部的和 z1=a+bi,z2=c+di 它们的和是:(a+bi)+(c+di)=(a+c)+(b+d)i 2.减法 z1=a+bi,z2=c+di 它们的差是:(a+bi)-(c+di)=(a-c)+(b-d)i 3.乘法 z1=a+bi,z2=c+di 它们的积是:(a+bi)(c+di)=(ac-bd)+(bc+ad)i 4.除法 z1=a+bi,z2=c+di 它们的商是:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c^2+d^2)
Complex.h
using namespace std; #include<stdlib.h> #include<iostream> class Complex { public : Complex();//默认构造函数 Complex(double real, double image);//构造函数(构造对象) //赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用 Complex& operator=(const Complex&d);//赋值操作符重载 bool operator==(const Complex&c);//重载== bool operator!=(const Complex&c);//重载!= Complex(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);//重载+= Complex operator-=(const Complex&c);//重载-= Complex operator*=(const Complex&c);//重载*= Complex operator/=(const Complex&c);//重载/= Complex operator++(int c);//后置++ Complex& operator++();//前置++ Complex operator--(int c);//后置-- Complex& operator--();//前置-- void Print(); private: double _real; double _image; friend ostream&operator<<(ostream&_cout, Complex&c);//输出运算符重载 };
Complex.cpp
#include"Complex.h" void Complex::Print() { cout << _real << "+" & bb62 lt;< _image << "i" << endl; } ostream&operator<<(ostream&_cout, Complex&c)//输出运算符重载 { _cout << c._real << "+" <<c._image << "i"; return _cout; } Complex::Complex()//默认构造函数 { _real = 1.3; _image = 1.4; } Complex::Complex(double real=0.1, double image=0.2) :_real(real) , _image(image) {} Complex::Complex(const Complex&c)//拷贝构造函数 { _real = c._real; _image = c._image; } Complex& Complex::operator=(const Complex&c)//赋值运算符重载 { if (*this != c) { _real = c._real; _image = c._image; } return *this; } bool Complex::operator==(const Complex&c)//重载== { if ((_real == c._real) && (_image = c._image)) return true; return false; } bool Complex::operator!=(const Complex&c)//重载!= { return!(*this == c); } Complex Complex::operator+(const Complex&c)//重载+ { return Complex(_real + c._real, _image + c._image); } Complex Complex::operator*(const Complex&c)//重载* { return Complex( ( (_real*c._real) - (_image*c._image)), ((_image*c._real) + (_real*c._image) ) ); } Complex Complex::operator-(const Complex&c)//重载— { return Complex(_real - c._real, _image - c._image); } Complex Complex::operator+=(const Complex&c)//重载+= { _real = _real+c._real; _image = _image+c._image; return *this; } Complex Complex::operator-=(const Complex&c)//重载-= { _real = _real - c._real; _image = _image -c._image; return *this; } Complex Complex::operator++(int c)//后置++ { Complex temp(*this); _real += 1; _image += 1; return temp; } Complex& Complex::operator++()//前置++ { _real += 1; _image += 1; return *this; } Complex Complex::operator--(int c)//后置—— { Complex temp(*this); _real -= 1; _image -= 1; return temp; } Complex& Complex::operator--()//前置-- { _real -= 1; _image -= 1; return *this; }
test.cpp
#include"Complex.h" int main() { Complex c1(1.1, 2.2); c1.Print(); Complex c2(3.3, 4.4); c2.Print(); Complex c3; c3.Print(); c3 = c2; cout << c3 << endl; cout << (c3 != c1) << endl; cout << (c3 == c1) << endl; cout << c1 + c2 << endl; cout << c1 - c2 << endl; cout << c1 * c2 << endl; cout << endl; Complex c4(5.5, 6.6); c4.Print(); cout << (c1 += c4) << endl; cout << (c1 -= c4) << endl; cout << endl; cout << c1++ << endl; cout << c2-- << endl; cout << ++c3 << endl; cout << --c4 << endl; system("pause"); return 0; }
运行结果:
相关文章推荐
- C++编程:定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载作为Complex的类的成员函数,编程序,求两个复数的和,差,积,商。
- 第八周项目一任务二——实现复数类中的运算符重载(利用类的友元函数)
- 2014-第八周项目一--用成员函数实现复数的运算符重载
- 第八周上机实践项目——实现复数类中的运算符重载(2)
- 第八周(项目一)——实现复数类中的运算符重载(3)。
- 9-1-实现复数的运算符重载
- 第7周-项目1-完整实现复数类中的运算符重载-扩展+、-、*、/运算符的功能
- 第8周项目1.1 类的成员函数实现复数类中的运算符重载
- 第八周 课后实践:项目一——实现复数类中的运算符重载(3)
- complex(x):创建一个复数
- 定义一个复数类Complex,重载运算符"+"
- 第八周任务1:实现复数类中的运算符重载
- 第八周—1.1实现复数中的运算符重载
- 使用运算符重载实现复数的加法运算
- 复数(Complex)类及FFT的C++实现
- 第八周项目一——实现复数类中的运算符重载(3)
- 2014年第九周项目一:复数类中的运算符重载(续)
- OJ第三批——Problem J:C++复数运算符重载(+与<<)
- 用成员运算符重载函数进行复数运算
- 第12周项目一(2)-实现复数中的运算符重载