码迷,mamicode.com
首页 > 其他好文 > 详细

调用成员函数重载运算符

时间:2017-09-11 18:22:48      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:运算符重载

#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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!