建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。

【问题描述】

建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。

【样例输入】

无。

【样例输出】

101 78.5

103 98.5

105 95.5
//补充程序
#include<iostream>
using namespace std;
class Student
{
private:
	int id;
	float scores;
public:
	void display() 
	{ 
		cout << id << " " << scores << endl; 
	}
	Student(int n, int m)//构造函数
	{
		id = n;
		scores = m;
	}
};


int  main()
{
	Student  stud[5] = {
		Student(101,78.5),Student(102,85.5),Student(103,98.5),
		Student(104,100.0),Student(105,95.5) };
	Student  *p = stud;
	for (int i = 0;i <= 2;p = p + 2, i++)
		p->display();
	return  0;
}
发布了20 篇原创文章 · 获赞 1 · 访问量 205

猜你喜欢

转载自blog.csdn.net/weixin_45491054/article/details/104914851