C++考试成绩统计(类实现)

题目:有三名同学,在一次考试中三科成绩分别如下表,请输出三名同学的平均成绩:

|----|-----|-----|-----|
| | 语文 | 数学 | 英语 |
| 张三 | 100 | 100 | 100 |
| 李四 | 90 | 50 | 100 |
| 王五 | 60 | 70 | 80 |

cpp 复制代码
#include <iostream>
#include <string>
/*考试成绩统计*/
using namespace std;

class Score {
	private :
		string id;
		int chinese;
		int english;
		int math;
	public :
		Score(string name, int score1, int score2, int score3) : id(name), chinese(score1), english(score2), math(score3) {}
		
		double getAvgScore(){
			return (chinese + math + english) / 3.0;
		}
		
		string getName() {
			return id;
		}
}; 

int main() {
	Score s1("张三", 100, 100, 100);
	Score s2("李四", 90, 50, 100);
	Score s3("王五", 60, 70, 80);
	
	cout << s1.getName() << "的平均成绩是" << s1.getAvgScore() << endl;
	cout << s2.getName() << "的平均成绩是" << s2.getAvgScore() << endl;
	cout << s3.getName() << "的平均成绩是" << s3.getAvgScore() << endl;
	
	return 0;
}
相关推荐
Dream it possible!20 分钟前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
yi碗汤园31 分钟前
【一文了解】C#反射
开发语言·unity·c#
sin_hielo41 分钟前
leetcode 2536
数据结构·算法·leetcode
小羊失眠啦.1 小时前
用 Rust 实现高性能并发下载器:从原理到实战
开发语言·后端·rust
避避风港1 小时前
Java 抽象类
java·开发语言·python
cookies_s_s1 小时前
C++20 协程
linux·开发语言·c++
what_20181 小时前
list集合使用
数据结构·算法·list
石油人单挑所有1 小时前
C语言知识体系梳理-第一篇
c语言·开发语言
hetao17338372 小时前
2025-11-13~14 hetao1733837的刷题记录
c++·算法
把csdn当日记本的菜鸡2 小时前
js查缺补漏
开发语言·javascript·ecmascript