完整题干:
将person类作为学生类student的基类,派生一个学生类,其中数据成员有学号、姓名、年龄、3门课程的成绩,以及若干成员函数。并利用这个类建立一个对象数组。(1)求第一门课的平均成绩;(2)找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩和平均成绩;(3)找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上三个要求。
要求:
(1)使用构造函数实现对数据的输入。
(2)以上三个要求必须用成员函数实现。
(3)在头文件中完成类的声明,在.cpp中完成类的实现,在主文件(.cpp)中完成主函数编写。
个人答案:
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
};
class Student:public person
{
public:
Student(string, int, int, int, int,int);
Student();
void f(Student*p);
void y(Student *p);
void h(Student* p);
protected:
string name; int xuehao; int zheng; int math; int zhuan; int age;
};
void Student::f(Student *p)
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum = (*(p+i)).zheng+ sum;
}
cout << "第一科政治平均成绩为:" << sum / 10.0 << endl;
}
void Student::y(Student*p)
{
for (int i = 0; i < 10; i++) {
int sum = 0;
sum = ((*(p + i)).zheng + (*(p + i)).math + (*(p + i)).zhuan) / 3.0;
if (((*(p + i)).zheng < 60 && (*(p + i)).math < 60) || ((*(p + i)).zheng < 60 && (*(p + i)).zhuan < 60) || ((*(p + i)).math < 60 && (*(p + i)).zhuan < 60))
{
cout << "不及格的学生:" << (*(p + i)).xuehao;
cout << "政治:" << (*(p + i)).zheng << "数学:" << (*(p + i)).math << "专业课:" << (*(p + i)).zhuan << "平均成绩:" << sum << endl;
}
}
}
void Student::h(Student* p)
{
for (int i = 0; i < 10; i++) {
int sum = 0;
sum = ((*(p + i)).zheng + (*(p + i)).math + (*(p + i)).zhuan) / 3.0;
if (((*(p + i)).zheng > 85 && (*(p + i)).math > 85 && (*(p + i)).zhuan > 85) || (sum > 90))cout << "优秀学生:" << (*(p + i)).name << endl;
}
}
Student::Student(string n, int x, int z, int m, int a,int g)
{
name = n;
xuehao = x;
zheng = z;
math = m;
zhuan = a;
age = g;
}
Student::Student()
{
name = " ";
xuehao =0;
zheng = 0;
math = 0;
zhuan = 0;
age = 0;
}
int main()
{
Student a[10] =
{
Student("白雪华",20110901,89,90,90,18),
Student("韩姣姣",20110902,88,90,90,19),
Student("张璐璐",20110903,95,90,90,18),
Student("张清晨",20110904,78,90,90,19),
Student("张博涵",20110902,79,90,90,20),
Student("朱玉玲",20110902,45,25,90,20),
Student("姜健康",20110902,75,90,90,18),
Student("王龙洋",20110902,88,55,90,17),
Student("马传皓",20110902,88,95,90,19),
Student("张振龙",20110902,95,98,90,19),
};
Student t;
t.f(a);
t.y(a);
t.h(a);
system("pause");
return 0;
}
结果: