3.定义一个学生类,内有数据成员(学号、姓名、成绩),定义一个对象数组,设立一个函数max,用指向对象的指针作为函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
(1)定义一个学生类,内有数据成员(学号、姓名、成绩),有update函数、display函数,可实现数据成员设置、显示;
(2)定义无参数的构造函数和带参数的构造函数Student(string xh,string name,float sc);
(3)定义一个对象数组stu[5],求出成绩最好者的学号、姓名。
大概这样子
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string xh, name;
float sc;
public:
void update();
void display();
float getsc();
Student(string xh, string name, float sc);
Student() { xh = name = "", sc = 0; };
};
Student::Student(string xh, string name, float sc)
{
this->xh = xh;
this->name = name;
this->sc = sc;
}
void Student::update()
{
cin >> xh >> name >> sc;
}
void Student::display()
{
cout << xh << "\t" << name << endl;
}
float Student::getsc()
{
return sc;
}
void max(Student *stu)
{
float max = 0;
int index = 0;
for (int i = 0; i < 5; i++)
{
if (max < (stu + i)->getsc())
{
max = (stu + i)->getsc();
index = i;
}
}
(stu + index)->display();
}
int main()
{
Student stu[5];
for (int i = 0; i < 5; i++)
{
stu[i].update();
}
max(stu);
return 0;
}