c++ 求解 谢谢大神们

c++ 求解 谢谢大神们定义一个描述学生的类 Student,数据成员包括学号、姓名、性别、家庭住址、联系电话、邮箱以及五门课程的成绩。成员函数包括对上述数据成员进行获取和设置,计算平均成绩,输出一个学生的全部描述信息。在主函数中定义一个学生类对象,然后对所有成员函数进行测试。

代码:

/**
 * 定义一个描述学生的类Student
 * 数据成员包括学号、姓名、性别、家庭住址、联系电话、邮箱以及五门课程的成绩。
 * 成员函数包括对上述数据成员进行获取和设置,计算平均成绩,输出一个学生的全部描述信息。
 * 在主函数中定义一个学生类对象,然后对所有成员函数进行测试。
 */

#include <cstdint>
#include <iostream>
#include <string>

class student_t {
 public:
  using gender_t = enum { male, female };

 public:
  student_t() {
    _id        = "";
    _name      = "";
    _gender    = female;
    _address   = "";
    _phone_num = "";
    _email     = "";
    _average   = 0.0f;
    for (uint32_t it = 0; it < _course_cnt; it++) { _grades[it] = 0.0f; }
  }
  ~student_t() {}
  student_t(const student_t&) = delete;
  student_t& operator=(const student_t&) = delete;

  // getters
 public:
  const std::string& address() const { return _address; }
  const std::string& email() const { return _email; }
  const std::string& id() const { return _id; }
  const std::string& name() const { return _name; }
  const std::string& phone_num() const { return _phone_num; }
  const std::string  gender() const {
    return ((_gender == male) ? "male" : "female");
  }

  // setters
 public:
  void set_gender(const gender_t gender) { _gender = gender; }
  void set_address(const std::string& address) { _address = address; }
  void set_email(const std::string& email) { _email = email; }
  void set_id(const std::string& id) { _id = id; }
  void set_name(const std::string& name) { _name = name; }
  void set_phone_num(const std::string& phone_num) { _phone_num = phone_num; }

  // course grades
 public:
  double grade_of(const uint32_t course_id) {
    if (course_id < _course_cnt) return _grades[course_id];
    return -1.0f;
  }
  void set_grade_of(const uint32_t course_id, const double grade) {
    if (course_id < _course_cnt) {
      _grades[course_id] = grade;
      update_grades();
    }
  }
  double average() const { return _average; }

  // info summary
 public:
  void dump(std::ostream& stream) {
    stream << "information of " << id() << std::endl;
    stream << "  name      : " << name() << std::endl;
    stream << "  gender    : " << gender() << std::endl;
    stream << "  address   : " << address() << std::endl;
    stream << "  phone_num : " << phone_num() << std::endl;
    stream << "  email     : " << email() << std::endl;
    stream << "  scores    : ";
    for (uint32_t it = 0; it < _course_cnt; it++) {
      stream << grade_of(it);
      if (it + 1 < _course_cnt) { stream << ","; }
    }
    stream << std::endl;
    stream << "  average   : " << average() << std::endl;
  }

 protected:
  void update_grades() {
    double sum = 0;
    for (uint32_t it = 0; it < _course_cnt; it++) { sum = sum + _grades[it]; }
    _average = sum / _course_cnt;
  }

 private:
  static const uint32_t _course_cnt = 5;
  std::string           _id;
  std::string           _name;
  gender_t              _gender;
  std::string           _address;
  std::string           _phone_num;
  std::string           _email;
  double                _average;
  double                _grades[_course_cnt];
};

void do_test(bool result, const std::string& info) {
  std::cout << "test over " << info << " " << (result ? "pass" : "fail")
            << std::endl;
}

int main(int argc, char const* argv[]) {
  // create target
  student_t student;

  // set basic informations
  student.set_gender(student_t::female);
  student.set_address("where ever");
  student.set_email("[email protected]");
  student.set_id("00100010");
  student.set_name("alice");
  student.set_phone_num("010-00000000");
  student.set_grade_of(0, 60.0f);
  student.set_grade_of(1, 70.0f);
  student.set_grade_of(2, 80.0f);
  student.set_grade_of(3, 90.0f);
  student.set_grade_of(4, 100.0f);

  // test through getters
  do_test(student.gender() == "female", "gender");
  do_test(student.address() == "where ever", "address");
  do_test(student.email() == "[email protected]", "email");
  do_test(student.id() == "00100010", "id");
  do_test(student.name() == "alice", "name");
  do_test(student.phone_num() == "010-00000000", "phone_num");
  do_test(student.grade_of(0) == 60.0f, "grade_of_0");
  do_test(student.grade_of(1) == 70.0f, "grade_of_1");
  do_test(student.grade_of(2) == 80.0f, "grade_of_2");
  do_test(student.grade_of(3) == 90.0f, "grade_of_3");
  do_test(student.grade_of(4) == 100.0f, "grade_of_4");
  do_test(student.average() - 80.0f < 1e-6, "average");

  // dump infos
  student.dump(std::cout);

  return system("pause");
}

结果:

温馨提示:答案为网友推荐,仅供参考
相似回答