出自:C++语言程序设计基础——清华郑莉老师
C++ 中,结构体是一种特殊的类
与类的唯一区别:结构体的缺省访问权限是public成员,而类的是private成员
什么时候用呢?
定义主要用来保存数据,而没有什么**操作(函数)**的类型
定义
struct 结构体名称 {
公有成员**(默认)**
protected:
保护型成员
private:
私有成员
};
例题
#include<iostream>
//#include<iomanip>
#include<string>
using namespace std;
struct Student {
int num; //学号
string name; //姓名,字符串对象
char sex; //性别
int age; //年龄
};
int main()
{
Student ylc = { 12018,"Ylc",'B',23 };
cout << "学号:" << ylc.num << endl;
cout << "姓名:" << ylc.name << endl;
cout << "性别:" << ylc.sex << endl;
cout << "年龄:" << ylc.age << endl;
system("pause");
return 0;
}