栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++对象数组

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++对象数组

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

输入:

定义数组时给定5个学生的数据 (1,90), (2,61.5), (3,90.5), (4,82.5), (5,76.5)

输出:

输出学号3

示例:

input

Student(1,90),Student(2,61.5),Student(3,90.5),Student(4,82.5),Student(5,76.5)

output

3

提示:

不要用键盘输入,直接用给定的数组值赋值;输出必须是3

代码如下

#include 
using namespace std;
class Student
{
public:
	Student(int n, float s) : num(n), score(s) {}
	int num;
	float score;
};
void max(Student *pt, int n)
{
	Student *p = pt, ma(0, 0);
	float max;
	for (max = pt->score; p < pt + n; p++)
		if ((p->score) > max) {
			max = p->score;
			ma = *p;
		}
	cout << ma.num  << endl;
}
int main()
{
	Student stu[5] = {
		Student(1, 90),
		Student(2, 61.5),
		Student(3, 90.5),
		Student(4, 82.5),
		Student(5, 76.5)
	};
	max(stu, 5);
	//system("pause");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号