基础知识
#include <iostream>
#include<string>
using namespace std;
//1.结构体:结构体是用户自定义的数据类型,允许用户存储不同的数据类型
/*语法:struct 结构体名 {结构体成员列表}
1.struct 结构体名 变量名
2.struct 结构体名 ={成员值1,成员值2...}
3.定义结构体时顺便创建变量*/
//创建学生数据类型:学生包括(姓名,年龄,分数)
//自定义数据类型,一些类型集合组成一个类型
//struct 结构体名 {结构体成员列表}
struct Student//结构体定义部分
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3;//创建结构体变量
//2.结构体数组部分
struct Student1
{
string name;
int age;
int score;
};
//结构体指针部分
//作用:通过指针访问结构体中的成员
//·利用操作符 -> 可以通过结构体指针访问结构体属性
struct Student2
{
string name;
int age;
int score;
};
//3.结构体嵌套结构体
//作用:结构体中的成员可以是另一个结构体
//例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
struct Student3//下边要用,写在前边,先定义再使用
{
string name;
int age;
int score;
};
struct teacher3
{
int id; //教师编号
string name;
int age;
struct Student3 stu3;
};
//结构体做函数参数部分
//作用:将结构体作为参数想函数中传递
//传递方式:1.值传递2.地址传递
struct Student4
{
string name;
int age;
int score;
};
//打印学生信息函数
//值传递(值传递中将实参中的数据copy一份给形参,开销大,使用地址传递课解决这开销大问题)
void printStudent1(struct Student4 s4)
{
//s4.age = 100; 特别注意78行和85行代码的,值传递(无影响)和地址传递(有影响)对实参的影响,对程序的影响
//如果想修改主函数中的数据,用地址传递,否则用值传递
cout << "子函数1中打印 姓名:" << s4.name << " 年龄:" << s4.age << " 分数:" << s4.score << endl;
}
//地址传递
void printStudent2(struct Student4* p1)
{
//p1->age = 200;
cout << "子函数2中打印 姓名:" << p1->name << " 年龄:" << p1->age << " 分数:" << p1->score << endl;
}
//结构体中const的使用场景 作用:用const来防止误操作
struct Student5
{
string name;
int age;
int score;
};
void printStudent3(const struct Student5* p2)
{
//p2->age = 150;加入const后,一旦有修改的操作时就会报错,可以防止无操作
cout << "子函数3中打印 姓名:" << p2->name << " 年龄:" << p2->age << " 分数:" << p2->score << endl;
}
int main()
{
//通过学生类型创建具体学生
//struct Student s1;
cout << "结构体定义部分" << endl;
struct Student s1; //struct 关键字可以省略
//给s1属性赋值,通过 . 访问结构体变量中的属性
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//struct Student s2=(...);
struct Student s2 = { "李四",19,80 };
cout << "姓名:" << s2.name << " 年龄:" << s2.age << " 分数:" << s2.score << endl;
//在定义结构体时顺便创建结构体变量;
s3.name = "王五";
s3.age = 20;
s3.score = 99;
cout << "姓名:" << s3.name << " 年龄:" << s3.age << " 分数:" << s3.score << endl;
cout << "\n " << endl;
//结构体数组部分
cout << "结构体数组部分" << endl;
//2.创建结构体数组
struct Student1 stuArray[3] = {
{"张三",18,100},
{"李四",28,99},
{"王五",38,66}
};
//3.给结构体数组中的元素赋值
stuArray[2].name = "赵六";
stuArray[2].age = 80;
stuArray[2].score = 60;
//4.遍历结构体数组
for (int i = 00;i < 3;i++) {
cout << "姓名:" << stuArray[i].name
<< " 年龄:" << stuArray[i].age
<< " 分数:" << stuArray[i].score << endl;
}
cout << "\n " << endl;
//结构体指针部分
cout << "结构体指针部分"<<endl;
//1.创建学生结构体变量
struct Student2 s3 = { "张三",18,100 };
//2.通过指正指向结构体变量
struct Student2 * p = &s3;
//3.通过指针访问结构体变量中的数据(通过结构体指针访问结构体中的属性,需要利用 -> )
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
cout << "\n " << endl;
//结构体嵌套结构体部分
cout << "结构体嵌套结构体部分 " << endl;
//创建老师
teacher3 t;
t.id = 10086;
t.name = "隔壁老王";
t.age = 55;
t.stu3.name = "小王";
t.stu3.age = 18;
t.stu3.score = 100;
cout << "老师姓名: " << t.name << " 老师编号:" << t.id << " 老师年龄:" << t.age << " 老师辅导学生姓名”"
<< t.stu3.name<< " 学生年龄:" << t.stu3.age << " 学生成绩:" << t.stu3.score << endl;
cout << "\n " << endl;
//结构体做函数参数部分
cout << "结构体做函数参数部分 " << endl;
//将学生传入到一个参数中,打印学生身上的所有信息
//创建结构体变量
struct Student4 s4;
s4.name = "张三";
s4.age = 18;
s4.score = 100;
cout << "main函数中打印 姓名:" << s4.name << " 年龄:" << s4.age << " 分数:" << s4.score << endl;
printStudent1(s4);
printStudent2(&s4);//指针
cout << "main函数中打印 姓名:" << s4.name << " 年龄:" << s4.age << " 分数:" << s4.score << endl;
cout << "\n " << endl;
//结构体中const使用场景
//创建结构体变量
cout << "结构体中const使用场景 " << endl;
struct Student5 s5 = { "张三",15,78 };
//通过函数来打印结构体变量的信息
printStudent3(&s5);
cout << "\n " << endl;
system("pause");
return 0;
}