本身選的路,含着淚也要走完。既然選擇了C++,就要堅持下去。ios
高樓想要蓋的高,地基要打的好,學習也是,基礎必定要打好。因此,要時不時的複習一下基本的知識點。(廢話很少說,繫好安全帶,準備起飛。)編程
C++ 是 C 語言的超集, 所以它有了C語言以外的新特性:數組
1. 命名空間 namespace 解決了變量名字衝突的問題。 使用格式 using namespace 命名空間名; 標準命名空間 std;安全
2.引用 & 是一個變量的別名,它不是一種數據類型,使用引用時必須對其初始化。函數
3.對堆空間的操做 new delete new運算符分配的空間在明確指定初始化參數時進行初始化,不然不進行初始化。 用法 指針變量名 = new 類型名(初始化表達式); delete 指針名;學習
4.類 class 由 C語言的結構體演變過來,抽象類型的集合。this
5.this 指針 this指針是C++實現封裝的一種機制,它將對象和該對象的成員函數鏈接在一塊兒spa
6.輸入/輸出 cin cout 以二進制流輸入輸出 PS:之前在杭電的一道題,用cin用作輸入操做,OJ過不來,代碼沒有動,只是把cin 變成 scanf 就過了,我想這是由於cin 轉換乘二進制代碼流效率下降了。debug
7.模版:函數模板,類模板。 template <class T> 此特性增長了代碼的重用性。指針
樂迪加速~~~(小侄子喜歡看《超級飛俠》)
封裝:隱藏對象的屬性和方法,只能經過公開的接口進行訪問。
繼承:子類直接使用父類的屬性和方法。繼承方式 公有繼承,保護繼承,私有繼承
基類成員在派生類中的訪問權限以下:
繼承方式/成員權限 public protected private
public 公有繼承 public protected 不可訪問
private 私有繼承 private private 不可訪問
protected 保護繼承 protected protected 不可訪問
多態:向不一樣的對象發送同一個消息,不一樣的對象會產生不一樣的行爲。
C++ 支持兩種多態形式:
1.編譯時的多態,靜態聯編。是經過重載的方式來實現的。
2.運行時的多態,動態聯編。是經過繼承和虛函數來實現的。
重載:(以[]重載和<< >> 操做符重載爲例)
<< >> 重載:
1 #include <iostream> 2 3 using namespace std; 4 5 class CMoney 6 { 7 public: 8 friend ostream& operator<<(ostream &os,CMoney &m); 9 friend istream& operator>>(istream &is,CMoney &m); 10 CMoney(int m = 0,int c = 0) 11 { 12 Dollar = m; 13 Cents = c; 14 } 15 public: 16 int Dollar; 17 int Cents; 18 }; 19 20 ostream & operator<<(ostream &os,CMoney &m) 21 { 22 os << "$" << m.Dollar << "元\t" << m.Cents << "分" << endl; 23 return os; 24 } 25 istream & operator>>(istream &is,CMoney &m) 26 { 27 is >> m.Dollar >> m.Cents; 28 return is; 29 } 30 int main() 31 { 32 CMoney m; 33 cin >> m; 34 cout << m; 35 return 0; 36 }
[]重載:
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 class Parent 6 { 7 public: 8 Parent(char *s) 9 { 10 this->str = new char [strlen(s) + 1]; 11 strcpy(this->str,s); 12 this->m_len = strlen(s); 13 } 14 Parent(int len) 15 { 16 this->m_len = len; 17 str = new char[this->m_len + 1]; 18 *(str + this->m_len) = '\0'; 19 } 20 ~Parent() 21 { 22 delete str; 23 } 24 char &operator[](int n) 25 { 26 27 static char ch = 0; 28 if (n > m_len) 29 { 30 cout << "數組下標越界" << endl; 31 return ch; 32 } 33 else 34 { 35 return *(str + n); // 返回偏移量 36 37 } 38 } 39 void display() 40 { 41 cout << str << endl; 42 } 43 private: 44 int m_len; 45 char *str; 46 }; 47 48 49 int main() 50 { 51 Parent p1("I love you1very much!"); 52 p1.display(); 53 cout << p1[0] << endl; 54 p1[0] = 't'; 55 Parent p2(10); 56 for (int i = 0;i < 10; ++i) 57 { 58 p2[i] = p1[i]; 59 } 60 p2.display(); 61 return 0; 62 }
模版在之後的STL學習中相當重要,它提升了代碼的重用性。下面以兩個簡單的例子說明,函數模版,和類模版
函數模版:
1 #include <iostream> 2 3 using namespace std; 4 5 template <class T> 6 7 void swapx(T &a, T &b) 8 { 9 T temp; 10 temp = a; 11 a = b; 12 b = temp; 13 } 14 15 int main() 16 { 17 int a,b; 18 char m,n; 19 double x,y; 20 21 cin >> a >> b; 22 cin >> m >> n; 23 cin >> x >> y; 24 25 swapx(a,b); 26 swapx(m,n); 27 swapx(x,y); 28 29 cout << a << " " << b << endl; 30 cout << m << " " << n << endl; 31 cout << x << " " << y << endl; 32 33 return 0; 34 }
類模版:
1 #include <iostream> 2 3 using namespace std; 4 5 struct student 6 { 7 int id; 8 int score; 9 }; 10 template <class T> 11 12 class buffer 13 { 14 private: 15 T a; 16 int em; 17 public: 18 buffer(); 19 T get(); 20 void put(T x); 21 }; 22 23 template <class T> 24 25 buffer <T>::buffer():em(0) 26 { 27 28 } 29 30 template <class T> 31 32 T buffer <T>::get() 33 { 34 if (em == 0) 35 { 36 cout << "em:" << endl; 37 } 38 return a; 39 } 40 template <class T> 41 42 void buffer<T>::put(T x) 43 { 44 em ++; 45 a = x; 46 } 47 48 49 int main() 50 { 51 student s = {541426,99}; 52 53 buffer <int> i1,i2; 54 buffer <student> stu; 55 buffer <double> d; 56 i1.put(13); 57 i2.put(-1101); 58 cout << i1.get() << " " << i2.get() << endl; 59 stu.put(s); 60 int x = stu.get().id; 61 int y = stu.get().score; 62 63 cout << "debug :" << x << endl; 64 cout << "debug :" << y<< endl; 65 66 return 0; 67 }
異常處理模式:
1.終止模式:程序拋出異常後,捕捉異常並退出致使異常的子程序或子系統。 默認狀況下,C++才用這種處理模式
2.恢復模式:異常拋出後,捕捉異常並試圖去糾正或調整引發異常的條件,而後從發生異常的地方繼續執行。
在C++的異常處理中,我的感受比Java的異常處理要難,雖然都是利用try - catch 塊來處理,可是C++更貼近於底層,不像Java那樣將底層的東西封裝好了。
簡單的複習到站了,雖然內容簡單,但有的概念是必需要知道的,我的以爲學一門語言,並非瞭解它的語法和編程法則這麼簡單,而是去真正去理解它的概念和原理。上文若有錯誤的地方,歡迎你們指正。稍後會持續更新C++ STL的學習分享,敬請關注。謝謝~