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;
}
相关推荐
wjs20246 小时前
Django Nginx+uWSGI 安装配置指南
开发语言
七夜zippoe7 小时前
JVM类加载机制(Class Loading)详解:双亲委派模型与破坏实践
java·开发语言·jvm·类加载·双亲委派
曹牧7 小时前
C#:无法从方法组转换为objec
开发语言·c#
自动化代码美学7 小时前
【Python3.13】官网学习之控制流
开发语言·windows·python·学习
AA陈超8 小时前
ASC学习笔记0020:用于定义角色或Actor的默认属性值
c++·笔记·学习·ue5·虚幻引擎
coderxiaohan9 小时前
【C++】仿函数 + 模板进阶
开发语言·c++
Kuo-Teng9 小时前
LeetCode 279: Perfect Squares
java·数据结构·算法·leetcode·职场和发展
IMPYLH10 小时前
Lua 的 collectgarbage 函数
开发语言·笔记·junit·单元测试·lua
百锦再10 小时前
第18章 高级特征
android·java·开发语言·后端·python·rust·django