1.定义一个学生的结构体,包含学生的姓名,年龄,成绩,性别,学生的成绩,姓名,定义为私有权限;定义一个学生类型的结构体变量,设置公有函数用于给学生的成绩和名字进行赋值,(结构体中的函数:结构体中声明,结构体外定义)
#include <iostream>using namespace std;struct stu
{int age;string sex;void func();void set_name(string &str,float &s){name = str;score = s;}
private:string name;float score;
};
void stu::func()
{cout << name << endl;cout << age << endl;cout << sex << endl;cout << score << endl;
}
int main()
{stu students;string str = "xjx";students.age = 22;students.sex = "女";float s = 100;students.set_name(str,s);students.func();return 0;
}
