二话不说,上代码
题目:建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1、3、5个学生的数据。
//4.建立一个数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出1.3.5个学生的数据 #includeusing namespace std; class Student { public: Student(int n, float s) :num(n), score(s) {}//定义构造函数 void display(); //声明用于输出的函数 private: int num; float score; }; void Student::display()//定义输出函数 { cout << "Students' data: " << num << " " << score << endl; } int main() { int i = 0; Student stu[5] = //定义对象数组 { Student(1001,76.50), Student(1002,91.50), Student(1003,88.60), Student(1004,45.60), Student(1005,92.40) }; Student* pe; //声明指针 while (i < 5)//此循环 控制只输出第1.3.5个数据 { pe = &stu[i];//指向对象 pe->display();//调用输出函数 i = i + 2; } return 0; }



