重载运算符,在类中的定义

#include<iostream>
using namespace std;//重载运算符,在类中的定义。

class x    //整数类作为例子来说明。
{
private:
    int i;
public:
    x(int a=0){i=a;}  //构造函数

    void show() {cout<<"i="<<i<<endl;}
    x operator+(x p)
    {
        x temp;
        temp.i=i+p.i;
        return temp;

    }
};

int main()
{
    x y(10),z(20); //定义两个x类型的变量。并进行初始化。
    cout<<"y:";
    y.show();
    cout<<"z:";
    z.show();    //显示初始的x,z的i值。


    x t;
    t=y+z;
    cout<<"t:";
    t.show();   //显示y和z的+的值。









    return 0;

}