输入

输出

1
2
3
4
5
6
int main()
{
cout << "Hello World"; //c-out 输出,相当于printf
cout << "I am learning C++" << endl; //可以同时输出多个,endl是换行
return 0;
}

数据类型

缩写 类型 备注
int 整形

运算符

常量与变量

1
const int myVar  //const常量

变量命名的一般规则是:

  • 名称可以包含字母、数字和下划线
  • 名称必须以字母或下划线(_)开头
  • 名称要区分大小写,myVar和myvar是不同的变量
  • 名称不能包含空格或特殊字符,如#%等。
  • 保留字(如C++关键字,如int不能作为名称使用)
1
2
3
4
5
6
7
8
int main()
{
int x;
cout << "Type a number: ";
cin >> x;
cout << "Your number is: " << x;
return 0;
}

字符串

程序流程结构

选择语句

循环语句

跳转语句

数组

string cars[4] = {"BYD", "Ford", "Mazda", "BWM"}

下标从0开始

指针

取地址运算符 &
& 是一元运算符,返回操作数的内存地址。例如,如果 var 是一个整型变量,则 &var 是它的地址。该运算符与其他一元运算符具有相同的优先级,在运算时它是从右向左顺序进行的。

您可以把 & 运算符读作"取地址运算符",这意味着,&var 读作"var 的地址"。

间接寻址运算符 *
第二个运算符是间接寻址运算符 *,它是 & 运算符的补充。* 是一元运算符,返回操作数所指定地址的变量的值。

请看下面的实例,理解这两种运算符的用法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
string food = "Pizza";
string &meal = food; //取meal的地址指向food的内容\值,类似于快捷方式引用,使得他们都是Pizza

cout << food << "\n"; //输出Pizza
cout << meal << "\n"; //输出Pizza
cout << &food; //输出地址

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
string food = "Pizza";
string *ptr = &food; //ptr里面的值是一个内存地址

cout << food << "\n"; //输出Pizza
cout << &food << "\n"; //输出地址

cout << ptr << "\n"; //输出地址
cout << &ptr << "\n"; //输出地址对应的值

*ptr = "Hamburger"; //对地址所在的值赋值,即*ptr等效于food
cout << *ptr << "\n";

return 0;
}

函数

函数的引用

函数的调用便于功能的复用,例如可以编写一个打印函数,通过传参使之实现打印功能

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

void myFuction()
{
cout << "666";
}

int main()
{
myFuction(); //函数引用
return 0;
}

函数的声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void myFuction();//必须要在最开始先声明函数,函数的实现可以不一定放在最开始

\\主函数放在最后,也就是其他函数需要提前声明
int main()
{
myFuction(); //函数引用
return 0;
}

