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;
}
相关推荐
郝学胜_神的一滴几秒前
干货版《算法导论》09:让哈希表稳如泰山的终极解法
数据结构·算法
kyle~7 分钟前
DDS分布式实时系统---自省机制
开发语言·分布式·机器人·c#·接口·ros2
yujunl8 分钟前
Integrated Security=True(Windows 集成身份验证)
开发语言
右耳朵猫AI9 分钟前
Python周刊2026W23 | Polars 1.41、PyPy v7.3.23、Python 3.15、httpx2、dj-lite-tenant
开发语言·python
洛水水13 分钟前
【力扣100题】88.多数元素
数据结构·算法·leetcode
昭昭颂桉a15 分钟前
TypeScript 前端的必修课,从 JS 到 TS
开发语言·前端·javascript·typescript
何以解忧,唯有..17 分钟前
Go 语言安装与环境配置完整指南
开发语言·后端·golang
alwaysrun17 分钟前
C++之常量体系const
c++·后端·程序员
郝学胜_神的一滴18 分钟前
CMake 016:深入浅出变量核心用法
c++·cmake
Java面试题总结21 分钟前
MarkItDown 再次登顶GitHub榜
开发语言·c#·github