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;
}
相关推荐
牛奔1 分钟前
Go 如何打印调试深层或嵌套的结构体
开发语言·后端·golang
不会就选b8 分钟前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
geovindu35 分钟前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
学逆向的36 分钟前
汇编——JCC指令
开发语言·汇编·网络安全
mayaairi1 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
kite01212 小时前
Go语言Map深度解析与最佳实践
开发语言·后端·golang
稚南城才子,乌衣巷风流2 小时前
块状链表:数据结构详解与实现
数据结构·链表
hold?fish:palm3 小时前
kv存储主从复制的设计与实现
c++·redis·后端
l156469484 小时前
突围!图文混合知识库难以解析,Kimi-K3 原生视觉架构深耕知识工作,DMXAPI 统一接口,缩短项目开发周期
java·大数据·开发语言
啦啦啦啦啦zzzz4 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法