OPERATOR OVERLOAD
(高顯忠, sjgau4311@gmail.com, 2011-04-28 15:42)
1樓
// prog15_1, 運算子「>」多載的範例
#include <iostream>
#include <cstdlib>
using namespace std;
class CWin // 定義視窗類別CWin
{
private:
char id;
int width, height;
public:
CWin(char i,int w,int h):id(i),width(w),height(h) // 建構元
{}
int operator>(CWin &win) // 定義運算子「>」的多載
{
return(this->area() > win.area());
}
int area(void)
{
return width*height;
}
void show(void)
{
printf("\n id= %1c, area= %d \n", id, this->area());
}
};
int main(void)
{
CWin win1('A', 20, 3);
CWin win2('B', 40, 5);
win1.show();
win2.show();
if(win1 > win2) // 判別win1與win2物件之面積的大小
cout << "\n win1 is larger than win2" << endl;
else
cout << "\n win2 is larger than win1" << endl;
system("pause");
return 0;
}