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

谭浩强c++ 面向对象程序设计(第2版)
习题 3.4 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据

#include <iostream>
using namespace std;

class Student
{
    
    
private:
	int num;
	int score;
public:
	void display();
	
	Student(int n,int s):num(n),score(s){
    
    }
};
Student stud[5] = {
    
    
	Student(1,91),
	Student(2,92),
	Student(3,93),
	Student(4,94),
	Student(5,95)
};
void Student::display()
{
    
    
	cout << num << "  " << score << endl;
}
//int* temp=&stud[0];
//容易错误的点
Student* temp = stud;


int main()
{
    
    
	temp->display();
	temp++;
	temp++;
	temp->display();
	temp++;
	temp++;
	temp->display();
	//同理
	/*stud[0].display();
	stud[2].display();
	stud[4].display();*/

	system("pause");
	return 0;
}

感谢访问 如果觉得有帮助请点个赞 谢谢
欢迎交流

猜你喜欢

转载自blog.csdn.net/Captain_Aaron/article/details/105056352