KuoHsiangYu

巨匠學員_郭翔宇_詢問C++問題.cpp

Dec 8th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  8.  
  9. // 定義視窗類別CWin
  10. class CWin {
  11.    
  12. private:
  13.     char id, *title;
  14.  
  15. public:
  16.     CWin(char i = 'D', char *text = (char *)"Default window") :id(i) {
  17.         title = new char[50];//配置可容納50個字元的記憶空間
  18.         strcpy(title, text);//將text所指向的字串拷貝給title
  19.     }
  20.    
  21.     void set_data(char i, char *text) {
  22.         id = i;
  23.         strcpy(title, text);//將text所指向的字串拷貝給title
  24.     }
  25.    
  26.     void show(void) {
  27.         cout << "Window " << id << ": " << title << endl;
  28.     }
  29.  
  30.     void show_address(void) {
  31.         cout << "Window " << id << ": " << &id << endl;
  32.     }
  33.  
  34.     //解構元
  35.     ~CWin() {
  36.         delete[] title;
  37.         title = NULL;
  38.     }
  39. };
  40.  
  41. int main(int argc, char** argv) {
  42.    
  43.     system("color f0");
  44.    
  45.     CWin win1('A', (char *)"Main window");
  46.     CWin win2;
  47.  
  48.     cout << "win1.show() = ";
  49.     win1.show();
  50.    
  51.     cout << "win2.show() = ";
  52.     win2.show();
  53.    
  54.     cout << "win1.show_address() = ";
  55.     win1.show_address();
  56.    
  57.     cout << "win2.show_address() = ";
  58.     win2.show_address();
  59.  
  60.     win1 = win2; // 設定win1=win2
  61.     cout << endl << "設定 win1 = win2 之後..." << endl;
  62.    
  63.     cout << "win1.show() = ";
  64.     win1.show();
  65.    
  66.     cout << "win2.show() = ";
  67.     win2.show();
  68.    
  69.     cout << "win1.show_address() = ";
  70.     win1.show_address();
  71.    
  72.     cout << "win2.show_address() = ";
  73.     win2.show_address();
  74.  
  75.     win1.set_data('B', (char *)"Hello window");
  76.     cout << endl << "更改 win1 的資料成員之後..." << endl;
  77.    
  78.     cout << "win1.show() = ";
  79.     win1.show();
  80.    
  81.     cout << "win2.show() = ";
  82.     win2.show();
  83.  
  84.     cout << endl;
  85.     system("pause");
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment