20230822(C++)定义一个学生的结构体,包含学生的姓名,年龄,成绩,性别,学生的成绩,姓名,定义为私有权限;定义一个学生类型的结构体变量,设置公有函数用于给学生的成绩和名字进行赋值,(结构体中

  1. 定义一个学生的结构体,包含学生的姓名,年龄,成绩,性别,学生的成绩,姓名,定义为私有权限;定义一个学生类型的结构体变量,设置公有函数用于给学生的成绩和名字进行赋值,(结构体中的函数:结构体中声明,结构体外定义)
    #include 
    
    using namespace std;
    struct test
    {
        int age;
        string xb;
        string name1;
        float prive1;
        void fun();
        void set(string name1,float prive1)
        {
    
            name = name1;
            prive = prive1;
        }
    private:
        string name;
    private:
        float prive;
    };
    
    void test :: fun()
    {
        cout << "名字=> " << name << endl;
        cout << "分数=> " << prive << endl;
    }
    
    int main()
    {
        test stu;
        cout << "输入名字=>  ";
        cin >> stu.name1;
        cout << "输入性别=>  ";
        cin >> stu.xb;
        cout << "输入年纪=>  ";
        cin >> stu.age;
        cout << "输入分数=>  ";
        cin >> stu.prive1;
        stu.set(stu.name1,stu.prive1);
        printf("<====================>\n");
        stu.fun();
        cout << "年纪=> " << stu.age << endl;
        cout << "性别=> " << stu.xb << endl;
        printf("<====================>\n");
    
        return 0;
    }
    ---------------------------------------
    输入名字=>  MC
    输入性别=>  男
    输入年纪=>  18
    输入分数=>  95.5
    <====================>
    名字=> MC
    分数=> 95.5
    年纪=> 18
    性别=> 男
    <====================>

    20230822(C++)定义一个学生的结构体,包含学生的姓名,年龄,成绩,性别,学生的成绩,姓名,定义为私有权限;定义一个学生类型的结构体变量,设置公有函数用于给学生的成绩和名字进行赋值,(结构体中_第1张图片

     

你可能感兴趣的:(c++,开发语言,算法)