一、基礎語法
C++是一種基於對象的編程語言,基礎語法主要包括數據類型、控制語句、函數和指針等。
1. 數據類型:
#include <iostream> using namespace std; int main() { int i = 10; float f = 10.5; double d = 10.56789; char c = 'A'; bool b = true; cout << "Value of i : " << i << endl; cout << "Value of f : " << f << endl; cout << "Value of d : " << d << endl; cout << "Value of c : " << c << endl; cout << "Value of b : " << b << endl; return 0; }
2. 控制語句:
#include <iostream> using namespace std; int main() { int a = 2, b = 3; if(a > b) { cout << "a is greater than b" << endl; } else { cout << "b is greater than a" << endl; } for(int i = 0; i < 5; i++) { cout << "The value of i is: " << i << endl; } return 0; }
3. 函數:
#include <iostream> using namespace std; int add(int x, int y) { return x + y; } int main() { int a = 5, b = 3, c; c = add(a, b); cout << "The sum of a and b is: " << c << endl; return 0; }
4. 指針:
#include <iostream> using namespace std; void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a = 5, b = 3; cout << "Before swapping: a = " << a << " and b = " << b << endl; swap(&a, &b); cout << "After swapping: a = " << a << " and b = " << b << endl; return 0; }
二、面向對象
C++是一種面向對象的編程語言,實現面向對象編程需要掌握類、繼承、多態等概念。
1. 類:
#include <iostream> using namespace std; class Rectangle { public: int length; int width; int getArea() { return length * width; } }; int main() { Rectangle rectangle; rectangle.length = 5; rectangle.width = 3; cout << "The area of rectangle is: " << rectangle.getArea() << endl; return 0; }
2. 繼承:
#include <iostream> using namespace std; class Shape { public: virtual int getArea() = 0; }; class Rectangle : public Shape { public: int length; int width; int getArea() { return length * width; } }; int main() { Rectangle rectangle; rectangle.length = 5; rectangle.width = 3; cout << "The area of rectangle is: " << rectangle.getArea() << endl; return 0; }
3. 多態:
#include <iostream> using namespace std; class Shape { public: virtual int getArea() = 0; }; class Rectangle : public Shape { public: int length; int width; int getArea() { return length * width; } }; class Circle : public Shape { public: int radius; int getArea() { return 3.14 * radius * radius; } }; int main() { Shape *shape; Rectangle rectangle; Circle circle; shape = &rectangle; rectangle.length = 5; rectangle.width = 3; cout << "The area of rectangle is: " <getArea() << endl; shape = &circle; circle.radius = 5; cout << "The area of circle is: " <getArea() << endl; return 0; }
三、異常處理
C++提供了異常處理機制,異常是程序執行期間可能出現的意外情況。
1. catch和throw語句:
#include <iostream> using namespace std; int division(int a, int b) { if(b == 0) { throw "Division by zero!"; } return a / b; } int main() { int a = 10, b = 0; try { int c = division(a, b); cout << "The result of division is: " << c << endl; } catch(const char* msg) { cerr << msg << endl; } return 0; }
2. 自定義異常:
#include <iostream> using namespace std; class MyException : public exception { public: const char* what() const throw() { return "My Exception Occurred"; } }; int division(int a, int b) { if(b == 0) { throw MyException(); } return a / b; } int main() { int a = 10, b = 0; try { int c = division(a, b); cout << "The result of division is: " << c << endl; } catch(exception& e) { cout << e.what() << endl; } return 0; }
四、STL庫
C++的STL庫為程序員提供了很多實用的數據結構和算法。
1. vector容器:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for(int i = 0; i < 5; i++) { v.push_back(i); } cout << "The content of vector is: "; for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } return 0; }
2. map容器:
#include <iostream> #include <map> using namespace std; int main() { map<string, int> m; m["apple"] = 5; m["banana"] = 3; m["cherry"] = 8; cout << "The price of apple is: " << m["apple"] << endl; return 0; }
五、文件處理
C++可以用於文件讀寫操作。
1. 寫文件:
#include <iostream> #include <fstream> using namespace std; int main() { ofstream file("example.txt"); if(file.is_open()) { file << "Hello World!\n"; file << "The value of pi is: " << 3.14 << endl; file.close(); } else { cout << "Unable to open file for writing!" << endl; } return 0; }
2. 讀文件:
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; ifstream file("example.txt"); if(file.is_open()) { while(getline(file, line)) { cout << line << endl; } file.close(); } else { cout << "Unable to open file for reading!" << endl; } return 0; }
以上就是C++手冊的全面詳解,包括基礎語法、面向對象、異常處理、STL庫和文件處理等方面。通過學習這些基本知識,你可以編寫出自己的C++程序,從而實現各種應用。
原創文章,作者:EUTOV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/370869.html