运算符重载举例

#include<iostream>//定义三角形类,底和高,然后定义一个重载运算符的定义。
using namespace std;

class triangle
{
private:
    float wide;
    float high;
public:
    void set(float a,float b){wide=a,high=b;};
    float area();
    friend bool operator>(triangle x,triangle y);
};


float triangle::area()
{
    float t;
    t=1.0/2.0*wide*high;
    return t;
}

bool operator>(triangle x,triangle y)
{
    bool m;
    m=x.area()>y.area();
    return m;
}

int main()
{
    triangle h,k;
    h.set(11.2,33.4);
    k.set(2.0,5.8);
    if(h>k) cout<<"h>k area.";
    else cout<<"h not >k.";









    return 0;

}


猜你喜欢

转载自blog.csdn.net/gaocui883/article/details/88372893