void myFuction()
{
cout << "666";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

//可以设置默认参数,假如引用函数时没有传参,就会使用默认参数
void myFuction(string fname = "Jack"){
cout << fname << "Refsnes\n";
}

int main()
{
myFuction("Liam"); //函数引用,分别传入三个不同的参数
myFuction("Jenny");
myFuction("Anja");
myFuction();
return 0;
}

函数亦可以设置返回值,调用函数的最终结果就是获得了返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

//可以设置默认参数,假如引用函数时没有传参,就会使用默认参数
void myFuction(int x, int y)
{
return x + y;
}

int main()
{
cout << myFuction(5, 3);
int z = myFuction(5, 3);
cout << z << endl;
return 0;
}

交换函数的实现

  • 传递值进函数内交换
  • 传递指针进函数内交换
  • 用引用也就是别名交换
  • 用定义宏的方式交换
  • 自带的交换函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//方式一:传指针
template<class T>
void swap1(T *p1,T *p2){
T temp = *p1; //temp的值为p1所指向的值
*p1 = *p2; //*p1的值为p2所指向的值
*p2 = temp; //*p2的值为temp所指向的值,即为temp所指向的值p1
}
//测试
int main(){
int a=10;
int b=-23;
swap1(&a,&b); //这就是调用方法的示例
cout<<a<<","<<b;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//方法二,用引用进行交换
template<class T>
void swaps(T &a,T &b){
T temp = a;
a = b;
b = temp;
}
int main(){
int a=-2;
int b=16;
swaps(a,b);
cout<<a<<endl<<b; //现在a为16,b为-2
return 0;
}
1
2
3
4
5
6
7
8
9
//方法三,定义宏
#define SWAP2(a,b,temp)((temp)=(a),(a)=(b),(b)=(temp)) //定义宏
int main(){
int a=15;
int b=-56
int temp;
SWAP2(a,b,temp);
cout<<a<<";"<<b<<endl; //此时的结果为-56;15
}
1
2
3
4
5
6
7
8
//方法四,用自带函数
int main(){
int a,b;
a = 100;
b = -200;
std::swap(a,b);
cout<<a<<endl<<b; //此时b=100;a=-200
}

函数重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

//可以设置默认参数,假如引用函数时没有传参,就会使用默认参数
int myFuction(int x, int y)
{
return x + y;
}

double myFuction(int x, int y)
{
return x - y;
}

int main()
{
cout << "Int: " << myFuction(5, 3);
cout << "Double: " << myFuction(5, 3);

return 0;
}

结构体

类和多态

类时对象的模板,对象是类的一个实例。当某个对象被创建的时候,他就继承了类里的所有特性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

class Car
{
public:
string brand;
string model;
int year;
};

int main()
{
Car carObj1; // 创造了类的对象(实例),只有在创建实例的时候才真正新生成一个内存块,使用了内存空间
carObj1.brand = "BWM"; // 给这个对象的brand属性赋值
carObj1.model = "BWM";
carObj1.year = 1997;

cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year; //输出实例的三个属性,用空格作为间隔
return 0;
}

类里面的函数叫做方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

class Car
{
public:
int speed(int maxSpeed);
void myMethod()
{
cout << "Hello World!";
}
};

//方法speed的实现
int Car::speed(int maxSpeed)
{
return maxSpeed;
}

int main()
{
Car myObj; // 创造了类的对象(实例)
cout << myObj.speed(200) << endl;
myObj.myMethod(); //调用方法(类中的函数)
return 0;
}

构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

class Car
{
public:
string brand;
string model;
int year;
Car(string x, string y, int z);
};

// 构造函数
Car::Car(string x, string y, int z)
{
brand = x;
model = y;
year = z;
}

int main()
{
Car carObj1("BWM", "XS", 1999); // 直接传入三个参数
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year; // 输出实例的三个属性,用空格作为间隔
return 0;
}

访问的权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class MyClass
{
int b;

public:
int x;
int y;

private:
int a;
};

int main()
{
MyClass myObj;
myObj.x = 25; // public的x可以访问
myObj.a = 50; //private的a不能访问,是私有属性
myObj.b = 11;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

class Employee
{
private:
int salary;

public:
void setSalary(int s)
{
salary = s;
}
int getSalary()
{
return salary;
}
};

int main()
{
Employee myObj;
myObj.setSalary(5000);
cout << myObj.getSalary();
}

类的继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

// 父类
class Vehicle
{
public:
string brand = "Ford";
void honk()
{
cout << "Tuut, tuut! \n";
}
};

// 子类,继承父类的属性(特性brand,方法honk)
class Car : public Vehicle
{
public:
string model = "Mustang";
};

int main()
{
Car myCar; // 创建实例myCar
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}

子类可以继承多个父类的特性(多继承)。

父类里的prtected只能被子类访问

多态性

继承让子类能从父类中继承属性和方法,多态性使用这些方法来执行不同的任务,即可以让我们用不同的方法执行一个单一的动作。

例如,有一个叫Animal的基类,他有一个叫做animalSound()的方法。动物的派生类可以是牛马猫狗,而且他们也可以有自己动物声音的不同实现(牛叫、马叫、猫叫、狗叫等)。方法名称相同,但调用的都是自己的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

// 基类
class Animal
{
public:
void animalSound()
{
cout << "The animal makes a sound";
}
};

// 派生类
class Pig : public Animal
{
public:
void animalSound()
{
cout << "The pig says: wee, wee \n";
}
};

// 派生类
class Dog : public Animal
{
public:
void animalSound()
{
cout << "The pig says: bow, bow \n";
}
};

int main()
{
Animal myAnimal;
Pig myPig;
Dog myDog;

myAnimal.animalSound();
myDog.animalSound();
myPig.animalSound();
return 0;
}

文件读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
// 创建txt
ofstream MyFile("filename.txt");
// 写入内容
MyFile << "Flies can be tricky, but it is fun enough!";
// 关闭文件
MyFile.close();

string myText; // 创建一个字符串,用于之后输出文件内容
ifstream MyReadFile("filename.txt"); // 读取文件

// 创建一个循环,用于输出一行一行读取的文件内容
while (getline(MyReadFile, myText))
{
cout << myText;
}
MyReadFile.close();
}

异常处理