Advertisement
KuoHsiangYu

C++語言_以類別為藍圖建立物件程式碼

May 19th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using std::cout;
  6. using std::string;
  7.  
  8. class Boy {
  9. public:
  10.  
  11.     Boy() {
  12.         cout << "Boy constructor one\n";
  13.     }
  14.  
  15.     Boy(int n) {
  16.         cout << "Boy constructor two, n = " << n << "\n";
  17.     }
  18.  
  19.     void hello(string name) {
  20.         cout << name << " say hello method.\n";
  21.     }
  22. };
  23.  
  24. int main(int argc, char *argv[]) {
  25.     system("color f0");
  26.  
  27.     Boy boy1;
  28.     boy1.hello("boy1");
  29.     cout << "\n";
  30.  
  31.     Boy boy2(5);
  32.     boy2.hello("boy2");
  33.     cout << "\n";
  34.  
  35.     Boy *boy3 = new Boy;
  36.     boy3->hello("boy3");
  37.     cout << "\n";
  38.  
  39.     Boy *boy4 = new Boy();
  40.     boy4->hello("boy4");
  41.     cout << "\n";
  42.  
  43.     Boy *boy5 = new Boy(16);
  44.     boy5->hello("boy5");
  45.     cout << "\n";
  46.  
  47.     Boy *boy6 = NULL;
  48.     boy6 = new Boy(20);
  49.     boy6->hello("boy6");
  50.     cout << "\n";
  51.  
  52.     cout << "\n";
  53.     system("pause");
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement