木子杳衫 2023-12-18 21:32 采纳率: 0%
浏览 11

用C++如何写这个问题的代码

设计一一个名为student学生类,其中有数据成员:姓名(name)、性别(sex)、 学号(num)、分数(score) ;类中的成员函数:设置带有3个参数的构造函数和无参构造函数、输出学生信息的函数display(),更改学生信息的函数modify ()。 .

(1)完成student类的定义和相应成员函数的实现;

(2)创建-个student对象st1。st的信息为“张三, 男,1234567",将st1学生分数更新为78,显示更新后学生的信息。

(3)创建一-个student对象st2,实现学生st2=st1;并输出st2的信息。

  • 写回答

1条回答 默认 最新

  • 李烁. 2023-12-18 22:52
    关注
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    class student {
    private:
        string name;
        string sex;
        string num;
        int score;
    
    public:
        student() {}
        student(string n, string s, string no, int sc) {
            name = n;
            sex = s;
            num = no;
            score = sc;
        }
    
        void display() {
            cout << "姓名:" << name << ",性别:" << sex << ",学号:" << num << ",分数:" << score << endl;
        }
    
        void modify(int sc) {
            score = sc;
        }
    };
    
    int main() {
        student st1("张三", "男", "1234567", 0);
        st1.modify(78);
        st1.display();
    
        student st2 = st1;
        st2.display();
    
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月18日