标签:运算符重载
#include <iostream> using namespace std; class Complex { private: int real; int image; public: Complex(int real=0,int image=0):real(real),image(image) { } Complex operator+(const Complex &c) { return Complex(real+c.real,image+c.image); } Complex operator-(const Complex &c) { return Complex(real-c.real,image-c.image); } void show()const { cout<<real; if(image>0) cout<<"+"; cout<<image<<"i"<<endl; } }; int main(int argc, char *argv[]) { Complex a(10,20); Complex b(70,80); Complex c=a+b; Complex d=a-b; a.show(); b.show(); c.show(); d.show(); return 0; }
本文出自 “10628473” 博客,请务必保留此出处http://10638473.blog.51cto.com/10628473/1964386
标签:运算符重载
原文地址:http://10638473.blog.51cto.com/10628473/1964386