定义一个学生类Student 数据成员 (private): 学生姓名(char *name)、性别(bool sex)、学号、出生日期(Birthday birth)、专业课门数、专业课成绩(double *score) 成员函数 (public): 构造函数:对学生的姓名、性别、学号、出生日期、专业课门数进行初始化 Student(char *_name, bool _sex, int _sno, int year, int month, int day, int _num); 析构函数:释放系统资源(堆空间) 提供拷贝构造函数 录入专业课的成绩 void input(double *s); 修改某门专业课的成绩 void change(int i, double s); 获取某门专业课的成绩 double get(int i); 获取专业课门数 计算专业课平均成绩 计算不及格专业课的门数 显示学生基本信息
时间: 2024-03-25 10:40:13 浏览: 92
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
下面是一个基本的学生类Student的定义,包括数据成员和成员函数的实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class Birthday {
public:
Birthday(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
void display() {
cout << year << "-" << month << "-" << day << endl;
}
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) {
name = new char[strlen(_name) + 1];
strcpy(name, _name);
sex = _sex;
sno = _sno;
birth = new Birthday(year, month, day);
num = _num;
score = new double[num];
}
~Student() {
delete[] name;
delete birth;
delete[] score;
}
Student(const Student &stu) {
name = new char[strlen(stu.name) + 1];
strcpy(name, stu.name);
sex = stu.sex;
sno = stu.sno;
birth = new Birthday(*stu.birth);
num = stu.num;
score = new double[num];
for (int i = 0; i < num; i++) {
score[i] = stu.score[i];
}
}
void input(double *s) {
for (int i = 0; i < num; i++) {
score[i] = s[i];
}
}
void change(int i, double s) {
score[i] = s;
}
double get(int i) {
return score[i];
}
int getNum() {
return num;
}
double getAverage() {
double sum = 0;
for (int i = 0; i < num; i++) {
sum += score[i];
}
return sum / num;
}
int getFailNum() {
int cnt = 0;
for (int i = 0; i < num; i++) {
if (score[i] < 60) {
cnt++;
}
}
return cnt;
}
void display() {
cout << "姓名:" << name << endl;
cout << "性别:" << (sex ? "男" : "女") << endl;
cout << "学号:" << sno << endl;
cout << "出生日期:";
birth->display();
cout << "专业课门数:" << num << endl;
cout << "专业课成绩:";
for (int i = 0; i < num; i++) {
cout << score[i] << " ";
}
cout << endl;
cout << "专业课平均成绩:" << getAverage() << endl;
cout << "不及格专业课门数:" << getFailNum() << endl;
}
private:
char *name;
bool sex;
int sno;
Birthday *birth;
int num;
double *score;
};
```
然后可以在主函数中进行测试:
```c++
int main() {
double score[] = { 80, 90, 70 };
Student stu("张三", true, 1001, 1998, 5, 1, 3);
stu.input(score);
stu.display();
return 0;
}
```
输出结果为:
```
姓名:张三
性别:男
学号:1001
出生日期:1998-5-1
专业课门数:3
专业课成绩:80 90 70
专业课平均成绩:80
不及格专业课门数:0
```
阅读全文