一、注释
cpp
/*多行
。。。
。。。
注释*/
//单行注释
#include <iostream>
using namespace std;
int main() {
cout << "hellow" << endl;
system("pause");
return 0;
}
二、变量定义
cpp
#include <iostream>
using namespace std;
int main()
{
int a = 10;//变量定义
cout << "a="<<a << endl;
system("pause");
return 0;
}
三、常量
用于记录程序中不可更改的数据
cpp
#include <iostream>
using namespace std;
//两种定义方式
//define 宏常量,不可修改,一旦修改就会报错
//const 修饰的变量
#define Day 7
int main()
{
Day = 14;
cout << "Day="<<Day << endl;
system("pause");
return 0;
}
cpp
#include <iostream>
using namespace std;
//两种定义方式
//define 宏常量,不可修改,一旦修改就会报错
//const 修饰的变量也是常量,不可修改
#define Day 7
int main()
{
//Day = 14;
cout << "Day="<<Day << endl;
const int mount = 12;
mount = 24
system("pause");
return 0;
}
四、命名规则
- 标识符不可以是关键字
- 标识符由字母,数字,下划线构成
- 标识符第一个字母只能是字母或下划线
- 标识符是能够区分大小写的
五、数据类型
1.整型
cpp
#include <iostream>
using namespace std;
int main()
{
short num1 = 10; //(-32768,32767)
int num2 = 10;
long num3 = 10;
long long num4 = 10;
short num5 = 32769; //打印为-32767
cout << "num1="<<num1 << endl;
cout << "num2="<<num2 << endl;
cout << "num3="<<num3 << endl;
cout << "num4="<<num4 << endl;
cout << "num5="<<num5 << endl;
system("pause");
return 0;
}
2.字符串
cpp
#include <iostream>
using namespace std;
int main()
{
char str[] = "hellow";
cout<<str<<endl;
//包含头文件 #include <string>
string str2 = "nihao";
cout<<str2<<endl;
system("pause");
return 0;
}
3.布尔型
true 真
false 假
cpp
#include <iostream>
using namespace std;
int main()
{
bool flag = true;
cout<<flag<<endl;
flag = false;
cout<<flag<<endl;
system("pause");
return 0;
}
4.浮点型
cpp
#include <iostream>
using namespace std;
int main()
//float 单精度 4字节
//double 双精度 8字节
{
float f1 = 3.1415926f;
cout<<f1<<endl;
double f2 = 3.1415926;
cout<<f2<<endl;
system("pause");
//科学计数法
float f3 = 3e2;//3*10~2
cout<<f3<<endl;
float f4 = 3e-2;//3*0.1~2
cout<<f4<<endl;
return 0;
}
5.键盘输入值
cpp
#include <iostream>
using namespace std;
int main()
//cin>>
{
int a = 0;
cout<<"请输入a值"<<endl;
cin>>a;
cout<<a<<endl;
return 0;
}
六、运算符
1.算数运算符
注意:两个小狮是不可以做取余运算的
cpp
#include <iostream>
using namespace std;
int main()
{
//前置递增
int a1 = 10;
int b1 = ++a1*10;//先让变量加1,然后进行表达式计算
cout<<a1<<endl;
cout<<b1<<endl;
//后置递增
int a2 = 10;
int b2 = a2++*10;//先进行表达式计算,后让变量加1
cout<<a2<<endl;
cout<<b2<<endl;
return 0;
}
2.赋值运算符
3.比较运算符
4.逻辑运算符
七、函数
1.格式
cpp
返回值类型 函数名 (参数列表)
{
函数体语句
return 表达式
}
2.函数实现与调用
cpp
#include <iostream>
using namespace std;
int add(int num1,int num2)//num1,num2为形参
{
int num3 = num1+num2;
return num3;
}
int main()
{
int a = 1;
int b = 2;
int c = add(a,b); //a,b为实参
cout<<c<<endl;
return 0;
}
3.值传递
cpp
#include <iostream>
using namespace std;
void change(int num1,int num2)//num1,num2为形参
{
//交换前
cout<<"交换前"<<endl;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
int num3 = num1;
num1 = num2;
num2 = num3;
//交换后
cout<<"交换后"<<endl;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
return ;
}
int main()
{
int a = 1;
int b = 2;
//当函数做值传递时,函数的形参会发生改变,并不会影响实参
cout<<"前a:"<<a<<endl;
cout<<"前b:"<<b<<endl;
change(a,b);
cout<<"后a:"<<a<<endl;
cout<<"后b:"<<b<<endl;
return 0;
}
4.函数声明
cpp
#include <iostream>
using namespace std;
//函数声明,提前告诉编译器函数的存在,可以利用函数的声明,避免主函数在上面报错
void change(int num1,int num2);
int main()
{
int a = 1;
int b = 2;
//当函数做值传递时,函数的形参会发生改变,并不会影响实参
cout<<"前a:"<<a<<endl;
cout<<"前b:"<<b<<endl;
change(a,b);
cout<<"后a:"<<a<<endl;
cout<<"后b:"<<b<<endl;
return 0;
}
void change(int num1,int num2)//num1,num2为形参
{
//交换前
cout<<"交换前"<<endl;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
int num3 = num1;
num1 = num2;
num2 = num3;
//交换后
cout<<"交换后"<<endl;
cout<<"num1:"<<num1<<endl;
cout<<"num2:"<<num2<<endl;
return ;
}
八、函数的分文件编写
步骤
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在原文件中写函数的定义
九、指针
1.利用指针变量保存地址
cpp
#include <iostream>
using namespace std;
int main()
{
//定义指针
int a = 10;
int *p;
p = &a;//指针记录a的地址
cout<<"a的地址"<<&a<<endl;
cout<<"指针p的地址"<<p<<endl;
//指针使用
//可以通过解引用的方式来找到指针指向的内容
//指针前加*代表解引用,找到指针指向的内存中的数据
*p = 1000;
cout<<"a的值"<<a<<endl;
cout<<"指针p的值"<<*p<<endl;
return 0;
}
2.空指针
用于给指针变量初始化
空指针是不可以进行访问的
cpp
#include <iostream>
using namespace std;
int main()
{
//空用于给指针变量初始化
int *p = NULL;
//空用于给指针变量初始化
//0-255之间的内存编号是系统占用的,因此不可以访问
*p = 1000;//会报错
return 0;
}
3.野指针
指向非法的内存空间
cpp
#include <iostream>
using namespace std;
int main()
{
//
int *p = (int *)0x000;
cout<<*p<<endl
return 0;
}
4.指针常量,常量指针
cpp
#include <iostream>
using namespace std;
int main()
{
//const修饰指针 常量指针
int a = 10;
int b = 20;
const int *p = &a;
//指针指向的值不能改,指针的指向可以改
//*p = 20 //报错
*p = &b;//正确
//const修饰常量 指针常量
//指针的指向不可以改,指针指向的值可以改
int * const p2 = &a;
*p2 = 100;//正确的
p2 = &b;//错误,指向不能改
//const 修饰指针和常量
const int * const p3 = &a;
//指针的指向和指针指向的值都不可以改
*p3 = 1000;
p3 = &b
return 0;
}
5.指针和数组的配合使用
cpp
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int * p = arr;
for(int i=0;i<10;i++)
{
cout<<*p<<endl;
p++;
}
}
6.指针和函数
cpp
#include <iostream>
using namespace std;
void seap(int a,int b)
{
int tem = a;
a = b;
b = tem;
}
void seap02(int *a,int *b)
{
int tem = *a;
*a = *b;
*b = tem;
}
int main()
{
int a = 10;
int b = 20;
//值传递
//seap(a,b);
//cout<<a<<endl;
//cout<<b<<endl;
//地址传递,会改变实参的值
seap02(&a,&b);
cout<<a<<endl;
cout<<b<<endl;
}
十、结构体
1.定义:
cpp
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
int scroe;
}s3;
int main()
{
//结构体调用
//1.struct Student s1;
struct Student s1;
s1.name = "xuxu";
s1.age = 20;
s1.scroe = 30;
cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<"分数"<<s1.scroe;
//2.struct Student s2 = {...};
struct Student s2 = {"xuxu2",26,100};
cout<<"姓名"<<s2.name<<"年龄"<<s2.age<<"分数"<<s2.scroe;
//3在定义结构体时顺便创建结构体变量
s3.name = "wangwu";
s3.age = 50;
s3.scroe = 2000;
cout<<"姓名"<<s3.name<<"年龄"<<s3.age<<"分数"<<s3.scroe;
return 0;
}
2.结构体数组
cpp
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
int scroe;
}s3;
int main()
{
//创建结构体数组
struct Student arr[3] = {
{"张三",20,100},
{"李四",21,101},
{"王五",22,102},
};
//修改结构体数据
arr[1].name = "liuliu";
//遍历结构体数组
for(int i=0;i<3;i++)
{
cout<<"姓名"<<arr[i].name<<"年龄"<<arr[i].age<<"分数"<<arr[i].scroe;
}
return 0;
}
3.结构体指针
cpp
#include <iostream>
using namespace std;
struct Student
{
string name;
int age;
int scroe;
};
int main()
{
//创建结构体数组
Student s1 = {"xuxu",22,1000};
//定义结构体指针
Student *p = &s1;
//利用指针访问数据
cout<<p->name<<p->age<<p->scroe<<endl;
return 0;
}
4.结构体嵌套
cpp
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
struct Teacher
{
int id;
string name;
int age;
struct student stu;
};
int main()
{
Teacher t;
t.id = 666;
t.name = "xuxu";
t.age = 30;
t.stu.age = 1000;
cout<<t.id<<t.name<<t.age<<t.stu.age<<endl;
return 0;
}
5.结构体参数传递
cpp
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//值传递
void printffun(struct student s)
{
cout<<s.name<<s.age<<s.score<<endl;
}
//地址传递
void printffun01(struct student *s)
{
cout<<s->name<<s->age<<s->score<<endl;
}
int main()
{
student t;
t.name = "xuxu";
t.age = 30;
t.score = 10000;
printffun(t);
printffun01(&t);
//cout<<t.name<<t.age<<t.score<<endl;
return 0;
}
6.结构体中的const
用以防止误操作
cpp
#include <iostream>
using namespace std;
struct student
{
string name;
int age;
int score;
};
//地址传递
void printffun01(const struct student *s)
{
s->age = 150;
cout<<s->name<<s->age<<s->score<<endl;
}
int main()
{
student t;
t.name = "xuxu";
t.age = 30;
t.score = 10000;
printffun01(&t);
//cout<<t.name<<t.age<<t.score<<endl;
return 0;
}