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;
}
相关推荐
ulias2122 分钟前
初步了解STL和string
开发语言·c++·mfc
二川bro3 分钟前
字符串格式化进阶:Python f-string性能优化
开发语言·python
报错小能手5 分钟前
数据结构 不带头结点的链表
数据结构·链表
LitchiCheng8 分钟前
Mujoco 机械臂 OMPL 进行 RRT 关节空间路径规划避障、绕障
开发语言·人工智能·python
烤麻辣烫8 分钟前
黑马程序员苍穹外卖(新手)DAY10
java·开发语言·学习·spring·intellij-idea
希望有朝一日能如愿以偿9 分钟前
力扣每日一题:使数组和能被p整除
数据结构·算法·leetcode
zz07232010 分钟前
数据结构 —— 图
数据结构
waves浪游15 分钟前
进程控制(上)
linux·运维·服务器·开发语言·c++
程序员三明治15 分钟前
【Java】synchronized关键字详解:从字节码到对象头与锁升级
java·开发语言·juc·synchronized··锁升级
y***548816 分钟前
Rust在嵌入式中的实时操作系统
开发语言·后端·rust