返回列表 發帖

20101225 - class類別( 運算子重載 )

  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;

  4. class Milk
  5. {
  6. public:
  7.        
  8.         int Box;
  9.         int Bottle;

  10. public:
  11.         Milk(int _Box, int _Bottle) //建構子(CONSTRUCTOR)
  12.         {
  13.                 Box = _Box;
  14.                 Bottle = _Bottle;
  15.         }

  16.         Milk operator+(const Milk& a) // use member function
  17.         {
  18.                 Milk c(0,0);
  19.                 c.Bottle = Bottle + a.Bottle;
  20.                 c.Box = Box + a.Box;
  21.                 return c;

  22.         }

  23.         Milk operator-(const Milk&) // use member function
  24.         {

  25.         }
  26. };

  27. int main()
  28. {
  29.         Milk A(2,10);
  30.         Milk B(5,3);       
  31.        
  32.         Milk C(0,0);

  33.         C = A + B;

  34.         //cout << C.Box << "," <<C.Bottle;

  35.         cout << C;
  36.         return 0;
  37. }
複製代碼
Mai  買大誠 [E-Mail : mainword@dlinfo.tw, mainword@gmail.com] 手機 : 0911-116194
Sun Certified Java Programmer

DL Info 鼎侖資訊 [886-7-969-0998] 高雄市苓雅區光華一路206號6樓之2

返回列表