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;
}
相关推荐
qing_0406035 分钟前
C++——继承
开发语言·c++·继承
武昌库里写JAVA6 分钟前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g7 分钟前
GESP C++四级样题卷
java·c++·算法
小叶学C++23 分钟前
【C++】类与对象(下)
java·开发语言·c++
ac-er888824 分钟前
PHP“===”的意义
开发语言·php
NuyoahC40 分钟前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_10143 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
weixin_4640780743 分钟前
C#串口温度读取
开发语言·c#
无敌の星仔1 小时前
一个月学会Java 第2天 认识类与对象
java·开发语言
豆豆1 小时前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建