定义一个Student

1.定义一个Student类,属性为name(String),
age(int), sex(char).
score(double),方法:study(),study(int hours),
study(String name, int hours),打印输出一句话代表实现过程
要求:创建对象,并对该对象赋值和取值,调用方法。

作业自己做!找打

【以下回答由 GPT 生成】

#include <iostream>
#include <string>

class Student {
public:
    // 构造函数
    Student(std::string name, int age, char sex, float score) {
        this->name = name;
        this->age = age;
        this->sex = sex;
        this->score = score;
    }

    // 无参数的study()方法
    void study() {
        std::cout << "Student " << name << " is studying." << std::endl;
    }

    // 接受一个整数参数hours的study()方法
    void study(int hours) {
        std::cout << "Student " << name << " is studying for " << hours << " hours." << std::endl;
    }

    // 接受一个字符串参数name和一个整数参数hours的study()方法
    void study(std::string subject, int hours) {
        std::cout << "Student " << name << " is studying " << subject << " for " << hours << " hours." << std::endl;
    }

private:
    std::string name;
    int age;
    char sex;
    float score;
};

int main() {
    // 创建一个Student对象
    Student student("John", 20, 'M', 95.5);

    // 调用类的方法
    student.study();                                     // 调用无参数的study()方法
    student.study(3);                                    // 调用接受一个整数参数hours的study()方法
    student.study("Math", 5);                            // 调用接受一个字符串参数name和一个整数参数hours的study()方法

    return 0;
}

解释:上述代码定义了一个Student类,并实现了属性和方法。Student类的属性包括name(字符串)、age(整数)、sex(字符)和score(浮点数),通过构造函数初始化。类中包含三个study()方法:一个无参数、一个接受一个整数参数hours、一个接受一个字符串参数name和一个整数参数hours。在main函数中,创建了一个Student对象,并调用了类的不同方法进行学习操作。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^