语言
1、头部
代码1
#include <iostream> //包含头文件iostream,包含输入输出流
using namespace std; //使用标准命名空间std,包含cout、cin、endl等
int main(){ //主函数,程序从此处开始执行
cout << "Hello World!" << endl; //输出Hello World!
return 0; //返回0,表示程序正常结束
}
代码2
#include <iostream>
int main(){
std::cout << "Hello World!" << std::endl;
return 0;
}
说明
- #include <iostream>
- 符号 #
- 以**#** 开头的被称为预处理(器)指令。所有的预处理(器)指令都是以 # 开头的。
- 只有空格可以出现在预处理(器)指令之前;
- 预处理(器)指令不是 C++的语句,所以不用以分号(;)结尾;
- 预处理指令 include
- include 它需要找到一个文件,然后将该文件的所有内容拷贝到现在的程序内
- 文件名 iostream
- 尖括号 <>
- 符号 #
- using namespace std:观察 代码1 和 代码2
using namespace std是一种命名空间的使用方式,它的作用是用于在当前作用域内引入命名空间std中的所有名称。这样一来,在程序中就可以直接使用std命名空间中的名称,而无需再写std::前缀。std::是命名空间的限定符,它的作用是指定名字的空间范围。例如,std::cout,表示cout是在std命名空间下面的,也就是在std命名空间中的全局作用域下声明的- 区别:
using namespace std是一种简化写法,它将std命名空间中的名称全部导入到当前作用域中,这可能会导致命名冲突问题。而使用std::则可以明确指定名称所在的命名空间,可以避免命名冲突。using namespace std只适用于当前作用域,而std::可以用于任何作用域,包括全局作用域,也就是说,使用std::可以避免一些不必要的重新定义。using namespace std可能会影响程序的可读性和可维护性,建议在头文件中使用std::来明确指定命名空间。- 使用
using namespace std有时会导致名称冲突,特别是在大型程序中,因此不建议滥用。
2、output (输出)
#include <iostream>
using namespace std;
// int main(){
// cout << "Hello World!\n"; // \n回车,输出结果换行
// cout << "I am learning C++";
// return 0;
// }
// int main(){
// cout << "Hello World!\n\n"; // \n回车,输出结果换行
// cout << "I am learning C++";
// return 0;
// }
int main(){
cout << "Hello World!" << "I am learning C++";
cout << "Hello World!" << endl; // endl回车,输出结果换行
return 0;
}
3、comments(注释)
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << endl; // 这是单行注释
cout << "I am learning C++";
/*这是
多行注释*/
return 0;
}
4、基本数据类型、variables变量、const常量、变量命名规则
# include<iostream>
using namespace std;
int main(){
int myNum = 15;
cout << myNum << endl;
///////////////////////////////////////////////////////////////
int myNum1; // 声明变量
myNum1 = 15; // 赋值
cout << myNum1 << endl;
///////////////////////////////////////////////////////////////
int myNum2 = 15;
myNum2 = 10;
cout << myNum2 << endl;
///////////////////////////////////////////////////////////////
int myAge = 35;
cout << "I am " << myAge << " years old." << endl;
///////////////////////////////////////////////////////////////
int x = 5;
int y = 6;
int z = 50;
int sum = x + y + z;
cout << sum << endl;
///////////////////////////////////////////////////////////////
int x1 = 5, y1 = 6, z1 = 50;
cout << x + y + z << endl;
///////////////////////////////////////////////////////////////
const int minutesPerHour = 60; // 常量,一小时60分钟
const float PI = 3.14159; // 常量,圆周率
const int myNum4=15; // 常量,整数
/* myNum4 = 10; */ // 错误,常量不能被修改
///////////////////////////////////////////////////////////////
cout << "type\t" << "value\t" << "size" << endl; // \t是制表符,用于对齐
int myNum5 = 5; // Integer (whole number without decimals)
cout << "int\t" << myNum5 << '\t' << sizeof(myNum5) << endl; // sizeof()是C++的内置函数,用于计算变量的字节数
float myFloatNum = 5.99; // Floating point number (with decimals)
cout << "float\t" << myFloatNum << '\t' << sizeof(myFloatNum) << endl;
double myDoubleNum = 9.98; // Floating point number (with decimals)
cout << "double\t" << myDoubleNum << '\t' << sizeof(myDoubleNum)<< endl;
float f1 = 35e3; // Scientific numbers
double d1 = 12E4;
cout << "float\t" << f1 << '\t' << sizeof(f1) << endl;
cout << "double\t" << d1 << '\t' << sizeof(d1) << endl;
char myLetter = 'D'; // Character
cout << "char\t" << myLetter << '\t' << sizeof(myLetter) << endl;
bool myBoolean = true; // Boolean
cout << "bool\t" << myBoolean << '\t' << sizeof(myBoolean) << endl;
string myText = "Hello"; // String
cout << "string\t" << myText << '\t' << sizeof(myText) << endl;
/* 字符串 */
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName << endl;
fullName = firstName + " " + lastName;
cout << fullName << endl;
fullName = firstName.append(lastName);
cout << fullName << endl;
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length() << endl;
cout << "The length of the txt string is: " << txt.size() << endl;
string myString = "Hello";
cout << myString[0] << endl;
myString[0] = 'J';
cout << myString << endl;
return 0;
}
输出:
15
15
10
I am 35 years old.
61
61
type value size
int 5 4
float 5.99 4
double 9.98 8
float 35000 4
double 120000 8
char D 1
bool 1 1
string Hello 32
John Doe
John Doe
John Doe
The length of the txt string is: 26
The length of the txt string is: 26
H
Jello
5、input(输入)
#include<iostream>
using namespace std;
int main(){
int x,y;
int sum;
cout << "请输入一个整数:";
cin >> x;
cout << "请输入另一个整数:";
cin >> y;
sum = x + y;
cout << "两个整数的和是:" << sum << endl;
return 0;
}
6、Operators(运算)
#include <iostream>
using namespace std;
int main(){
int x = 5;
int y = 3;
cout << x + y << endl; // 8
cout << x - y << endl; // 2
cout << x * y << endl; // 15
cout << x / y << endl; // 1
cout << x % y << endl; // 2
cout << x++ << endl; // 5 先执行cout,再执行x = x + 1
cout << x-- << endl; // 6
cout << ++x << endl; // 6 先执行x = x + 1,再执行cout
cout << --x << endl; // 5
cout << y++ << endl; // 3
cout << y-- << endl; // 4
cout << ++y << endl; // 4
cout << --y << endl; // 3
int z = 5;
z +=3;
cout << "z += 3: " << z << endl; // 8
z = 5;
z -=3;
cout << "z -= 3: " << z << endl; // 2
z = 5;
z *=3;
cout << "z *= 3: " << z << endl; // 15
z = 5;
z /=3;
cout << "z /= 3: " << z << endl; // 1
int a = 5;
int b = 3;
cout << (a == b) << endl; // 0 返回的是布尔型
cout << (a != b) << endl; // 1
cout << (a > b) << endl; // 1
cout << (a < b) << endl; // 0
cout << (a >= b) << endl; // 1
cout << (a <= b) << endl; // 0
/* 与或非 */
cout << (true && false) << endl; // 0
cout << (true || false) << endl; // 1
cout << (!true) << endl; // 0
return 0;
}
7、Math(算数 cmath库)
#include<iostream>
#include<cmath>
using namespace std;
int main(){
cout << max(5, 10) << endl;
cout << min(5, 10) << endl;
cout << sqrt(64) << endl;
cout << round(2.6) << endl;
cout << log(2) << endl;
cout << "abs(x)"
<< "Returns the absolute value of x" << abs(-5) << endl;
cout << "acos(x)"
<< "Returns the arccosine of x" << acos(0.64) << endl;
// ...
return 0;
}
8、Condition(条件语句 if/else if/else、三目运算、Switch)
#include<iostream>
#include<cmath>
using namespace std;
int main(){
if (20 > 18){
cout << "20 is greater than 18" << endl;
}
int time = 20;
if (time < 18){
cout << "Good day." << endl;
} else {
cout << "Good evening." << endl;
}
if (time < 10){
cout << "Good morning." << endl;
} else if (time < 20){
cout << "Good day." << endl;
} else {
cout << "Good evening." << endl;
}
////////////////简单写法:三目运算///////////////////////////
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result << endl;
////////////////switch///////////////////////////
int day = 4;
switch (day){
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Looking forward to the Weekend" << endl;
}
return 0;
}
9、Loop(循环语句 while、do...while、for)
#include<iostream>
using namespace std;
int main(){
int i = 0;
while (i < 5){
cout << i << endl;
i++;
}
i = 0;
do {
cout << i << endl;
i++;
} while (i < 5);
for (int i = 0; i < 5; i++){
cout << i << endl;
}
for (int i = 0; i < 10; i++){
if (i == 4){
continue;
}
cout << i << endl;
}
return 0;
}
10、Array(数组)
#include<iostream>
#include<string>
using namespace std;
int main(){
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"}; // array of strings
string bands[] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"}; // size of array is 5
int myNum[3] = {10, 20, 30}; // array of integers
cout << cars[0] << endl;
for (int i = 0; i < 4; i++){
cout << i << ": " << cars[i] << endl;
}
return 0;
}
11、Reference(引用)和 Pointer(指针)
11.1、定义和性质(本质区别)
- 指针:一个变量,存储的是一个地址,指向内存的一个存储单元;
- 引用:原变量的一个别名,跟原来的变量实质上是同一个东西
#include <iostream>
using namespace std;
int main(){
/* 下面面定义了一个整型变量 a,p 是一个指针变量,p 的值是变量 a 的地址; */
/* 而引用 r,是 a 的一个别名,在内存中 r 和 a 占有同一个存储单元。*/
int a = 996; //
int* p = &a; // p是指针, &在此是求地址运算
int& r = a; // r是引用, &在此起标识作用
/* 指针的各关系 */
cout << &a << endl; // 0x7ffedc9cbfbc
cout << p << endl; // 0x7ffedc9cbfbc
cout << *p << endl; // 996
cout << &*p << endl;// 0x7ffedc9cbfbc
/* 引用的各关系 */
cout << &r << endl;// 0x7ffedc9cbfbc
cout << r << endl;// 996
return 0;
}
11.2、指针可以有多级,引用只能是一级
int **p; // 合法
int &&a; // 不合法
11.3、指针可以在定义的时候不初始化,引用必须在定义的时候初始化
int *p; // 合法
int &r; // 不合法
int a = 996;
int &r = a; // 合法
11.4、指针可以指向NULL,引用不可以为NULL
int *p = NULL; // 合法
int &r = NULL; // 不合法
5、指针初始化之后可以再改变,引用不可以
int a = 996;
int *p = &a; // 初始化, p 是 a 的地址
int &r = a; // 初始化, r 是 a 的引用
int b = 885;
p = &b; // 合法, p 更改为 b 的地址
r = b; // 不合法, r 不可以再变
11.6、sizeof 的运算结果不同
int a = 996;
int *p = &a;
int &r = a;
cout << sizeof(p); // 返回 int* 类型的大小 在64位机器上大小为8个字节
cout << sizeof(r); // 返回 int 类型的大小 在64位机器上大小为4个字节
11.7、自增运算意义不同
int a = 996;
int *p = &a;
int &r = a;
p++;
r++;
11.8、指针和引用作为函数参数时,指针需要检查是否为空,引用不需要
void fun_p(int *p)
{
// 需要检查P是否为空
if (p == NULL)
{
// do something
}
}
void fun_r(int &r)
{
// 不需要检查r
// do something
}
12、Function(函数)
#include<iostream>
using namespace std;
// 函数声明
void myFunction();
// 函数定义
void myFunction()
{
cout << "I just got executed!" << endl;
}
/* 函数重载 */
void myFunction(string fname){
cout<< fname << " Refsnes\n" << endl;
}
void myFunction(string fname,int age){
cout<<fname<<" Refsnes. "<<age<<" years old. \n" << endl;
}
// 参数传递默认值
int myFunction(int x, int y=1){
return x + y;
}
// 主函数
int main()
{
// 函数调用
myFunction();
myFunction("rm");
int z= myFunction(1);
int y = myFunction(1,2);
cout << z << y << endl;
return 0;
}
12.1值传递、引用传递、指针传递
#include<iostream>
using namespace std;
/* 值传递 */
void swap1(int x, int y){
int temp = x;
x = y;
y = temp;
}
/* 引用传递 */
void swap2(int &x, int &y){
int temp = x;
x = y;
y = temp;
}
/* 指针传递 */
void swap3(int* x, int* y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int first = 10;
int second = 20;
cout << "Before swap1: " << first << " " << second << endl;
// Before swap1: 10 20
swap1(first, second);
cout << "After swap1: " << first << " " << second << endl;
//After swap1: 10 20
swap2(first, second);
cout << "After swap2: " << first << " " << second << endl;
// After swap2: 20 10
int* f=&first;
int* s=&second;
swap3(f, s);
cout << "After swap3: " << first << " " << second << endl;
// After swap3: 10 20
return 0;
}
12 Class(类)
#include<iostream>
using namespace std;
class Employee {
int b; // private by default,默认是私有的
public: // public access specifier,公有访问说明符
int x; // public attribute,公有属性
int y;
string brand; // Attribute,属性
string model; // Attribute,属性
int year; // Attribute,属性
int salary;
// Setter,功能:设置salary的值,避免直接访问私有变量
void setSalary(int s) {
salary = s;
}
// Getter,功能:获取salary的值
int getSalary() {
return salary;
}
Employee(string x, string y, int z); // Constructor declaration,构造函数声明
private: // private access specifier,私有访问说明符
int a;
int salary;
};
// Constructor definition outside the class,构造函数定义在类外
Employee::Employee(string x, string y, int z) {
brand = x;
model = y;
year = z;
};
int main(){
Employee emp;
MyClass myObj;
myObj.x = 25;
myObj.y = 50;
// myObj.b = 100;
// myobj.a = 200;
cout<<myObj.x<<endl; // allowed (public)
cout<<myObj.y<<endl; // allowed (public)
// cout<<myObj.b<<endl; // not allowed (private)
// cout<<myObj.a<<endl; // not allowed (private)
emp.setSalary(50000);
cout << emp.getSalary();
return 0;
}