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

本文通过一个具体的C++程序实例,演示了如何使用对象数组存储学生数据,并利用指针操作来访问特定元素,展示了面向对象编程中数组与指针的基本用法。

谭浩强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;
}

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值