標題:
20101225 - class類別( 運算子重載 )
[打印本頁]
作者:
buy
時間:
2010-12-25 11:23
標題:
20101225 - class類別( 運算子重載 )
#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&) // use member function
{
}
};
int main()
{
Milk A(2,10);
Milk B(5,3);
Milk C(0,0);
C = A + B;
//cout << C.Box << "," <<C.Bottle;
cout << C;
return 0;
}
複製代碼
作者:
Alen
時間:
2010-12-25 11:34
本帖最後由 Alen 於 2010-12-25 11:47 編輯
#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;
//cout << C;
system("Pause");
return 0;
}
複製代碼
作者:
chuangjoy
時間:
2010-12-25 11:56
#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;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2