指向学生类的指针

来源:互联网 发布:假期综合症 锻炼 知乎 编辑:程序博客网 时间:2024/05/16 06:02
/** 程序的版权和版本声明部分* Copyright (c)2014, 烟台大学计算机学院学生* All rightsreserved.* 文件名称:a.cpp* 作    者:孔云* 完成日期:2014年3月27日* 版 本 号: v1.0* 输入描述:略。* 问题描述:设计学生类Student,自行设计成员函数,建立一个对象数组,用指针指向数组首元素,通过初始化,设置5个学生的数据。* 输出描述:输出第1、3、5个学生的信息,成绩最高者的学号。*/#include <iostream>#include <cstdlib>using namespace std;class Student{public:    Student(int x,double y):num(x),score(y) {}    double get1();    double get2();    void display();private:    int num;   //学号    double score;   //成绩};//max函数返回arr指向的对象数组中的最高成绩(max并不是成员函数,而是普通函数)int max(Student *arr);int main(){    int i;    Student stud[5]=    {        Student(101,78.5),Student(102,85.5),Student(103,100),        Student(104,98.5),Student(105,95.5)    };//输出第1、3、5个学生的信息(用循环语句)    for(i=0; i<5; i+=2)    {        cout<<"第"<<i+1<<"个学生信息:";        stud[i].display();        cout<<endl;    }    //输出成绩最高者的学号    cout<<"5个学生中成绩最高者的学号为: "<<max(stud);//调用函数显示最高成绩    return 0;}double Student::get1(){    return num;}double Student::get2(){    return score;}//定义函数max,返回arr指向的对象数组中的最高成绩int max(Student *arr)//求最高成绩及对应同学的学号{    int i,p=0;    double MAX=arr[0].get2();//返回最高成绩者的学号    for(i=0; i<5; i++)    {        if(arr[i].get2()>MAX)        {            MAX=arr[i].get2();            p=i;        }    }    return arr[p].get1();}void Student::display(){    cout<<num<<' '<<score<<endl;}

心得体会:对象数组需要建立构造函数哦奋斗

0 0
原创粉丝点击