鱼皮 C++ 学习路线一条龙!
发布人:shili8
发布时间:2025-01-31 02:02
阅读次数:0
**鱼皮 C++ 学习路线一条龙!**
作为一名初学者或是想深入学习C++的朋友,你可能会感到迷惑,不知道从哪里开始。鱼皮将带你一步一步地走完一个完整的C++学习路线,涵盖基础知识、数据结构和算法、面向对象编程等方面。
### 一、基础知识####1. C++基本语法* **变量声明**: `int a;` 或 `int a =10;`
* **运算符**:
* 算术运算符:`+ - * / % << >> == != <= >= < >`
* 赋值运算符:`= += -= *= /= %= <<= >>= &= ^= |=`
* 逻辑运算符:`&& || !`
* 位运算符:`& | ^ ~ << >>`
* **控制结构**:
* 条件语句:`if (条件) {代码}`
* 循环语句:`for (初始化;条件;增量) {代码}` 或 `while (条件) {代码}`
* **函数**:
* 函数声明:`return-type function-name(parameters)`
* 函数调用:`function-name(arguments)`
####2. C++数据类型* **整型**:`int short long`
* **浮点型**:`float double long double`
* **字符型**:`char`
* **布尔型**:`bool`
* **指针型**:`int * p;` 或 `char * str;`
####3. C++运算符重载
cpp#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
// 构造函数 Person(string n, int a) : name(n), age(a) {}
// 运算符重载 friend ostream & operator<<(ostream & os, const Person & p) {
os << "Name: " << p.name << ", Age: " << p.age;
return os;
}
};
int main() {
Person person("John",30);
cout << person; // Output: Name: John, Age:30 return0;
}
### 二、数据结构####1. 数组
cpp#include <iostream>
using namespace std;
int main() {
int arr[5] = {1,2,3,4,5};
for (int i =0; i < 5; i++) {
cout << arr[i] << " ";
}
return0;
}
####2. 链表
cpp#include <iostream>
using namespace std;
class Node {
public:
int data;
Node * next;
// 构造函数 Node(int d) : data(d), next(nullptr) {}
};
int main() {
Node head(1);
Node second(2);
Node third(3);
head.next = &second;
second.next = &third;
Node * temp = &head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
return0;
}
####3. 栈和队列
cpp#include <iostream>
using namespace std;
class Stack {
private:
int * arr;
int top;
public:
// 构造函数 Stack(int size) : arr(new int[size]), top(-1) {}
// 入栈 void push(int data) {
if (top == size -1) {
cout << "Stack is full!" << endl;
return;
}
arr[++top] = data;
}
// 出栈 int pop() {
if (top == -1) {
cout << "Stack is empty!" << endl;
return -1;
}
return arr[top--];
}
};
int main() {
Stack stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
while (true) {
int data = stack.pop();
if (data == -1) break;
cout << data << " ";
}
return0;
}
### 三、面向对象编程####1. 类和对象
cpp#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
// 构造函数 Person(string n, int a) : name(n), age(a) {}
// getter string getName() { return name; }
// setter void setName(string n) { name = n; }
};
int main() {
Person person("John",30);
cout << "Name: " << person.getName() << ", Age: " << person.age << endl;
person.setName("Jane");
cout << "Name: " << person.getName() << ", Age: " << person.age << endl;
return0;
}
####2. 继承
cpp#include <iostream>
using namespace std;
class Animal {
public:
string name;
// 构造函数 Animal(string n) : name(n) {}
void sound() { cout << "The animal makes a sound." << endl; }
};
class Dog : public Animal {
public:
// 构造函数 Dog(string n) : Animal(n) {}
void bark() { cout << "The dog barks." << endl; }
};
int main() {
Dog dog("Max");
dog.sound(); // Output: The animal makes a sound.
dog.bark(); // Output: The dog barks.
return0;
}
####3. 多态
cpp#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() =0; // pure virtual function};
class Circle : public Shape {
private:
int radius;
public:
// 构造函数 Circle(int r) : radius(r) {}
void draw() override { cout << "Drawing a circle with radius " << radius << endl; }
};
class Rectangle : public Shape {
private:
int width;
int height;
public:
// 构造函数 Rectangle(int w, int h) : width(w), height(h) {}
void draw() override { cout << "Drawing a rectangle with width " << width << " and height " << height << endl; }
};
int main() {
Shape * shape = new Circle(5);
shape->draw(); // Output: Drawing a circle with radius5 shape = new Rectangle(3,4);
shape->draw(); // Output: Drawing a rectangle with width3 and height4 return0;
}
### 四、异常处理
cpp#include <iostream>
using namespace std;
class MyException : public exception {
public:
const char * what() const throw() { return "My custom exception"; }
};
int main() {
try {
// code that might throw an exception int x =5 /0;
} catch (const MyException & e) {
cout << "Caught my custom exception: " << e.what() << endl;
}
return0;
}
### 五、线程
cpp#include <iostream>
using namespace std;
void printHello() {
for (int i =0; i < 5; i++) {
cout << "Hello ";
}
}
void printWorld() {
for (int i =0; i < 5; i++) {
cout << "World ";
}
}
int main() {
// create threads thread t1(printHello);
thread t2(printWorld);
// join threads t1.join();
t2.join();
return0;
}
### 六、智能指针
cpp#include <iostream>
using namespace std;
class MySmartPointer {
private:
int * ptr;
public:
// 构造函数 MySmartPointer(int value) : ptr(new int(value)) {}
// getter int getValue() { return *ptr; }
// setter void setValue(int value) { *ptr = value; }
};
int main() {
MySmartPointer smartPtr(10);
cout << "Value: " << smartPtr.getValue() << endl;
return0;
}
### 七、容器
cpp#include <iostream>
using namespace std;
void printVector(const vector<int> & vec) {
for (const auto & element : vec) {
cout << element << " ";
}
}
int main() {
// create a vector vector<int> vec = {1,2,3,4,5};
// print the vector printVector(vec); // Output:12345 return0;
}
### 八、算法
cpp#include <iostream> using namespace std; void printSortedArray(const vector

