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;
}
相关推荐
weixin_5206498715 小时前
C#线程底层原理知识
java·开发语言
agilearchitect15 小时前
Matlab导入Excel表格教程:从基础到进阶全攻略
数据结构·其他·matlab·excel
AC赳赳老秦15 小时前
OpenClaw与Excel联动:批量读取/写入数据,生成可视化报表
开发语言·python·excel·产品经理·策略模式·deepseek·openclaw
code_whiter15 小时前
C++9(vector)
开发语言·c++
xieliyu.15 小时前
Java手搓数据结构:从零模拟实现单向无头非循环链表
java·数据结构·学习·链表
覆东流15 小时前
第5天:Python字符串操作进阶
开发语言·后端·python
吴梓穆15 小时前
UE5 C++ 使C++创建动画蓝图
开发语言·c++·ue5
冰暮流星15 小时前
javascript之表单事件1
开发语言·前端·javascript
0xDevNull15 小时前
队列(Queue)实战教程:从原理到架构应用
java·开发语言·后端