[C++] Operator Overloading

#include<iostream>
#include<cstdlib>

using namespace std;
class rec
{
    private:
    int width,length,area;
    public:
    rec(int w=1,int l=1)
    {
        width=w;
        length=l;
        area=w*l;
    }
    bool operator > (rec temp_rec)
    {
        if(area>temp_rec.area)
        {
            return true;
        }
        return false;
    }
    bool operator == (rec temp_rec)
    {
        if(area==temp_rec.area)return true;
        return false;
    }
};
int main()
{
    rec a(5,10),b(10,6);
    if(a>b)
    {
        cout<<"a>b\n";
    }
    else if(a==b)
    {
        cout<<"a=b\n";
    }
    else
    {
        cout<<"a<b\n";
    }
    system("pause");
    return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *