一、函数的分文件编写:
- 创建后缀名为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件中写函数的声明
- 在源文件中写函数的定义
二、指针的定义:
int a = 10;//且a有其对应的地址,假设其地址为0x0004;
int * p; //定义指针
p = &a; //定义p为a的地址;
/*指针变量可以通过" * "操作符,操作指针变量指向的内存空间,这个过程称为解引用。即*用来访问地址对应的值*/
*p = 100; //通过指针改变a的地址对应的值,即将地址0x0004储存的值改成100;
cout << * p <<endl;
cout << a <<endl; //则a = *p = 100;
三、空指针和野指针
空指针:指针变量指向内存中编号为0的空间
即初始化指针变量
ps:空指针指向的内存是不可以访问的,包括0~255的内存是不可访问的
野指针:指针变量指向非法的内存空间,即也不能访问。
总结:空指针和野指针都不是我们申请的空间,因此不要访问。
四、const修饰指针
1、常量指针
特点:指针的指向可以改,指针指向的值不可以改
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 10;
const int* p = &a;
//*p = 100;//err 即指针指向的值不可以改
p = &b; //指针的指向可以改
system("pause");
return 0;
}
2、指针常量
特点:指针的指向不可以改,指针指向的值可以改
即p = &b;是错误的,
*p = 20;是可以执行的
3、const既修饰指针又修饰常量
特点:指针的指向不可以改,指针指向的值也不可以改!!
五、指针和函数
#include<iostream>
using namespace std;
void swap(int* a, int* b) //a,b都代表指针
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int a = 100;
int b = 200;
swap(&a, &b); //这里传入的是地址,可以修改地址对应的值
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}
六、结构体
1.定义和创建变量
#include<iostream>
using namespace std;
#include<string>
//自定义数据类型
struct student
{
string name;
int age;
int score;
};
int main()
{
//通过学生类型创建具体学生
//1.struct student s1;
student s1; //创建好变量名s1后,用“.”来访问结构体数据类型里的变量属性
s1.age = 18;
s1.name = "张三 ";
s1.score = 617;
cout << s1.name << s1.age <<" "<< s1.score << endl;
//2.struct student s2={....};
student s2 = { "李四 ",19,600 };
cout << s2.name << s2.age <<" "<<s2.score << endl;
//在定义结构体时时顺便创建变量
/*struct student
{
string name;
int age;
int score;
}s3;
s3.age = 18;
s3.name = "王五 ";
s3.score = 583;*/
system("pause");
return 0;
}
2.结构体数组
#include<iostream>
using namespace std;
#include<string>
//自定义数据类型
struct student
{
string name;
int age;
int score;
};
int main()
{
//创建结构体数组
student arr[3] =
{
{"张三",18,100 },
{"李四",20,90},
{"王五",30,70}
};
//给结构体数组的元素赋值
arr[2].name = "赵六";
arr[2].age = 22;
arr[2].score = 80;
for (int i = 0; i < 3; i++)
{
cout << arr[i].name<<" "
<< arr[i].age<<" "
<< arr[i].score << endl;
}
system("pause");
return 0;
}
3、结构体指针
#include<iostream>
using namespace std;
#include<string>
//自定义数据类型
struct student
{
string name;
int age;
int score;
};
int main()
{
student s = { "张三",18,617 };
//通过指针指向结构体变量
student* p = &s;
//通过指针访问结构体变量的数据
cout << p->name << " " << p->age << " " << p->score << " " << endl;
//即可以通过“->”来访问结构体的数据属性
system("pause");
return 0;
}
4、结构体嵌套结构体
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
student stu;
};
int main()
{
teacher t; //定义结构体teacher的变量名t
t.stu.name = "laowang";
t.stu.age = 18;
t.stu.score = 100;
cout << t.stu.name << t.stu.age << t.stu.score << endl;
system("pause");
return 0;
}
5、结构体作函数参数
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
int score;
};
//结构体作函数参数
//值传递
void printstudent1(student stu)
{
stu.age = 50;
cout << "值传递子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}
void printstudent2(student* stu)
{
stu->age = 50;
cout << "地址传递子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
}
int main()
{
student stu;
stu.name = "张三 ";
stu.age = 18;
stu.score = 617;
//值传递
printstudent1(stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
//地址传递
printstudent2(&stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
system("pause");
return 0;
}
6、结构体中 const使用场景
void printStudent(const student *stu) //加const防止函数体中的误操作
{
//stu->age = 100; //修改操作失败,因为加了const修饰
cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
}
7、结构体案例
1)、
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
struct student
{
string name;
int score;
};
struct teacher
{
string name;
student sarr[5];
};
void fu(teacher tarr[])
{
string n = "ABCDE";
for (int i = 0; i < 3; i++)
{
tarr[i].name = n[i];
for (int j = 0; j < 5; j++)
{
tarr[i].sarr[j].name = n[j];
int num = rand() % 61 + 40;
tarr[i].sarr[j].score = num;
}
}
}
void print1(teacher tarr[])
{
for (int i = 0; i < 3; i++)
{
cout << "老师的名字:教师" << tarr[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "学生" << tarr[i].sarr[j].name << " "
<< tarr[i].sarr[j].score << endl;
}
}
}
int main()
{
srand((size_t)time(NULL));
teacher tarr[3];
fu(tarr);
print1(tarr);
system("pause");
return 0;
}
2)、
#include<iostream>
using namespace std;
struct hero
{
string name;
string sex;
int age;
};
int sort(hero* man)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5-i; j++)
{
if (man[j].age <man[j+1].age )
{
hero tmp = man[j];
man[j] = man[j + 1];
man[j + 1] = tmp;
}
}
}
return 0;
}
int main()
{
hero man[6] =
{
{"保罗乔治","男",30},
{"字母哥","男",28},
{"詹姆斯","男",37},
{"杜兰特","男",33},
{"库里","男",32},
{"欧文","男",31},
};
sort(man);
for (int i = 0; i < 6; i++)
{
cout << man[i].name << " " << man[i].sex << " " << man[i].age << endl;
}
}