C++练习建立一个对象数组

本文通过一系列C++代码示例,展示了如何创建对象数组存储学生数据,包括输出指定学生成绩、查找最高分、使用常量对象、修改对象数据成员、通过函数调用操作对象以及友元函数在类模板中的应用。这些示例深入浅出地解释了C++中类和对象的高级特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第三章

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

#include<iostream>
using namespace std;
class Student {
public:
	Student(int s = 0, int sc = 0) :sno(s), score(sc) {};
		
	void display() {
		cout << "学号:" << sno << "分数:" << score << endl;
	}
private:
	int sno;
	int score;

};
int main() {
	Student s[5] = {
		Student(1,60),
		Student(2,70),
		Student(3,80),
		Student(4,90),
		Student(5,100)
	};
	Student* p;
	for ( p = s; p < (s+5); p+=2) {
		p->display();
	}
    return 0;
}

结果:

 5.建立一个对象数组,内存放5个学生数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

#include<iostream>
using namespace std;
class Student {
public:
	Student(int s = 0, int sc = 0) :sno(s), score(sc) {};
		
	void display() {
		cout << "学号:" << sno << "分数:" << score << endl;
	}
	int get_score() {
		return score;
	}
	int get_sno(){
		return sno;
	}
private:
	int sno;
	int score;

};

void max(Student* p,Student s[]) {
	Student* max =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值