- #include <iostream>
- #include <math.h>
- using namespace std;
- class Milk
- {
- public:
-
- int Box;
- int Bottle;
- public:
- Milk(int _Box, int _Bottle) //建構子(CONSTRUCTOR)
- {
- Box = _Box;
- Bottle = _Bottle;
- }
- Milk operator+(const Milk& a) // use member function
- {
- Milk c(0,0);
- c.Bottle = Bottle + a.Bottle;
- c.Box = Box + a.Box;
- return c;
- }
- Milk operator-(const Milk& a) // use member function
- {
- Milk c(0,0);
- c.Bottle = Bottle - a.Bottle;
- c.Box = Box - a.Box;
- return c;
- }
- };
- int main()
- {
- Milk A(2,10);
- Milk B(5,3);
-
- Milk C(0,0);
- C = A + B;
- cout << C.Box << "," <<C.Bottle;
- C = A - B;
- cout << C.Box << "," <<C.Bottle;
- return 0;
- }
複製代碼 |