输入
输出
1 2 3 4 5 6 int main () { cout << "Hello World" ; cout << "I am learning C++" << endl; return 0 ; }
数据类型
运算符
常量与变量
变量命名的一般规则是:
名称可以包含字母、数字和下划线
名称必须以字母或下划线(_
)开头
名称要区分大小写,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; cout << food << "\n" ; cout << meal << "\n" ; 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; cout << food << "\n" ; cout << &food << "\n" ; cout << ptr << "\n" ; cout << &ptr << "\n" ; *ptr = "Hamburger" ; 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; *p1 = *p2; *p2 = temp; } 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; 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; }
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; }
函数重载
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" ; 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!" ; } }; 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 ; myObj.a = 50 ; 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" ; } }; class Car : public Vehicle{ public : string model = "Mustang" ; }; int main () { Car 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 () { 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 (); }
异常处理