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;
}
相关推荐
维吉斯蔡2 小时前
【VS Code / Cursor】文件夹右键快捷打开与文件类型自动关联
开发语言·程序人生·学习方法
luj_17684 小时前
星火科技助力边远地区防病攻坚
c语言·开发语言·c++·经验分享·算法
always_TT5 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
xcLeigh5 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
zmzb01035 小时前
C++课后习题训练记录Day175
开发语言·c++
啦啦啦啦啦zzzz6 小时前
工具:动态类工厂和用配置文件存储属性
c++·设计模式·工具·动态工厂
脱胎换骨-军哥6 小时前
C++ 代码规范与格式化指南
开发语言·c++·代码规范
木木子226 小时前
# 鸿蒙 ArkTS 实战:秒表 Stopwatch(示例 5)
数据结构·华为·list·harmonyos
枕星而眠7 小时前
C++ STL Map容器完全指南:从有序红黑树到无序哈希表
java·开发语言
geats人山人海7 小时前
数据结构第六章c语言 树的存储下二叉树的概念和存储
c语言·数据结构·算法