C++程序设计项目:指针操作学生类

来源:互联网 发布:nodejs高级编程 中文 编辑:程序博客网 时间:2024/06/14 18:08
/*设计一个学生类Student,包括学号num和成绩score,建立一个对象数组,设置5个学生的数据要求:1.用指针指向数组首元素,输出第1、3、5个学生的信息;2.设计一个函数int max(Student *arr);用指向对象的指针作函数参数,在max函数中找到5个学生中成绩最高者,并返回其学号。题目源自:http://blog.csdn.net/sxhelijian/article/details/8737405*/#include <iostream>using namespace std;class Student{private:long num;float score;public:Student(long n, float s):num(n),score(s){};void showMessage();//输出学生信息//通过公共的成员函数取出私立有的数据成员long getNum(){return num;}float getScore(){return score;}}stu[5] = {Student(10001,67.5),Student(10002,78.5),Student(10003,98.5),Student(10004,75),Student(10005,70)};void Student::showMessage(){cout << num << '\t' << score << endl;}//寻找成绩最高者int max(Student *arr){float maxScore = 0;int flag = 0;for(; arr < stu + 5; arr++){if(arr->getScore() > maxScore){maxScore = arr->getScore();flag = arr->getNum();}}return flag;}int main(){Student *p = stu;cout << "第1、3、5个学生的信息:" << endl; for(; p < stu+5; p+=2){p->showMessage();}p = stu;cout << "5位学生中成绩最高者学号:" << max(p) << endl;return 0;}

原创粉丝点击