一、基础语法
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/n/370869.html