定义一个学生的结构体,包含学生的姓名年龄,成绩,性别,学生的成绩姓名定义为私有权限;定义一个学生类型的结构体变量,设置共有函数用于给学生的成绩和名字进行赋值
#include <iostream>
using namespace std;
struct Stu
{
string sex;
int age;
void chan_ns(Stu *s1,string na,float sc);
void output(Stu s1);
private:
string name;
float score;
};
void Stu::chan_ns(Stu *s1,string na,float sc)
{
s1->name = na;
s1->score = sc;
}
void Stu::output(Stu s1)
{
cout << s1.name << " " << s1.sex << " " << s1.age << " " << s1.score << endl;
}
int main()
{
Stu stud;
stud.sex = "man";
stud.age = 18;
string stna = "zs";
float stsc = 82.2;
stud.chan_ns(&stud,stna,stsc);
stud.output(stud);
return 0;
}