Holey_yan

[C++] TeachingExampleWithClass

Oct 8th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. /******************************************/
  2. /** 20151008 Teaching Example With Class **/
  3. /** Maker    : Yan                       **/
  4. /** E-Mail   : [email protected]  **/
  5. /** Date     : 2015/10/08 pm.10:59       **/
  6. /******************************************/
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. // 建立新類別
  12. class TestClass {
  13. private:
  14.     int x1;
  15.     int y1;
  16. public:
  17.     // 建構子
  18.     TestClass(int InitialX1, int InitialY1) {
  19.         cout << "Create TestClass" << endl;
  20.         x1 = InitialX1; y1 = InitialY1;
  21.     }
  22.     // 設定 X
  23.     void inX(int x) { x1 = x; }
  24.     // 設定 Y
  25.     void inY(int y) { y1 = y; }
  26.     // 輸出 X
  27.     int outX() { return x1; }
  28.     // 輸出 Y
  29.     int outY() { return y1; }
  30.     // 解構子
  31.     ~TestClass() { cout << "Delete TestClass" << endl; };
  32.  
  33. };
  34.  
  35. int main(int argc, char const *argv[])
  36. {
  37.     // 建立類別物件, 並給予初始值 (建構子)
  38.     TestClass tmp1(10, 20);
  39.     cout << "X1 = " << tmp1.outX() << ", Y1 = " << tmp1.outY() << endl;
  40.  
  41.     // 指定設定 X Y 值
  42.     tmp1.inX(30);
  43.     tmp1.inY(40);
  44.     cout << "X1 = " << tmp1.outX() << ", Y1 = " << tmp1.outY() << endl;
  45.  
  46.     // 當 return 0 之前會先處理類別解構子
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment