C++面向对象程序设计
题目:设计一个CStudent类,并使CStudent类具有以下特点:(1)该类具有学生姓名、学号、某几门课的课程成绩
(2)学生信息全部由键盘输入,以提高程序的适应性
(3)通过成员函数统计学生的平均成绩,当课程数量增加时无需修改仍可求出平均成绩
(4)输出学生的基本信息、各科成绩与平均成绩
(5)学生对象的定义采用对象数组实现
(6)统计不及格学生的人数
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
string name;
string ID;
double ach[10];
double ave = 0;
int num;
static int count;
public:
CStudent(string i_name, string i_ID, int i_num, double* i_ach)
{
name = i_name;
ID = i_ID;
num = i_num;
for (int i = 0; i < num; i++)
{
ach[i] = i_ach[i];
ave += ach[i] / num;
}
for (int i = 0; i < num; i++)
{
if (ach[i] < 60)
{
count++;
break;
}
}
}
CStudent(){
};
void Print(){
for (int i = 0; i < num; i++)
cout << "第" << i+1 << "门成绩为:" << ach[i] << endl;
cout << "平均成绩为:" << ave << endl;
};
void print(){
cout << "不及格人数为:" << count << endl;
};
};
int CStudent::count = 0;
int main()
{
CStudent student[100] = {};
int n;
double ach[10];
string name, ID;
cout << "共需要录入多少学生的成绩" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "请输入学生的姓名" << endl;
cin >> name;
cout << "请输入学生的学号" << endl;
cin >> ID;
int j;
cout << "请输入该学生需要录入多少门成绩" << endl;
cin >> j;
for (int m = 0; m < j; m++)
{
cout << "请输入该学生的第" << m+1 << "门成绩" << endl;
cin >> ach[m];
}
student[i] = CStudent(name, ID, j, ach);
student[i].Print();
}
student[0].print();
return 0;
}