C++day2作业(2023.8.22)

2023-11-18

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;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++day2作业(2023.8.22) 的相关文章

随机推荐