设为首页 加入收藏

TOP

C++之类和对象课后习题1
2017-10-12 18:14:31 】 浏览:439
Tags:类和 对象 课后 习题

建立一个对象数组,内放5个学生的(学号,成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生的最高成绩者,并输出其学号。

#include<iostream>
using namespace std;

class Student
{public:
    Student(int=10,int=0);
    int number;
    int score;
    void display();
};

Student::Student(int num,int sco):number(num),score(sco){
}

void Student::display()
{
    cout<<number<<" "<<score<<endl;
}

void max(Student *p)
{
    int maxi=p[0].score;
    int temp=0;
    for(int i=1;i<5;i++)
    if(p[i].score>maxi)
    {
    maxi=p[i].score;
    temp=i;
    }
    cout<<p[temp].number<<" "<<maxi<<endl;
}

int main()
{
    Student stu[5]={
Student{1001,90},
Student{1002,80},
Student{1003,99},
Student{1004,84},
Student{1005,85}
};
    Student *p=&stu[0];
     max(p);
    return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇仿《雷霆战机》飞行射击手游开发-.. 下一篇VS2013 打开项目时出现 未定义标..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目