一、c++简介
1、c++起源
1979年贝尔实验室
2、c++用处
(1)编译器
(2)游戏开发
(3)操作系统
二、开发环境
Linux和Windows均可
Windows编译器:Devc++、VS2019、VC、code::blocks
Linux编译器:记事本、code::blocks
三、语法
- 对象-对象具有状态的行为。对象是类的实例。
- 类-类可以定义为对象行为、状态的模版。
- 方法-从基本上讲,一个方法表示一种行为,一个类可以包含多种方法。
- 变量-变量是计算机语言中能存储计算结果或能表示值的抽象概念。
四、注释
//<-行注释
/*
块注释
*/
五、头文件写法
//#include <头文件名称>
//如:
#include <cstdio>
六、普通语句写法
普通语句一半最后要加分号
七、主函数、函数写法
1、主函数名称:main
2、主函数写法
int main(){
return 0;//必须要加
}
3、其他函数写法
/*
函数类型 函数名(函数变量){
函数内容
返回值
}
*/
//例如:
int plus(int a,int b){
return a + b;
}
八、基础数据类型
数据类型主要分为int、float、double、char、bool、long、long long、long double、string
九、关键字
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | union |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template | / |
/ | / | / | / |
/ | / | / | / |
十、基本输入输出scanf-printf
前面需要加上cstdio/stdio.h头文件
1、scanf输入
注意,除了string类型变量名前都要加&
//scanf("输入/出控制符",输入参数);
//代码示例↓
#include <cstdio>
int main(){
int a,b,c;
scanf("%d",&a);
scanf("%d%d",&b,&c);
return 0;
}
2、printf输出
转义字符请参见这个博客,这里不做介绍
//printf("输入/出控制符",输出参数);
//printf("字符串");
//代码示例↓
#include <cstdio>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d",a);
printf("\n");
printf("hello world!");
printf("\n");
printf("%d + %d = %d",b,c,b + c);
return 0;
}
十一、输入输出-cin/cout
需要加iostream头文件和using namespace std;两行
1、cin输入
//cin >> 变量名;
#include <iosteam>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
printf("%d %d",a,b);
return 0;
}
2、cout输出
//cout << 变量名;
//cout << endl;->输出换行
//cout << "字符串";
#include <iosteam>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
cout << a << " " << b << endl;
cout << "hello world!";
return 0;
}
十二、运算符
- 关系运算符:
== != < > >= <=
- 逻辑运算符:
&& || !
- 位运算符:
& | ^ ~ << >>
- 赋值运算符:
= += -= *= /= %= <<= >>= &= ^= !=
- 杂项运算符:
算术运算符:+ - * / % ++ --
/*
sizeof 返回变量的大小,eg:sizeof(a)返回4 a是整型 sizeof(int)
Condition?X:Y 三元运算符 Condition为true,值为X,否则值为Y
, 逗号表达式,值为最后一个表达式的值
.和-> 用于引用类、结构和公用体的成员
Cast 强制类型转换符 eg:int(2.202)返回2
& 指针运算符 返回变量的地址
* 指针运算符 指向一个变量
*/
十三、条件语句
1、if条件语句
(1)if
/*
if (关系式){
关系式正确时执行
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
if (a % 2 == 0){
cout << a << "是偶数";
}
return 0;
}
(2)else
/*
if (关系式){
关系式正确时执行
} else {
关系值错误时执行
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
if (a % 2 == 0){
cout << a << "是偶数";
} else {
cout << a << "是奇数";
}
return 0;
}
(3)else if
/*
if (1号关系式){
关系式正确时执行
} else if (2号关系式){
1号关系式错误且2号关系式正确时执行
} else {
1号关系式错误且2号关系式错误时执行
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
if (a % 3 == 0){
cout << a << "是3的倍数";
} else if (a % 3 == 1){
cout << a << "除以3的余数是1";
} else {
cout << a << "除以3的余数是2";
}
return 0;
}
2、switch
每个case后满的常量表达式必须各不相同。
case语句和default语句出现的顺序对执行结果没有影响。
若case后没有break,执行完就不会判断,继续执行下一个case语句。直到遇到brerak。
default后面如果没有case,则break可以省略
多个case可以用一组执行语句
/*
switch (表达式){
case 表达式 :
执行内容
……
default :
执行内容
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
switch (a % 3){
case 0 :
cout << a << "是3的倍数";
break;
case 1 :
cout << a << "除以3余1";
break;
default :
cout << a << "除以3余0";
break;
}
return 0;
}
十四、循环语句
(1)for循环
/*
for (自变量名称;条件句;自变量步幅){
当条件句不满足时退出
执行内容
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
for (int i = 1;i <= a;i++){
cout << i << " ";
}
return 0;
}
(2)while循环
/*
while (条件句){
条件句成立时执行
执行语句
}
*/
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
int i = 1;
while (i <= a){
cout << i << " ";
i++;
}
return 0;
}