Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************/
- /** 20151008 Teaching Example With Class **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2015/10/08 pm.10:59 **/
- /******************************************/
- #include <iostream>
- using namespace std;
- // 建立新類別
- class TestClass {
- private:
- int x1;
- int y1;
- public:
- // 建構子
- TestClass(int InitialX1, int InitialY1) {
- cout << "Create TestClass" << endl;
- x1 = InitialX1; y1 = InitialY1;
- }
- // 設定 X
- void inX(int x) { x1 = x; }
- // 設定 Y
- void inY(int y) { y1 = y; }
- // 輸出 X
- int outX() { return x1; }
- // 輸出 Y
- int outY() { return y1; }
- // 解構子
- ~TestClass() { cout << "Delete TestClass" << endl; };
- };
- int main(int argc, char const *argv[])
- {
- // 建立類別物件, 並給予初始值 (建構子)
- TestClass tmp1(10, 20);
- cout << "X1 = " << tmp1.outX() << ", Y1 = " << tmp1.outY() << endl;
- // 指定設定 X Y 值
- tmp1.inX(30);
- tmp1.inY(40);
- cout << "X1 = " << tmp1.outX() << ", Y1 = " << tmp1.outY() << endl;
- // 當 return 0 之前會先處理類別解構子
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment