// C9_2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include // stream I/O 程式庫 using namespace std; class Rectangle // 類別定義 { public: // 建構子定義 Rectangle(double ll = 1.0, double ww = 1.0, double hh = 1.0): m_length1(ll), m_width1(ww), m_height1(hh) { cout << endl << "建構子定義已被呼叫。"; } // 函式--計算體積 double Cal_Val() const { // 傳回立方體體積 return m_length1*m_width1*m_height1; } // 覆載 "大於" 運算子 bool operator>(const Rectangle& vBox) const { return this->Cal_Val() > vBox.Cal_Val(); } // 覆載 "大於" 運算子 -- 以值比較 bool operator>(const double& value) const { return this->Cal_Val() > value; } // 解構子定義 ~Rectangle() { cout << " ~Rectangle 解構子定義已被呼叫。" << endl;} private: double m_length1; // 長度,以公分為單位 double m_width1; // 寬度,以公分為單位 double m_height1; // 高度,以公分為單位 }; int operator>(const double& value, const Rectangle& vBox); // 函式原型 int main(int argc, char* argv[]) { Rectangle Box1(4.2, 3.15, 1.9); Rectangle Box2(12.8, 4.3, 2.8); cout << endl << "Box1 = " << Box1.Cal_Val(); cout << endl << "Box2 = " << Box2.Cal_Val(); cout << endl << "-------------------------------------"; if(Box2 > Box1) cout << endl << "Box2 大於 Box1"; if(Box2 > 100.0) cout << endl << "Box2 體積大於 100"; else cout << endl << "Box2 體積不大於 100"; if(20.0 > Box1) cout << endl << "Box1 體積小於 20"; else cout << endl << "Box1 體積不小於 20"; cout << endl; return 0; } // 函式 -- 比較一常數 int operator>(const double& value, const Rectangle& vBox) { return value > vBox.Cal_Val(); }