#include<iostream> using namespace std; class Complex { private: double real; double image; public: Complex() { real = 0; image = 0; } Complex(double a, double b) { real = a; image = b; } Complex operator+(const Complex &c) { /*Complex tmp; tmp.real = real + c.real; tmp.image = image + c.image; return tmp;*/ real = real + c.real; image = image + c.image; return *this; } Complex operator-(const Complex &c)const { Complex tmp; tmp.real = real - c.real; tmp.image = image - c.image; return tmp; } Complex operator*(const Complex &c)const { Complex tmp; tmp.real = real*c.real - image*c.image; tmp.image = image*c.real + real*c.image; return tmp; } void show() { cout << real << "+" << image << "i" << endl; } }; int main() { Complex c1(2, 3); Complex c2(1, 1); Complex c3; c1.show(); c2.show(); (c1 + c2).show(); c3 = c1 + c2; c3.show(); return 0; }
定义一个复数类,通过重载运算符:”+”、”-”和”*”为复数类的成员函数,直接 实现两个复数之间的运算。编写一个完整的程序包括主函数测试。
猜你喜欢
转载自blog.csdn.net/qq_25368751/article/details/80634126
今日推荐
周排行