C++入門到精通(名師教學·手把手教會)【職座標】_騰訊課堂java
https://ke.qq.com/course/101465#term_id=100105503ios
https://github.com/haotang923/ke.qq.com.cppc++
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 13 14 #define WRONGSQUARE(x) (x * x) 15 #define SQUARE(x) ((x) * (x)) 16 17 inline int max(int a, int b) 18 { 19 return a > b ? a : b; 20 } 21 22 int main() 23 { 24 int a = 55, b = 4; 25 26 int c = max(a ++, b); 27 28 cout << "c = " << c << endl; 29 cout << "a = " << a << endl; 30 31 a = 55; 32 33 int d = MAX(a ++, b); // a ++ > b ? a ++ : b; 34 35 cout << "d = " << d << endl; 36 cout << "a = " << a << endl; 37 38 int e = SQUARE(2 + 3); // ((2 + 3) * (2 + 3)) 39 40 cout << "e = " << e << endl; 41 42 int f = WRONGSQUARE(2 + 3); // (2 + 3 * 2 + 3) 43 44 cout << "f = " << f << endl; 45 46 return 0; 47 }
c = 55 a = 56 d = 56 a = 57 e = 25 f = 11 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 void foo(int i, int j = 5, int k = 10); 13 //void foo(int, int = 5, int = 10); 14 15 int main() 16 { 17 foo(20); 18 foo(20, 30); 19 foo(20, 30, 40); 20 21 return 0; 22 } 23 24 void foo(int i, int j, int k) 25 { 26 cout << i << " " << j << " " << k << endl; 27 }
20 5 10 20 30 10 20 30 40 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 int square(int x) 13 { 14 cout << __FILE__ << " " << __LINE__ << " " << __func__ << endl; // File / Line / Function 15 return x * x; 16 } 17 18 double square(double x) 19 { 20 cout << __FILE__ << " " << __LINE__ << " " << __func__ << endl; // File / Line / Function 21 return x * x; 22 } 23 24 int main() 25 { 26 cout << "square(10)\n" << square(10) << endl; 27 cout << "suqare(1.1)\n" << square(1.1) << endl; 28 29 return 0; 30 }
square(10) /Users/hao/PROJECTS/LeetCode/LeetCode/main.cpp 14 square 100 suqare(1.1) /Users/hao/PROJECTS/LeetCode/LeetCode/main.cpp 20 square 1.21 Program ended with exit code: 0
1 // 2 // Header.h 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/3. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 //常見的C語言頭文件格式 10 #ifndef Header_h 11 #define Header_h 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 // C語言的函數在C++中調用 18 void sample(); 19 20 #ifdef __cplusplus 21 } 22 #endif 23 24 #endif /* Header_h */
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 template <typename T> 13 T fAbs(T x) 14 { 15 return x < 0 ? -x : x; 16 } 17 18 int main() 19 { 20 int n = -5; 21 double d = -5.5; 22 23 cout << fAbs(n) << endl; 24 cout << fAbs(d) << endl; 25 26 return 0; 27 }
5 5.5 Program ended with exit code: 0
1 // 2 // Student.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/11. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef Student_hpp 10 #define Student_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 class Student 16 { 17 public: 18 Student(int id = 0); 19 ~Student(); 20 21 const int getID() const; 22 void setID(int id); 23 const int getScore() const; 24 void setScore(int score); 25 26 private: 27 int m_id; 28 int m_score; 29 }; 30 31 inline const int Student::getID() const 32 { 33 return m_id; 34 } 35 36 inline void Student::setID(int id) 37 { 38 m_id = id; 39 } 40 41 inline const int Student::getScore() const 42 { 43 return m_score; 44 } 45 46 inline void Student::setScore(int score) 47 { 48 m_score = score; 49 } 50 51 #endif /* Student_hpp */
1 // 2 // Student.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/11. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Student.hpp" 10 #include <iostream> 11 using namespace std; 12 13 Student::Student(int id) 14 : m_score(0), m_id(id) 15 { 16 cout << "Student Constructor" << endl; 17 } 18 19 Student::~Student() 20 { 21 cout << "Student destructor" << endl; 22 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include "Student.hpp" 11 using namespace std; 12 13 int main() 14 { 15 // { 16 class Student std(10); 17 18 cout << std.getID() << endl; 19 cout << std.getScore() << endl; 20 // } 21 cout << "return from main()" << endl; 22 23 return 0; 24 }
Student Constructor 10 0 return from main() Student destructor Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <cstdlib> // malloc/free 11 using namespace std; 12 13 class Test 14 { 15 public: 16 Test(int val = 0) 17 : m_val(val) 18 { 19 cout << "Test" << endl; 20 } 21 22 ~Test() 23 { 24 cout << "~Test" << endl; 25 } 26 27 private: 28 int m_val; 29 }; 30 31 int main () 32 { 33 { 34 Test a; // "Test" 35 } // End of scope : "~Test" 36 cout << "end of }" << endl; 37 38 Test *pVal = new Test(); // "Test" 39 delete pVal; // "~Test" 40 pVal = nullptr; 41 42 int *p = (int *)malloc(sizeof(int)); 43 free(p); 44 p = nullptr; 45 46 Test *pArray = new Test[2]; // twice "Test" 47 48 delete[] pArray; // twice call of destructor "~Test" 49 //delete pArray; // memory leak 50 51 pVal = new Test(10); // "Test" 52 delete pVal; // "~Test" 53 54 return 0; 55 }
Test ~Test end of } Test ~Test Test Test ~Test ~Test Test ~Test Program ended with exit code: 0
1 // 2 // person.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/26. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef person_hpp 10 #define person_hpp 11 12 class Person 13 { 14 public: 15 Person(char * pName); 16 ~Person(); 17 /* 18 Person(const Person &s); 19 Person& operator=(const Person &other); 20 */ 21 22 void Print(); 23 24 private: 25 char *name; 26 }; 27 28 29 #endif /* person_hpp */
1 // 2 // person.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/26. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "person.hpp" 10 11 #include <iostream> 12 #include <cstring> 13 using namespace std; 14 15 Person::Person(char *pN) 16 { 17 if (pN != nullptr) { 18 cout << "Constructing " << pN << " --->" << endl; 19 20 int len = strlen(pN) + 1; 21 name = new char[len]; 22 cout << "name = " << static_cast<void *>(name) << "\n" << endl; 23 memset(name, 0, len); 24 strcpy(name, pN); 25 } else { 26 name = nullptr; 27 } 28 } 29 30 Person::~Person() 31 { 32 cout << "Destrcuting Person --->" << endl; 33 34 if (name != nullptr) { 35 Print(); 36 delete [] name; 37 name = nullptr; 38 } 39 } 40 41 void Person::Print() 42 { 43 cout << "pName = " << static_cast<void *>(name) << "\n" << endl; 44 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 #include "person.hpp" 13 14 int main () 15 { 16 Person p("Joe"); 17 Person p3("Tom"); 18 19 Person p2 = p; // 淺拷貝:使用編譯器提供的默認的拷貝構造函數,指向同一塊內存空間。致使析構時出現問題,同一塊內存空間被析構兩次。 20 21 cout << "Print p --->" << endl; 22 p.Print(); 23 24 cout << "Print p2 --->" << endl; 25 p2.Print(); 26 27 return 0; 28 }
// 構造p Constructing Joe ---> name = 0x100429ab0 // 構造p3 Constructing Tom ---> name = 0x100429d80 Print p ---> pName = 0x100429ab0 Print p2 ---> pName = 0x100429ab0 // 析構p2 Destrcuting Person ---> pName = 0x100429ab0 // 析構p3 Destrcuting Person ---> pName = 0x100429d80 // 析構p Destrcuting Person ---> pName = 0x100429ab0 LeetCode(2399,0x100395340) malloc: *** error for object 0x100429ab0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug (lldb)
1 // 2 // person.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/26. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef person_hpp 10 #define person_hpp 11 12 class Person 13 { 14 public: 15 Person(char * pName); 16 ~Person(); 17 Person(const Person &s); 18 Person& operator= (const Person &other); 19 20 void Print(); 21 22 private: 23 char *name; 24 }; 25 26 27 #endif /* person_hpp */
1 // 2 // person.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/26. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "person.hpp" 10 11 #include <iostream> 12 #include <cstring> 13 using namespace std; 14 15 Person::Person(char *pN) 16 { 17 if (pN != nullptr) { 18 cout << "Constructing " << pN << " --->" << endl; 19 20 int len = strlen(pN) + 1; 21 name = new char[len]; 22 cout << "name = " << static_cast<void *>(name) << "\n" << endl; 23 memset(name, 0, len); 24 strcpy(name, pN); 25 } else { 26 name = nullptr; 27 } 28 } 29 30 Person::~Person() 31 { 32 cout << "Destrcuting Person --->" << endl; 33 34 if (name != nullptr) { 35 Print(); 36 delete [] name; 37 name = nullptr; 38 } 39 } 40 41 Person::Person(const Person &p) 42 { 43 cout << "Copy Constructor of Person --->" << endl; 44 45 if (p.name != nullptr) { 46 int len = strlen(p.name) + 1; 47 name = new char[len]; 48 cout << "name = " << static_cast<void *>(name) << "\n" << endl; 49 memset(name, 0, len); 50 strcpy(name, p.name); 51 } else { 52 name = nullptr; 53 } 54 } 55 56 // 注意「operator= ()」須要留一個空格,不然出錯 57 Person& Person::operator= (const Person &other) 58 { 59 cout << "operator= --->\n" << endl; 60 61 // 防止自賦值 62 if (&other == this) { 63 return *this; 64 } 65 66 if (name != nullptr) { 67 delete [] name; 68 name = nullptr; 69 } 70 71 if (other.name != nullptr) { 72 int len = strlen(other.name) + 1; 73 name = new char[len]; 74 memset(name, 0, len); 75 strcpy(name, other.name); 76 } else { 77 name = nullptr; 78 } 79 80 return *this; 81 } 82 83 void Person::Print() 84 { 85 cout << "pName = " << static_cast<void *>(name) << "\n" << endl; 86 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 #include "person.hpp" 13 14 int main () 15 { 16 Person p("Joe"); 17 Person p3("Tom"); 18 19 Person p2 = p; // 深拷貝:自定義拷貝構造函數,指向不一樣內存空間。析構時沒有問題。 20 21 cout << "Print p --->" << endl; 22 p.Print(); 23 24 cout << "Print p2 --->" << endl; 25 p2.Print(); 26 27 p2 = p3; // 重載賦值運算符 28 29 cout << "Print p3 --->" << endl; 30 p3.Print(); 31 32 cout << "Print p2 --->" << endl; 33 p2.Print(); 34 35 return 0; 36 }
// 構造p Constructing Joe ---> name = 0x103105370 // 構造p3 Constructing Tom ---> name = 0x1031053f0 // 構造p2 Copy Constructor of Person ---> name = 0x10310a520 Print p ---> pName = 0x103105370 Print p2 ---> pName = 0x10310a520 // 賦值給p2 operator= ---> Print p3 ---> pName = 0x1031053f0 Print p2 ---> pName = 0x100608e20 // 析構p2 Destrcuting Person ---> pName = 0x100608e20 // 析構p3 Destrcuting Person ---> pName = 0x1031053f0 // 析構p Destrcuting Person ---> pName = 0x103105370 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 class Student 13 { 14 public: 15 Student(int id = 0) 16 : m_id(id) // Must use initialization list to initialize the const member 17 { 18 cout << "Student constructor --->\n" << endl; 19 20 // ERROR : Cannot assign to non-static data member 'm_id' with const-qualified type 'const int' 21 //m_id = id; 22 } 23 24 ~Student() 25 { 26 cout << "Student destructor --->\n" << endl; 27 }; 28 29 Student(Student &other) 30 : m_id(other.m_id) 31 { 32 cout << "Student copy constructor --->\n" << endl; 33 } 34 35 inline int getID() const 36 { 37 return m_id; 38 } 39 40 private: 41 const int m_id; 42 }; 43 44 void foo(Student stu) 45 { 46 cout << __func__ << "\n" << endl; 47 } 48 49 void bar(const Student &stu) 50 { 51 cout << __func__ << "\n" << endl; 52 } 53 54 int main () 55 { 56 const int a = 1; 57 58 // ERROR : Cannot assign to variable 'a' with const-qualified type 'const int' 59 //a = 3; 60 61 int b = 0; 62 63 // const value 64 const int *p = &a; 65 66 p = &b; 67 b = 3; 68 69 cout << "p = " << *p << endl; 70 71 // ERROR : Read-only variable is not assignable 72 //*p = 1; 73 74 // const pointer 75 int * const p2 = &b; 76 77 // ERROR : Cannot assign to variable 'p2' with const-qualified type 'int *const' 78 //p2 = &a; 79 80 *p2 = 2; 81 82 cout << "p2 = " << *p2 << "\n" << endl; 83 84 { 85 Student john(1001); 86 87 cout << "Call foo(Student stu)\n" << endl; 88 89 foo(john); 90 91 cout << "Call bar(const Student &stu)\n" << endl; 92 93 bar(john); 94 } 95 96 cout << "Return from main" << endl; 97 98 return 0; 99 }
p = 3 p2 = 2 // 構造對象 Student constructor ---> Call foo(Student stu) // 值傳遞時對行參調用拷貝構造函數 Student copy constructor ---> foo // 函數調用完畢,對行參調用析構函數 Student destructor ---> // 引用傳遞不會調用構造析構行參 Call bar(const Student &stu) bar // 對象生命週期結束,析構對象 Student destructor ---> Return from main Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 // 類的前置聲明 13 class X; 14 15 class Y 16 { 17 public: 18 void f(X *); // 對前置聲明的類只能使用指針做爲行參,由於在32位系統下,一個指針佔4個字節是固定的,編譯器可預判的 19 // void b(X); // 用類做爲行參則出錯,由於編譯器沒法判斷類的類型及大小 20 21 private: 22 X* pX; 23 }; 24 25 /* 26 // ERROR : Variable has incomplete type 'X' 27 void Y::b(X x) 28 { 29 } 30 */ 31 32 class X 33 { 34 public: 35 void initialize(); 36 void print(); 37 friend void fG(X *, int); // Global friend 38 friend void Y::f(X *); // class member friend 39 friend class Z; // Entire class is a friend 40 friend void h(); // Global friend 41 42 private: 43 int i; 44 }; 45 46 void X::initialize() 47 { 48 i = 0; 49 } 50 51 void X::print() 52 { 53 cout << "i = " << i << "\n" << endl; 54 } 55 56 void fG(X *x, int i) 57 { 58 x->i = i; 59 } 60 61 void Y::f(X *x) 62 { 63 x->i = 47; 64 } 65 66 class Z 67 { 68 public: 69 void initialize(); 70 void g(X *x); 71 72 private: 73 int j; 74 }; 75 76 void Z::initialize() 77 { 78 j = 100; 79 } 80 81 void Z::g(X *x) 82 { 83 x->i += j; 84 } 85 86 void h() 87 { 88 X x; 89 90 x.i = 100; // Direct data manipulation 91 x.print(); 92 } 93 94 int main () 95 { 96 X x; 97 98 x.initialize(); 99 x.print(); 100 101 // friend void fG(X *, int); // Global friend 102 fG(&x, 9); 103 x.print(); 104 105 // friend void Y::f(X *); // class member friend 106 Y y; 107 108 y.f(&x); 109 x.print(); 110 111 // friend class Z; // Entire class is a friend 112 Z z; 113 114 z.initialize(); 115 z.g(&x); 116 x.print(); 117 118 // friend void h(); // Global friend 119 h(); 120 121 return 0; 122 }
i = 0 i = 9 i = 47 i = 147 i = 100 Program ended with exit code: 0
1 // 2 // Increase.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/28. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef Increase_hpp 10 #define Increase_hpp 11 12 class Increase 13 { 14 public: 15 Increase(int val); 16 ~Increase(); 17 18 Increase& operator++ (); // prefix, return reference 19 Increase operator++ (int val); // postfix, return value 20 21 int getVal() const 22 { 23 return m_val; 24 } 25 26 private: 27 int m_val; 28 }; 29 30 #endif /* Increase_hpp */
1 // 2 // Increase.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/28. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Increase.hpp" 10 11 Increase::Increase(int val) 12 : m_val(val) 13 { 14 } 15 16 Increase::~Increase() 17 { 18 } 19 20 Increase& Increase::operator++ () 21 { 22 ++ m_val; 23 return *this; 24 } 25 26 Increase Increase::operator++ (int) 27 { 28 Increase ret(m_val); 29 ++ m_val; 30 31 return ret; 32 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Increase.hpp" 10 11 #include <iostream> 12 using namespace std; 13 14 int main () 15 { 16 Increase val(100); 17 Increase val2 = ++ val; 18 19 cout << "val = " << val.getVal() << "\n" << endl; 20 cout << "val2 = " << val2.getVal() << "\n" << endl; 21 22 Increase val3 = val ++; 23 24 cout << "val3 = " << val3.getVal() << "\n" << endl; 25 cout << "val = " << val.getVal() << "\n" << endl; 26 27 return 0; 28 }
val = 101 val2 = 101 val3 = 101 val = 102 Program ended with exit code: 0
1 // 2 // TString.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/28. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef TString_hpp 10 #define TString_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 namespace T { 16 17 class String 18 { 19 public: 20 String (const char * = nullptr); 21 ~String (); 22 23 String (const String &); 24 25 // 重載賦值運算符 26 27 // String a; a = b; 28 String& operator= (const String &); 29 // String a; a = "hello"; 30 String& operator= (const char *); 31 32 String& operator+= (const String &); 33 String operator+ (const String &) const; 34 35 String& operator+= (const char *); 36 String operator+ (const char *) const; 37 38 inline const char * data() const 39 { 40 return m_data; 41 } 42 43 private: 44 char *m_data; 45 }; 46 47 } 48 49 50 #endif /* TString_hpp */
1 // 2 // TString.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/28. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "TString.hpp" 10 11 #include <iostream> 12 #include <cstring> 13 using namespace std; 14 15 namespace T 16 { 17 18 String::String(const char *str) 19 { 20 if (nullptr == str) { 21 m_data = new char[1]; 22 *m_data = '\0'; 23 } else { 24 int length = strlen(str); 25 m_data = new char[length + 1]; 26 strcpy(m_data, str); 27 } 28 } 29 30 String::~String() 31 { 32 delete [] m_data; 33 } 34 35 String::String(const String & other) 36 { 37 int length = strlen(other.m_data); 38 39 m_data = new char[length + 1]; 40 strcpy(m_data, other.m_data); 41 } 42 43 String& String::operator= (const String &other) 44 { 45 // 檢查自賦值 46 if (this == &other) { 47 return *this; 48 } 49 50 // 釋放原有內存資源 51 delete[] m_data; 52 53 int length = strlen(other.m_data); 54 m_data = new char[length + 1]; 55 strcpy(m_data, other.m_data); 56 57 return *this; 58 } 59 60 String& String::operator= (const char *other) 61 { 62 delete[] m_data; 63 64 if (nullptr == other) { 65 m_data = new char[1]; 66 *m_data = '\0'; 67 } else { 68 int length = strlen(other); 69 m_data = new char[length + 1]; 70 strcpy(m_data, other); 71 } 72 73 return *this; 74 } 75 76 String& String::operator+= (const T::String &other) 77 { 78 char *tmp = m_data; 79 int length = strlen(m_data) + strlen(other.m_data); 80 81 m_data = new char[length + 1]; 82 strcpy(m_data, tmp); 83 strcat(m_data, other.m_data); 84 85 delete [] tmp; 86 87 return *this; 88 } 89 90 String String::operator+ (const T::String &other) const 91 { 92 String result; 93 94 result += *this; 95 result += other; 96 97 return result; 98 } 99 100 String& String::operator+= (const char *other) 101 { 102 String tmp(other); 103 104 *this += tmp; 105 106 return *this; 107 } 108 109 String String::operator+ (const char *other) const 110 { 111 String result = *this; 112 113 result += other; 114 115 return result; 116 } 117 118 } // end of namespace T
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "TString.hpp" 10 11 #include <iostream> 12 using namespace std; 13 14 using namespace T; 15 16 int main () 17 { 18 String s1("hello "); 19 20 String s2 = s1; 21 22 String s3 = "world "; 23 24 cout << "s1 = " << s1.data() << "\n" << endl; 25 26 cout << "s2 = " << s2.data() << "\n" << endl; 27 28 cout << "s3 = " << s3.data() << "\n" << endl; 29 30 // String& operator= (const char *); 31 s1 = "hello world "; 32 33 cout << "s1 = " << s1.data() << "\n" << endl; 34 35 // String& operator= (const String &); 36 s3 = s1; 37 38 cout << "s3 = " << s3.data() << "\n" << endl; 39 40 // String& operator+= (const String &); 41 s1 += s3; 42 43 cout << "s1 = " << s1.data() << "\n" << endl; 44 45 // String& operator+= (const char *); 46 s3 += "!"; 47 48 cout << "s3 = " << s3.data() << "\n" << endl; 49 50 // String operator+ (const String &) const; 51 String s4 = s1 + s2; 52 53 cout << "s4 = " << s4.data() << "\n" << endl; 54 55 // String operator+ (const char *) const; 56 s4 = s1 + "hello "; 57 58 cout << "s4 = " << s4.data() << "\n" << endl; 59 60 return 0; 61 }
s1 = hello s2 = hello s3 = world s1 = hello world s3 = hello world s1 = hello world hello world s3 = hello world ! s4 = hello world hello world hello s4 = hello world hello world hello Program ended with exit code: 0
1 // 2 // Animal.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/29. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef Animal_hpp 10 #define Animal_hpp 11 12 #include <string> 13 using namespace std; 14 15 namespace T 16 { 17 class Animal 18 { 19 public: 20 Animal(int age, string location); 21 ~Animal(); 22 23 void setAge(int age); 24 int getAge() const; 25 26 string getLocation() const; 27 28 protected: 29 void setLocation(string location); 30 31 string m_location; 32 33 private: 34 int m_age; 35 }; 36 37 class Cat : public Animal 38 { 39 public: 40 Cat(int age, int color, string location); 41 ~Cat(); 42 43 int getColor() const; 44 void setColor(int color); 45 46 void setCatLocation(string location); 47 48 private: 49 int m_color; 50 }; 51 52 class Dog : public Animal 53 { 54 public: 55 Dog(int age, int weight, string location); 56 ~Dog(); 57 58 int getWeight() const; 59 void setWeight(int weight); 60 61 private: 62 int m_weight; 63 }; 64 } 65 66 #endif /* Animal_hpp */
1 // 2 // Animal.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/29. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Animal.hpp" 10 11 #include <iostream> 12 using namespace std; 13 14 namespace T 15 { 16 Animal::Animal(int age, string location) 17 : m_age(age), m_location(location) 18 { 19 cout << "Animal constructing --->\n" << endl; 20 } 21 22 Animal::~Animal() 23 { 24 cout << "Animal destructing --->\n" << endl; 25 } 26 27 int Animal::getAge() const 28 { 29 return m_age; 30 } 31 32 void Animal::setAge(int age) 33 { 34 m_age = age; 35 } 36 37 string Animal::getLocation() const 38 { 39 return m_location; 40 } 41 42 void Animal::setLocation(string location) 43 { 44 cout << __func__ << " --->\n" << endl; 45 } 46 47 Cat::Cat(int age, int color, string location) 48 : Animal(age, location), m_color(color) 49 { 50 cout << "Cat constructing --->\n" << endl; 51 } 52 53 Cat::~Cat() 54 { 55 cout << "Cat destructing --->\n" << endl; 56 } 57 58 int Cat::getColor() const 59 { 60 return m_color; 61 } 62 63 void Cat::setColor(int color) 64 { 65 m_color = color; 66 } 67 68 void Cat::setCatLocation(string location) 69 { 70 Animal::setLocation(location); 71 } 72 73 Dog::Dog(int age, int weight, string location) 74 : Animal(age, location), m_weight(weight) 75 { 76 cout << "Dog constructing --->\n" << endl; 77 } 78 79 Dog::~Dog() 80 { 81 cout << "Dog destructing --->\n" << endl; 82 } 83 84 int Dog::getWeight() const 85 { 86 return m_weight; 87 } 88 89 void Dog::setWeight(int weight) 90 { 91 m_weight = weight; 92 } 93 94 } // end of namespace T
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Animal.hpp" 10 11 using namespace T; 12 13 int main () 14 { 15 { 16 Cat cat(1, 1, "house"); 17 cat.setAge(2); 18 cat.setColor(2); 19 20 cat.setCatLocation("apartment"); 21 } 22 23 { 24 Dog dog(2, 2, "home"); 25 dog.setAge(3); 26 dog.setWeight(3); 27 } 28 29 return 0; 30 }
Animal constructing ---> Cat constructing ---> setLocation ---> Cat destructing ---> Animal destructing ---> Animal constructing ---> Dog constructing ---> Dog destructing ---> Animal destructing ---> Program ended with exit code: 0
1 // 2 // Furniture.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/31. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef Furniture_hpp 10 #define Furniture_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 class Furniture 16 { 17 public: 18 Furniture(int weight = 0) 19 : m_weight(weight) 20 { 21 cout << "Furniture constructing --->\n" << endl; 22 } 23 24 void setWeight(int weight) 25 { 26 m_weight = weight; 27 } 28 29 int getWeight() const 30 { 31 return m_weight; 32 } 33 34 private: 35 int m_weight; 36 }; 37 38 // Two classes virtually inheriting Furniture 39 class Sofa : public virtual Furniture 40 { 41 public: 42 Sofa(int weight = 0); 43 44 void watchTV() 45 { 46 cout << __func__ << "\n" << endl; 47 } 48 }; 49 50 class Bed : public virtual Furniture 51 { 52 public: 53 Bed(int weight = 0); 54 55 void sleep() 56 { 57 cout << __func__ << "\n" << endl; 58 } 59 }; 60 61 // 多重繼承 62 class SofaBed : public Sofa, public Bed 63 { 64 public: 65 SofaBed(); 66 67 void foldout() 68 { 69 cout << __func__ << "\n" << endl; 70 } 71 }; 72 73 #endif /* Furniture_hpp */
1 // 2 // Furniture.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/31. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Furniture.hpp" 10 11 // Should not set weight = 0, or else ERROR Redefinition of default argument 12 Sofa::Sofa(int weight) 13 : Furniture(weight) 14 { 15 cout << "Sofa constructing --->\n" << endl; 16 } 17 18 Bed::Bed(int weight) 19 : Furniture(weight) 20 { 21 cout << "Bed constructing --->\n" << endl; 22 } 23 24 SofaBed::SofaBed() 25 { 26 cout << "SofaBed constructing --->\n" << endl; 27 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Furniture.hpp" 10 11 int main () 12 { 13 Furniture furniture; 14 Sofa sofa; 15 Bed bed; 16 SofaBed sofabed; 17 18 cout << "sizeof(Furniture) : " << sizeof(Furniture) << "\n" << endl; 19 cout << "sizeof(Sofa) : " << sizeof(Sofa) << "\n" << endl; 20 cout << "sizeof(Bed) : " << sizeof(Bed) << "\n" << endl; 21 cout << "sizeof(SofaBed) : " << sizeof(SofaBed) << "\n" << endl; 22 23 sofabed.watchTV(); 24 25 sofabed.sleep(); 26 27 sofabed.foldout(); 28 29 return 0; 30 }
Furniture constructing ---> Furniture constructing ---> Sofa constructing ---> Furniture constructing ---> Bed constructing ---> Furniture constructing ---> Sofa constructing ---> Bed constructing ---> SofaBed constructing ---> sizeof(Furniture) : 4 sizeof(Sofa) : 16 sizeof(Bed) : 16 sizeof(SofaBed) : 24 watchTV sleep foldout Program ended with exit code: 0
1 // 2 // Car.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/3. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef Car_hpp 10 #define Car_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 namespace T { 16 17 class Engine { 18 public: 19 Engine(int id) 20 : m_id(id) 21 { 22 cout << "Engine constructing\n" << endl; 23 } 24 25 ~Engine() 26 { 27 cout << "Engine destructing\n" << endl; 28 } 29 30 void start() 31 { 32 cout << "Engine start\n" << endl; 33 } 34 35 void stop() 36 { 37 cout << "Engine stop\n" << endl; 38 } 39 40 private: 41 int m_id; 42 }; 43 44 class Wheel { 45 public: 46 Wheel(int id) 47 : m_id(id) 48 { 49 cout << "Wheel constructing\n" << endl; 50 } 51 52 ~Wheel() 53 { 54 cout << "Wheel destructing\n" << endl; 55 } 56 57 void roll() 58 { 59 cout << "Wheel rolling\n" << endl; 60 } 61 62 private: 63 int m_id; 64 }; 65 66 // class Car : public Engine, public Wheel // 避免使用多繼承 67 class Car { 68 public: 69 Car(Engine *, Wheel *, string, int); 70 71 ~Car() 72 { 73 cout << "Car destructing\n" << endl; 74 }; 75 76 void run(); 77 void stop(); 78 79 private: 80 Car(const Car&); 81 Car& operator= (const Car&); 82 83 Engine *m_engine; 84 Wheel *m_wheel; 85 string m_name; 86 int m_price; 87 }; 88 89 class Stero { 90 public: 91 Stero() 92 { 93 cout << "Stero constructing\n" << endl; 94 } 95 96 ~Stero() 97 { 98 cout << "Stero destructing\n" << endl; 99 } 100 101 void play() 102 { 103 cout << "Stero playing\n" << endl; 104 } 105 }; 106 107 class Benchi : public Car { 108 public: 109 Benchi(Engine *, Wheel *, string, int, Stero *); 110 111 ~Benchi() 112 { 113 cout << "Benchi destructing\n" << endl; 114 } 115 116 void musicOn(); 117 118 private: 119 Stero *m_stero; 120 }; 121 122 class Transformer : public Car { 123 public: 124 Transformer(Engine *, Wheel *, string, int, bool); 125 126 ~Transformer() 127 { 128 cout << "Transformer destructing\n" << endl; 129 } 130 131 void fight(); 132 void transform(); 133 134 private: 135 bool m_val; 136 }; 137 138 } 139 140 #endif /* Car_hpp */
1 // 2 // Car.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/3. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #include "Car.hpp" 10 11 namespace T { 12 Car::Car(Engine *e, Wheel *w, string name, int price) 13 : m_engine(e), m_wheel(w), m_name(name), m_price(price) 14 { 15 cout << "Car constructing\n" << endl; 16 } 17 18 void Car::run() 19 { 20 m_engine->start(); 21 m_wheel->roll(); 22 23 cout << "Car running\n" << endl; 24 } 25 26 void Car::stop() 27 { 28 m_engine->stop(); 29 30 cout << "Car stopping\n" << endl; 31 } 32 33 Benchi::Benchi(Engine *e, Wheel *w, string name, int price, Stero *st) 34 : Car(e, w, name, price), m_stero(st) 35 { 36 cout << "Benchi constructing\n" << endl; 37 } 38 39 void Benchi::musicOn() 40 { 41 Car::run(); 42 m_stero->play(); 43 44 cout << "Music on\n" << endl; 45 } 46 47 Transformer::Transformer(Engine *e, Wheel *w, string name, int price, bool val) 48 :Car(e, w, name, price), m_val(val) 49 { 50 cout << "Transformer constructing\n" << endl; 51 } 52 53 void Transformer::fight() 54 { 55 run(); 56 57 cout << "Transformer fight\n" << endl; 58 } 59 60 void Transformer::transform() 61 { 62 run(); 63 64 cout << "Transformer transform\n" << endl; 65 } 66 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Car.hpp" 10 using namespace T; 11 12 int main () 13 { 14 Engine e1(1); 15 Wheel w1(1); 16 Stero stero; 17 Benchi benchi(&e1, &w1, "benchi", 100, &stero); 18 19 benchi.musicOn(); 20 21 Transformer t(&e1, &w1, "optimusprime", 200, true); 22 23 t.transform(); 24 t.fight(); 25 26 return 0; 27 }
Engine constructing Wheel constructing Stero constructing Car constructing Benchi constructing Engine start Wheel rolling Car running Stero playing Music on Car constructing Transformer constructing Engine start Wheel rolling Car running Transformer transform Engine start Wheel rolling Car running Transformer fight Transformer destructing Car destructing Benchi destructing Car destructing Stero destructing Wheel destructing Engine destructing Program ended with exit code: 0
1 // 2 // Animal.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/4. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef Animal_hpp 10 #define Animal_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 namespace T 16 { 17 class Animal 18 { 19 public: 20 Animal() 21 { 22 cout << "Animal constructing\n" << endl; 23 24 } 25 26 // 爲基類聲明虛析構函數 27 virtual ~Animal() 28 { 29 cout << "Animal destructing\n" << endl; 30 } 31 32 // Need to add "const" to avoid error Member function 'makeSound' not viable: 'this' argument has type 'const T::Animal', but function is not marked const 33 void makeSound() const 34 { 35 cout << "Animal make sound\n" << endl; 36 } 37 38 // "virtual" 只須要在聲明時加上,不須要在定義時加上 39 virtual void smile() const; 40 41 /* 42 virtual void smile() const = 0; // pure virtual function 43 */ 44 45 /* 46 virtual void smile() const 47 { 48 cout << "Animal smile\n" << endl; 49 } 50 */ 51 }; 52 53 class Dog : public Animal 54 { 55 public: 56 Dog() 57 { 58 cout << "Dog constructing\n" << endl; 59 } 60 61 ~Dog() 62 { 63 cout << "Dog destructing\n" << endl; 64 } 65 66 void makeSound() const 67 { 68 cout << "Dog make sound\n" << endl; 69 } 70 71 void smile() const 72 { 73 cout << "Dog smile\n" << endl; 74 } 75 }; 76 77 class Cat : public Animal 78 { 79 public: 80 Cat() 81 { 82 cout << "Cat constructing\n" << endl; 83 } 84 85 ~Cat() 86 { 87 cout << "Cat destructing\n" << endl; 88 } 89 90 void makeSound() const 91 { 92 cout << "Cat make sound\n" << endl; 93 } 94 95 void smile() const 96 { 97 cout << "Cat smile\n" << endl; 98 } 99 }; 100 } 101 102 #endif /* Animal_hpp */
1 // 2 // Animal.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/4. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #include "Animal.hpp" 10 11 // "virtual" 只須要在聲明時加上,不須要在定義時加上 12 void T::Animal::smile() const 13 { 14 cout << "Animal smile\n" << endl; 15 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Animal.hpp" 10 using namespace T; 11 12 void func(const Animal & animal) 13 { 14 cout << "Pass by reference ---> \n" << endl; 15 16 animal.makeSound(); 17 animal.smile(); 18 } 19 20 void foo(Animal * pAnimal) 21 { 22 cout << "Pass by pointer ---> \n" << endl; 23 24 pAnimal->makeSound(); 25 pAnimal->smile(); 26 } 27 28 void bar(Animal animal) 29 { 30 cout << "Pass by value ---> \n" << endl; 31 32 animal.makeSound(); 33 animal.smile(); 34 } 35 36 int main () 37 { 38 { 39 Dog dog; 40 Cat cat; 41 42 cout << "sizeof(Animal) : " << sizeof(Animal) << "\n" << endl; 43 cout << "sizeof(Dog) : " << sizeof(Dog) << "\n" << endl; 44 cout << "sizeof(Cat) : " << sizeof(Cat) << "\n" << endl; 45 46 func(dog); 47 48 func(cat); 49 50 foo(&dog); 51 52 foo(&cat); 53 54 bar(dog); 55 56 bar(cat); 57 } 58 59 cout << "爲多態基類聲明虛析構函數 --->\n" << endl; 60 61 { 62 Animal * pCat = new Cat; 63 64 pCat->makeSound(); 65 pCat->smile(); 66 67 foo(pCat); 68 69 // 若是不聲明爲虛析構函數,則只會調用基類Animal的析構函數,而不會調用派生類Cat的析構函數,形成內存泄漏 70 delete pCat; 71 } 72 73 return 0; 74 }
Animal constructing Dog constructing Animal constructing Cat constructing sizeof(Animal) : 8 sizeof(Dog) : 8 sizeof(Cat) : 8 Pass by reference ---> Animal make sound Dog smile Pass by reference ---> Animal make sound Cat smile Pass by pointer ---> Animal make sound Dog smile Pass by pointer ---> Animal make sound Cat smile Pass by value ---> Animal make sound Animal smile Animal destructing Pass by value ---> Animal make sound Animal smile Animal destructing Cat destructing Animal destructing Dog destructing Animal destructing 爲多態基類聲明虛析構函數 ---> Animal constructing Cat constructing Animal make sound Cat smile Pass by pointer ---> Animal make sound Cat smile // 若是不聲明爲虛析構函數,則只會調用基類Animal的析構函數,而不會調用派生類Cat的析構函數,形成內存泄漏 Cat destructing Animal destructing Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "iostream" 10 using namespace std; 11 12 class Base 13 { 14 public: 15 char Value() { return 'A'; } 16 17 virtual char VirtualValue() { return 'X'; } 18 }; 19 20 class Derived : public Base 21 { 22 public: 23 char Value() { return 'U'; } 24 }; 25 26 class VirtualDerived : virtual public Base 27 { 28 public: 29 char Value() { return 'Z'; } 30 31 char VirtualValue() { return 'V'; } 32 }; 33 34 int main () 35 { 36 Base * p1 = new Derived(); 37 38 Base * p2 = new VirtualDerived(); 39 40 cout << p1->Value() << " " << 41 42 p1->VirtualValue() << " " << 43 44 p2->Value() << " " << 45 46 p2->VirtualValue() << " " << endl; 47 48 return 0; 49 }
A X A V Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 namespace nsT { 13 template <typename T> 14 15 T min(T a, T b) 16 { 17 return (a < b) ? a : b; 18 } 19 } 20 21 int main () 22 { 23 int a = 10, b = 9; 24 int c = min(a, b); 25 26 cout << c << endl; 27 28 double d = 1.0, e = 2.0; 29 double f = min(d, e); 30 31 cout << f << endl; 32 33 return 0; 34 }
1 // 2 // class_template.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/7. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef class_template_hpp 10 #define class_template_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 namespace nsT { 16 class Test { 17 public: 18 Test() {} 19 20 private: 21 // Error : Calling a private constructor of class 'nsT::Test' 22 /* 23 Test(const Test &); 24 Test& operator= (const Test &); 25 */ 26 }; 27 28 template <typename T> 29 class Example { 30 public: 31 Example(T val) 32 : m_val(val) 33 { 34 cout << "Example constructing\n" << endl; 35 } 36 37 ~Example() 38 { 39 cout << "Example destructing\n" << endl; 40 } 41 42 T get() const 43 { 44 return m_val; 45 } 46 47 void set(T val) 48 { 49 m_val = val; 50 } 51 52 private: 53 T m_val; 54 }; 55 } // end of namespace nsT 56 57 #endif /* class_template_hpp */
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 #include "class_template.hpp" 13 14 using namespace nsT; 15 16 int main () 17 { 18 // Using int as data type 19 Example<int> val(1); 20 21 int data = val.get(); 22 23 cout << "data = " << data << "\n" << endl; 24 25 // Using double as data type 26 Example<double> val2(2.0); 27 28 cout << val2.get() << "\n" << endl; 29 30 Test test; 31 32 // Using class as data type 33 Example<Test> val3(test); 34 35 return 0; 36 }
1 // 2 // Stack.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/8. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef Stack_hpp 10 #define Stack_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 namespace nsT { 16 template<typename T> class StackIterator; // 前置聲明 17 18 template<typename T> 19 class Stack 20 { 21 public: 22 Stack() 23 : m_top(0) 24 { 25 cout << "Stack constructing\n" << endl; 26 27 m_array[0] = T(); 28 } 29 30 ~Stack() 31 { 32 cout << "Stack destructing\n" << endl; 33 } 34 35 int size() const 36 { 37 return m_top; 38 } 39 40 void push(const T&); 41 T pop(); 42 friend class StackIterator<T>; // 友元類 43 44 private: 45 enum { SIZE = 100 }; 46 T m_array[SIZE]; 47 int m_top; 48 }; 49 50 template<typename T> 51 void Stack<T>::push(const T& val) 52 { 53 if (m_top < SIZE) { 54 m_array[m_top ++] = val; 55 } 56 } 57 58 template<typename T> 59 T Stack<T>::pop() 60 { 61 if (m_top > 0) 62 return m_array[-- m_top]; 63 else 64 return m_array[0]; 65 } 66 67 template<typename T> 68 class StackIterator { 69 public: 70 StackIterator(Stack<T>& val) 71 : m_stack(val), m_index(0) 72 { 73 cout << "StackIterator constructing\n" << endl; 74 } 75 76 ~StackIterator() 77 { 78 cout << "StackIterator destructing\n" << endl; 79 } 80 81 T& operator++ (int) // post 82 { 83 int ret = m_index; 84 85 if (m_index < m_stack.m_top - 1) 86 m_index ++; 87 88 return m_stack.m_array[ret]; 89 } 90 91 private: 92 Stack<T>& m_stack; 93 int m_index; 94 }; 95 } 96 #endif /* Stack_hpp */
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 using namespace std; 11 12 #include "Stack.hpp" 13 14 using namespace nsT; 15 16 int main () 17 { 18 Stack<int> stack; 19 20 stack.push(1); 21 stack.push(2); 22 stack.push(3); 23 24 auto size = stack.size(); 25 26 cout << "size of stack : " << size << "\n" << endl; 27 28 StackIterator<int> iter(stack); 29 30 for (auto i = 0; i < size; i ++) 31 cout << iter ++ << "\n" << endl; 32 33 cout << "pop : " << stack.pop() << "\n" << endl; 34 35 return 0; 36 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 using namespace std; 12 13 int main() 14 { 15 vector<int> coll; 16 17 cout << "capacity : " << coll.capacity() << "\n" << endl; 18 cout << "size : " << coll.size() << "\n" << endl; 19 20 for (auto i = 0; i < 10; i ++) 21 coll.push_back(i); 22 23 cout << "capacity : " << coll.capacity() << "\n" << endl; 24 cout << "size : " << coll.size() << "\n" << endl; 25 26 cout << coll[9] << endl; 27 28 return 0; 29 }
capacity : 0 size : 0 capacity : 16 size : 10 9 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 using namespace std; 12 13 void print_array(const vector<int> & array) 14 { 15 vector<int>::const_iterator iter; 16 17 cout << "array["; 18 19 for (iter = array.begin(); iter != array.end(); ++ iter) 20 cout << *iter << " , "; 21 22 cout << "]\n" << endl; 23 } 24 25 int main() 26 { 27 vector<int> array; 28 vector<int>::iterator iter; 29 vector<int>::const_iterator citer; 30 31 array.push_back(42); 32 array.push_back(1); 33 array.push_back(100); 34 35 array.pop_back(); 36 37 iter = array.begin(); 38 39 cout << *iter << "\n" << endl; 40 41 *iter = 109; 42 43 citer = array.begin(); 44 45 cout << *citer << "\n" << endl; 46 47 // *citer = 20; // Error : Cannot assign to return value because function 'operator*' returns a const value 48 49 print_array(array); 50 51 return 0; 52 }
42 109 array[109 , 1 , ] Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 using namespace std; 12 13 bool compare(const int & a, const int & b) 14 { 15 return a > b; 16 } 17 18 void print_array(const vector<int> & array) 19 { 20 vector<int>::const_iterator iter; 21 22 cout << "array["; 23 24 for (iter = array.begin(); iter != array.end(); ++ iter) 25 cout << *iter << " , "; 26 27 cout << "]\n" << endl; 28 } 29 30 int main() 31 { 32 vector<int> array; 33 vector<int>::iterator iter; 34 vector<int>::const_iterator citer; 35 36 array.push_back(42); 37 array.push_back(1); 38 array.push_back(100); 39 40 print_array(array); 41 42 sort(array.begin(), array.end()); 43 44 cout << "After sort(array.begin(), array.end())\n" << endl; 45 46 print_array(array); 47 48 sort(array.begin(), array.end(), compare); 49 50 cout << "After sort(array.begin(), array.end(), compare)\n" << endl; 51 52 print_array(array); 53 54 return 0; 55 }
array[42 , 1 , 100 , ] After sort(array.begin(), array.end()) array[1 , 42 , 100 , ] After sort(array.begin(), array.end(), compare) array[100 , 42 , 1 , ] Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 #include <string> 12 using namespace std; 13 14 class ID 15 { 16 public: 17 ID() : m_name(""), m_score(0) {} 18 ID(string name, int score) : m_name(name), m_score(score){} 19 20 string m_name; 21 int m_score; 22 }; 23 24 bool operator== (const ID & x, const ID & y) 25 { 26 return (x.m_name == y.m_name) && (x.m_score == y.m_score); 27 } 28 29 bool operator< (const ID & x, const ID & y) 30 { 31 return x.m_score < y.m_score; 32 } 33 34 bool compare(const ID & x, const ID & y) 35 { 36 return x.m_score > y.m_score; 37 } 38 39 int main() 40 { 41 vector<ID> ids; 42 vector<ID>::iterator iter; 43 44 ids.push_back(ID("Tom", 5)); 45 ids.push_back(ID("John", 1)); 46 ids.push_back(ID("Alex", 2)); 47 48 // By default 49 sort(ids.begin(), ids.end()); 50 51 cout << "After sort(ids.begin(), ids.end())\n" << endl; 52 53 for (iter = ids.begin(); iter != ids.end(); ++ iter) 54 { 55 cout << (*iter).m_name << " : " << (*iter).m_score << endl; 56 } 57 58 cout << endl; 59 60 // With compare function 61 sort(ids.begin(), ids.end(), compare); 62 63 cout << "After sort(ids.begin(), ids.end(), compare)\n" << endl; 64 65 for (iter = ids.begin(); iter != ids.end(); ++ iter) 66 { 67 cout << (*iter).m_name << " : " << (*iter).m_score << endl; 68 } 69 70 return 0; 71 }
After sort(ids.begin(), ids.end()) John : 1 Alex : 2 Tom : 5 After sort(ids.begin(), ids.end(), compare) Tom : 5 Alex : 2 John : 1 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <list> 11 using namespace std; 12 13 bool compare(const int & a, const int & b) 14 { 15 return a > b; 16 } 17 18 void show_list(const list<int> & coll) 19 { 20 list<int>::const_iterator citer; 21 22 for (citer = coll.begin(); citer != coll.end(); ++ citer) 23 cout << *citer << endl; 24 } 25 26 int main() 27 { 28 list<int> coll; 29 30 coll.push_back(10); // 10 31 coll.push_back(11); // 10, 11 32 coll.push_front(12); // 12, 10, 11 33 coll.push_front(9); // 9, 12, 10, 11 34 35 list<int>::iterator iter = coll.begin(); // 9 36 37 coll.erase(iter); // 12, 10, 11 38 39 iter ++; // 12 40 41 coll.erase(iter); // 10, 11 42 43 coll.push_back(2); // 10, 11, 2 44 coll.push_back(1); // 10, 11, 2, 1 45 46 // Sort in ascending order 47 coll.sort(); 48 49 cout << "After coll.sort() : \n" << endl; 50 51 show_list(coll); 52 53 cout << endl; 54 55 // Sort in descending order 56 coll.sort(compare); 57 58 cout << "After coll.sort(compare) : \n" << endl; 59 60 show_list(coll); 61 62 return 0; 63 }
After coll.sort() : 1 2 10 11 After coll.sort(compare) : 11 10 2 1 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <set> 11 using namespace std; 12 13 int main() 14 { 15 set<int> coll; 16 set<int>::iterator iter; 17 18 coll.insert(1); 19 coll.insert(100); 20 coll.insert(90); 21 coll.insert(-1); 22 coll.insert(80); 23 24 for (iter = coll.begin(); iter != coll.end(); ++ iter) { 25 cout << *iter << endl; 26 } 27 28 return 0; 29 }
-1 1 80 90 100 Program ended with exit code: 0
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <map> 11 #include <string> 12 using namespace std; 13 14 class Compare 15 { 16 public: 17 // Overload operator(), must be wtih "const" 18 bool operator() (const int & a, const int & b) const 19 { 20 return a > b; 21 } 22 }; 23 24 int main() 25 { 26 map<int, string> coll; 27 map<int, string>::iterator iter; 28 29 coll.insert(pair<int, string>(9, "Nine")); 30 coll.insert(map<int, string>::value_type(1, "One")); 31 coll.insert(make_pair(2, "Two")); 32 coll[0] = "Zero"; 33 34 cout << "Sort in default ascending order\n" << endl; 35 36 for (iter = coll.begin(); iter != coll.end(); ++ iter) 37 cout << iter->first << " : " << iter->second << endl; 38 39 cout << endl; 40 41 // Sort with overloaded Compare 42 map<int, string, Compare> coll_cmp; 43 map<int, string, Compare>::iterator iter_cmp; 44 45 coll_cmp.insert(pair<int, string>(9, "Nine")); 46 coll_cmp.insert(map<int, string>::value_type(1, "One")); 47 coll_cmp.insert(make_pair(2, "Two")); 48 coll_cmp[0] = "Zero"; 49 50 cout << "Sort in descending order with overloaded Compare operator\n" << endl; 51 52 for (iter_cmp = coll_cmp.begin(); iter_cmp != coll_cmp.end(); ++ iter_cmp) 53 cout << iter_cmp->first << " : " << iter_cmp->second << endl; 54 55 return 0; 56 }
Sort in default ascending order 0 : Zero 1 : One 2 : Two 9 : Nine Sort in descending order with overloaded Compare operator 9 : Nine 2 : Two 1 : One 0 : Zero Program ended with exit code: 0
1 // 2 // RefCount.h 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/14. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef RefCount_h 10 #define RefCount_h 11 12 //#pragma once 13 14 #include <stdio.h> 15 16 #define TRACE printf 17 18 class RefCount 19 { 20 public: 21 RefCount(void) 22 { 23 TRACE("RefCount constructing\n\n"); 24 crefs = 0; 25 } 26 27 virtual ~RefCount(void) 28 { 29 TRACE("RefCount destructing\n\n"); 30 } 31 32 void upcount(void) 33 { 34 ++ crefs; 35 TRACE("up to %d\n\n", crefs); 36 } 37 38 void downcount(void) 39 { 40 if (-- crefs == 0) { 41 TRACE("down to %d\n\n", crefs); 42 delete this; 43 } else { 44 TRACE("down to %d\n\n", crefs); 45 } 46 } 47 48 private: 49 int crefs; 50 }; 51 52 class Sample : public RefCount 53 { 54 public: 55 Sample() 56 { 57 TRACE("Sample constructing\n\n"); 58 } 59 60 ~Sample() 61 { 62 TRACE("Sample destructing\n\n"); 63 } 64 65 void doSomething(void) 66 { 67 printf("Did something\n\n"); 68 } 69 }; 70 71 #endif /* RefCount_h */
1 // 2 // SmartPtr.h 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/14. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef SmartPtr_h 10 #define SmartPtr_h 11 12 #include <stdio.h> 13 14 #define TRACE printf 15 16 /* 17 //Compiler switch 18 #ifdef TRACE_SMPTR 19 #define TRACE printf 20 #else 21 #define TRACE 22 #endif 23 */ 24 25 template <typename T> 26 class SmartPtr 27 { 28 public: 29 SmartPtr(T* p_) : m_p(p_) 30 { 31 TRACE("SmartPtr constructing\n\n"); 32 m_p->upcount(); 33 } 34 35 ~SmartPtr(void) 36 { 37 TRACE("SmartPtr destructing\n\n"); 38 m_p->downcount(); 39 } 40 41 operator T* (void) 42 { 43 TRACE("%s, %d\n\n", __func__, __LINE__); 44 return m_p; 45 } 46 47 T& operator* (void) 48 { 49 TRACE("%s, %d\n\n", __func__, __LINE__); 50 return *m_p; 51 } 52 53 T* operator-> (void) 54 { 55 TRACE("%s, %d\n\n", __func__, __LINE__); 56 return m_p; 57 } 58 59 SmartPtr& operator= (SmartPtr<T> &p_) 60 { 61 TRACE("%s, %d\n\n", __func__, __LINE__); 62 return operator= ((T*)p_); 63 } 64 65 SmartPtr& operator= (T* p_) 66 { 67 TRACE("%s, %d\n\n", __func__, __LINE__); 68 p_->upcount(); 69 m_p->downcount(); 70 m_p = p_; 71 72 return *this; 73 } 74 75 SmartPtr(const SmartPtr<T> &p_) 76 { 77 TRACE("%s, %d\n\n", __func__, __LINE__); 78 m_p = p_.m_p; 79 m_p->upcount(); 80 } 81 82 private: 83 T* m_p; 84 }; 85 86 #endif /* SmartPtr_h */
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "RefCount.h" 10 #include "SmartPtr.h" 11 #include <iostream> 12 using namespace std; 13 14 int main(int argc, char* argv[]) 15 { 16 cout << "SmartPtr<Sample> p = new Sample; --->\n" << endl; 17 18 SmartPtr<Sample> p = new Sample; // Sample* 19 20 cout << "SmartPtr<Sample> p2 = new Sample; --->\n" << endl; 21 22 SmartPtr<Sample> p2 = new Sample; 23 24 cout << "p = p2; --->\n" << endl; 25 26 p = p2; 27 28 cout << "p->doSomething(); --->\n" << endl; 29 30 p->doSomething(); 31 32 cout << "(*p).doSomething(); --->\n" << endl; 33 34 (*p).doSomething(); 35 36 return 0; 37 }
SmartPtr<Sample> p = new Sample; ---> RefCount constructing Sample constructing SmartPtr constructing up to 1 SmartPtr<Sample> p2 = new Sample; ---> RefCount constructing Sample constructing SmartPtr constructing up to 1 p = p2; ---> operator=, 61 operator Sample *, 43 operator=, 67 up to 2 down to 0 Sample destructing RefCount destructing p->doSomething(); ---> operator->, 55 Did something (*p).doSomething(); ---> operator*, 49 Did something SmartPtr destructing down to 1 SmartPtr destructing down to 0 Sample destructing RefCount destructing Program ended with exit code: 0
1 // 2 // Singleton.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/27. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #ifndef Singleton_hpp 10 #define Singleton_hpp 11 12 class Singleton 13 { 14 public: 15 static Singleton* getInstance(); 16 void doSomething(); 17 void destroy(); 18 19 private: 20 Singleton(); 21 ~Singleton(); 22 Singleton(const Singleton &); 23 Singleton& operator=(const Singleton &); 24 25 static Singleton *instance; 26 }; 27 28 #endif /* Singleton_hpp */
1 // 2 // Singleton.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/12/27. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Singleton.hpp" 10 11 #include <iostream> 12 using namespace std; 13 14 // 靜態成員變量須要在類外初始化 15 Singleton* Singleton::instance = nullptr; 16 17 Singleton::Singleton() 18 { 19 cout << "Constructing Singleton instance --->\n" << endl; 20 } 21 22 Singleton::~Singleton() 23 { 24 cout << "Destructing Singleton instance --->\n" << endl; 25 } 26 27 // Not thread safe, 28 // Need to use pthread_mutex_lock/unlock 29 Singleton* Singleton::getInstance() 30 { 31 Singleton* ret = instance; 32 33 if (instance == nullptr) { 34 instance = new Singleton(); 35 ret = instance; 36 } 37 38 return ret; 39 } 40 41 void Singleton::doSomething() 42 { 43 cout << __func__ << " , LINE " << __LINE__ << "\n" << endl; 44 } 45 46 void Singleton::destroy() 47 { 48 delete this; 49 instance = nullptr; 50 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Singleton.hpp" 10 11 #include <iostream> 12 using namespace std; 13 14 int main () 15 { 16 /* 17 Singleton* val = new Singleton(); // ERROR : Calling a private constructor of class 'Singleton' 18 delete val; // ERROR : Calling a private destructor of class 'Singleton' 19 val = nullptr; 20 */ 21 22 Singleton::getInstance()->doSomething(); 23 24 Singleton::getInstance()->destroy(); 25 26 return 0; 27 }
Constructing Singleton instance ---> doSomething , LINE 43 Destructing Singleton instance ---> Program ended with exit code: 0
1 // 2 // Fruit.hpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/5. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #ifndef Fruit_hpp 10 #define Fruit_hpp 11 12 #include <iostream> 13 using namespace std; 14 15 class Fruit 16 { 17 public: 18 virtual ~Fruit() = 0; // 接口類純虛函數的析構函數須要定義 19 virtual void plant() = 0; 20 virtual void grow() = 0; 21 virtual void harvest() = 0; 22 }; 23 24 class Apple : public Fruit 25 { 26 public: 27 Apple(); 28 ~Apple(); 29 30 void plant(); 31 void grow(); 32 void harvest(); 33 }; 34 35 class Grape : public Fruit 36 { 37 public: 38 Grape(); 39 ~Grape(); 40 41 void plant(); 42 void grow(); 43 void harvest(); 44 }; 45 46 enum { 47 APPLE = 0, 48 GRAPE = 1, 49 ORANGE = 2 50 }; 51 52 // 前置聲明 53 class Orange; 54 55 class Gardener 56 { 57 public: 58 Gardener(); 59 ~Gardener(); 60 61 // 0 apple, 1 grape, orange 62 Fruit * getFruit(int); 63 64 private: 65 Apple * m_apple; 66 Grape * m_grape; 67 Orange * m_orange; 68 }; 69 70 class Orange : public Fruit 71 { 72 public: 73 Orange(); 74 ~Orange(); 75 76 void plant(); 77 void grow(); 78 void harvest(); 79 }; 80 81 #endif /* Fruit_hpp */
1 // 2 // Fruit.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2018/1/5. 6 // Copyright © 2018年 Hao. All rights reserved. 7 // 8 9 #include "Fruit.hpp" 10 #include <iostream> 11 using namespace std; 12 13 Fruit::~Fruit() 14 { 15 cout << "Fruit destructing\n" << endl; 16 } 17 18 Apple::Apple() 19 { 20 cout << "Apple constructing\n" << endl; 21 } 22 23 Apple::~Apple() 24 { 25 cout << "Apple destructing\n" << endl; 26 } 27 28 void Apple::plant() 29 { 30 cout << "Apple planting\n" << endl; 31 } 32 33 void Apple::grow() 34 { 35 cout << "Apple growing\n" << endl; 36 } 37 38 void Apple::harvest() 39 { 40 cout << "Apple harvesting\n" << endl; 41 } 42 43 Grape::Grape() 44 { 45 cout << "Grape constructing\n" << endl; 46 } 47 48 Grape::~Grape() 49 { 50 cout << "Grape destructing\n" << endl; 51 } 52 53 void Grape::plant() 54 { 55 cout << "Grape planting\n" << endl; 56 } 57 58 void Grape::grow() 59 { 60 cout << "Grape growing\n" << endl; 61 } 62 63 void Grape::harvest() 64 { 65 cout << "Grape harvesting\n" << endl; 66 } 67 68 Orange::Orange() 69 { 70 cout << "Orange constructing\n" << endl; 71 } 72 73 Orange::~Orange() 74 { 75 cout << "Orange destructing\n" << endl; 76 } 77 78 void Orange::plant() 79 { 80 cout << "Orange planting\n" << endl; 81 } 82 83 void Orange::grow() 84 { 85 cout << "Orange growing\n" << endl; 86 } 87 88 void Orange::harvest() 89 { 90 cout << "Orange harvesting\n" << endl; 91 } 92 93 Gardener::Gardener() 94 { 95 cout << "Gardener constructing\n" << endl; 96 97 m_apple = nullptr; 98 m_grape = nullptr; 99 m_orange = nullptr; 100 } 101 102 Gardener::~Gardener() 103 { 104 cout << "Gardener destructing\n" << endl; 105 106 if (m_apple != nullptr) { 107 delete m_apple; 108 m_apple = nullptr; 109 } 110 111 if (m_grape != nullptr) { 112 delete m_grape; 113 m_grape = nullptr; 114 } 115 116 if (m_orange != nullptr) { 117 delete m_orange; 118 m_orange = nullptr; 119 } 120 } 121 122 Fruit * Gardener::getFruit(int type) 123 { 124 Fruit * fruit = nullptr; 125 126 if (APPLE == type) { 127 if (nullptr == m_apple) 128 m_apple = new Apple(); 129 130 fruit = m_apple; 131 } else if (GRAPE == type) { 132 if (nullptr == m_grape) 133 m_grape = new Grape(); 134 135 fruit = m_grape; 136 } else if (ORANGE == type) { 137 if (nullptr == m_orange) 138 m_orange = new Orange(); 139 140 fruit = m_orange; 141 } 142 143 return fruit; 144 }
1 // 2 // main.cpp 3 // LeetCode 4 // 5 // Created by Hao on 2017/3/16. 6 // Copyright © 2017年 Hao. All rights reserved. 7 // 8 9 #include "Fruit.hpp" 10 11 int main () 12 { 13 Gardener Tom; 14 15 Fruit * fruit = Tom.getFruit(APPLE); 16 17 fruit->plant(); 18 fruit->grow(); 19 fruit->harvest(); 20 21 cout << "------New request for ORANGE------\n" << endl; 22 23 fruit = Tom.getFruit(ORANGE); 24 25 fruit->plant(); 26 fruit->grow(); 27 fruit->harvest(); 28 29 return 0; 30 }
Gardener constructing Apple constructing Apple planting Apple growing Apple harvesting ------New request for ORANGE------ Orange constructing Orange planting Orange growing Orange harvesting Gardener destructing Apple destructing Fruit destructing Orange destructing Fruit destructing Program ended with exit code: 0