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

《C++面向对象程序设计(第三版)》谭浩强编著—第三章练习—第四题详解

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

《C++面向对象程序设计(第三版)》谭浩强编著—第三章练习—第四题详解

    二话不说,上代码

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

//4.建立一个数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出1.3.5个学生的数据
#include 
using 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;
}

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

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

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