0
点赞
收藏
分享

微信扫一扫

C++结构体

booksmg2014 2022-01-05 阅读 182
c++结构体

结构体

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。

整型、浮点型、布尔型、字符型、字符串型。系统内置的类型。

#include<iostream>
using namespace std;
#include<string>
struct Student
{
	//成员列表
	string name;
	int age;
	int score;
}s3;

int main()
{
	// 通过学生类型创建具体学生。三种方式:
	// 1、struct Student s1
	// 2、struct Student s2 = {...}
	// 3、在定义结构体时顺便创建结构体变量。

	struct Student s1; // 给s1属性赋值。
	s1.name = "张三";
	s1.age = 18;
	s1.score = 99;

	cout << "姓名:" << s1.name << ",年龄:" << s1.age << ",成绩:" << s1.score << "。" << endl;

	struct Student s2 = { "李四", 20, 100 };
	cout << "姓名:" << s2.name << ",年龄:" << s2.age << ",成绩:" << s2.score << "。" << endl;

	s3.name = "王五";
	s3.age = 17;
	s3.score = 99;
	cout << "姓名:" << s3.name << ",年龄:" << s3.age << ",成绩:" << s3.score << "。" << endl;

	// 创建变量时,关键字struct可以省略。
	Student s4;
	s4.name = "赵六";
	s4.age = 19;
	s4.score = 100;
	cout << "姓名:" << s4.name << ",年龄:" << s4.age << ",成绩:" << s4.score << "。" << endl;

	system("pause");
	return 0;
}



结构体数组

#include<iostream>
using namespace std;
#include<string>

struct Student
{
	string name;
	int age;
	int score;
};

int main()
{
	struct Student stuArray[3] = {
		{"张三", 19, 90 },
		{"李四", 20, 99 },
		{"王五", 18, 96 }

	};
	stuArray[1].name = "赵六";
	stuArray[0].age = 99;
	stuArray[2].score = 100;

	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name 
			 << ",年龄:" << stuArray[i].age
			 << ",成绩:" << stuArray[i].score 
			 << "。" << endl;
	}

	system("pause");
	return 0;
}





结构体指针

#include<iostream>
using namespace std;
#include<string>

struct Student
{
	string name;
	int age;
	int score;
};

int main()
{

	// 指针指向结构体变量。
	// 通过指针访问结构体变量中的数据。  -> 访问符

	struct Student s = { "张三", 19, 200 };
	struct Student* p = &s;
	cout << "姓名:" << p->name << "	年龄:" << p->age << " 成绩:" << p->score << endl;
	system("pause");
	return 0;
}


结构体嵌套结构体

#include<iostream>
using namespace std;
#include<string>

struct Student
{
	string name;
	int age;
	int score;
};

struct Teacher
{
	int id;
	string name;
	int age;
	struct Student stu;   // 辅导的学生。
};

int main()
{
	Student st = { "张三", 20, 99 };
	Teacher te;
	te.id = 100;
	te.name = "老王";
	te.age = 50;
	te.stu = st;

	cout << "老师 " << te.name << " 辅导的学生 " << te.stu.name << " 成绩为:" << te.stu.score << endl;

	system("pause");
	return 0;
}



结构体做函数参数

作用:将结构体作为参数向函数中传递

#include<iostream>
using namespace std;
#include<string>


struct Student
{
	string name;
	int age;
	int score;
};

void PrintStudentInfo(Student s)
{
	s.name = "赵四";
	cout << "子函数中结构体参数值:" << s.name << s.age << ":" << s.score << endl;
}
void PrintStudentInfo1(Student* s)
{
	s->name = "尼古拉斯赵四";
	s->age = 100;
	cout << "子函数1中结构体参数值:" << s->name << s->age << ":" << s->score << endl;
}
int main()
{
	Student s = { "张三", 19, 99 };
	cout << "main中结构体实参值:" << s.name << s.age <<":"<< s.score << endl;

	PrintStudentInfo(s);
	PrintStudentInfo1(&s);
	cout << "main中结构体实参值:" << s.name << s.age << ":" << s.score << endl;

	system("pause");
	return 0;
}


结构体中const使用场景

const使用场景:

#include<iostream>
using namespace std;
#include<string>


struct Student
{
	string name;
	int age;
	int score;
};

void printStuInfo(Student s)
{
	s.age = 10000;
	cout << s.name << "的年龄:" << s.age << "。成绩为:" << s.score << endl;
}

int main()
{
	Student s = { "张三", 19, 99 };
	// 通过函数来打印结构变量的信息。
	printStuInfo(s);


	system("pause");
	return 0;
}
举报

相关推荐

0 条评论