学生类Student 数据成员(访问权限定义为protected): 学生姓名(char*name)、性别(boolsex、学号(intsno)、出生日期 (Birthdaybirth)、专业课门数(intnum)、专业课成绩(double*score) 成员西数(访问权限定义为public): •构造西数:对学生的姓名、性别、学号、出生日期、专业课门数进行初始 化 Student(char *_name, bool _sex, int _sno, int year, int month, int day, int _пит): 注意: 1.在构造两数中为score申请堆空间数组 2.加入异常处理机制 •拷贝构造西数:数据成员初始化(避免浅拷贝) Student(constStudent&xst); 注意:加入异常处理机制 •析构两数:释放系统资源 ~StudentO; •录入专业课的成绩voidinput(double*s);或voidinputtdoublestJ); •修改某门专业课的成绩 void changeScore(int i, double s); •修改专业课的门数 void changeNum(int _num); •计算专业课平均成缋(定义为const成员函数) double average) const; 注意:加入异常处理机制 •计算不及格专业课的门数(定义为const成员函数) intfaildconst; •品示学生基本信息(定义为const成员函数) void print) const; 要求:将类的定义与类成员
时间: 2023-12-12 16:02:36 浏览: 190
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
函数的实现放在头文件中,同时在头文件中添加必要的头文件引用和命名空间声明,以便其他程序可以直接包含该头文件使用该类。例如:
```c++
#ifndef STUDENT_H
#define STUDENT_H
#include <cstring>
#include <stdexcept>
#include <iostream>
namespace mynamespace {
class Birthday {
public:
Birthday(int year, int month, int day) : year_(year), month_(month), day_(day) {}
~Birthday() {}
int getYear() const { return year_; }
int getMonth() const { return month_; }
int getDay() const { return day_; }
private:
int year_;
int month_;
int day_;
};
class Student {
public:
Student(char *_name, bool _sex, int _sno, int year, int month, int day, int _num);
Student(const Student& xst);
~Student();
void input(double *s);
void input(const std::vector<double>& scores);
void changeScore(int i, double s);
void changeNum(int _num);
double average() const;
int faild() const;
void print() const;
protected:
char *name_;
bool sex_;
int sno_;
Birthday birth_;
int num_;
double *score_;
};
} // namespace mynamespace
#endif // STUDENT_H
```
注意,在实现类的成员函数时,需要加上类的命名空间前缀。例如:
```c++
void mynamespace::Student::input(double *s) {
for (int i = 0; i < num_; ++i) {
score_[i] = s[i];
}
}
void mynamespace::Student::input(const std::vector<double>& scores) {
if (scores.size() != num_) {
throw std::invalid_argument("the size of scores does not match the number of courses");
}
for (int i = 0; i < num_; ++i) {
score_[i] = scores[i];
}
}
void mynamespace::Student::changeScore(int i, double s) {
if (i < 0 || i >= num_) {
throw std::out_of_range("invalid course index");
}
score_[i] = s;
}
void mynamespace::Student::changeNum(int _num) {
if (_num < 0) {
throw std::invalid_argument("invalid number of courses");
}
double *new_score = new double[_num];
int n = std::min(num_, _num);
for (int i = 0; i < n; ++i) {
new_score[i] = score_[i];
}
delete[] score_;
score_ = new_score;
num_ = _num;
}
double mynamespace::Student::average() const {
if (num_ == 0) {
throw std::logic_error("no course scores");
}
double sum = 0;
for (int i = 0; i < num_; ++i) {
sum += score_[i];
}
return sum / num_;
}
int mynamespace::Student::faild() const {
int cnt = 0;
for (int i = 0; i < num_; ++i) {
if (score_[i] < 60) {
++cnt;
}
}
return cnt;
}
void mynamespace::Student::print() const {
std::cout << "Name: " << name_ << std::endl;
std::cout << "Sex: " << (sex_ ? "male" : "female") << std::endl;
std::cout << "Student Number: " << sno_ << std::endl;
std::cout << "Birthday: " << birth_.getYear() << "-" << birth_.getMonth() << "-" << birth_.getDay() << std::endl;
std::cout << "Number of Courses: " << num_ << std::endl;
std::cout << "Course Scores: ";
for (int i = 0; i < num_; ++i) {
std::cout << score_[i] << " ";
}
std::cout << std::endl;
}
```
阅读全文