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;
}
相关推荐
m0_74825238几秒前
Ruby 模块(Module)的基本概念
开发语言·python·ruby
羊小猪~~3 分钟前
【QT】-- QT基础类
开发语言·c++·后端·stm32·单片机·qt
木卫二号Coding14 分钟前
Python-文件拷贝+文件重命名+shutil+记录
开发语言·python
leaves falling14 分钟前
冒泡排序(基础版+通用版)
数据结构·算法·排序算法
bubiyoushang88832 分钟前
基于Q-learning的路径规划MATLAB仿真程序实现
开发语言·matlab
C雨后彩虹37 分钟前
无向图染色
java·数据结构·算法·华为·面试
FAFU_kyp43 分钟前
Rust 结构体(struct)
开发语言·后端·rust
努力写代码的熊大1 小时前
深入探索C++关联容器:Set、Map、Multiset与Multimap的终极指南及底层实现剖析
开发语言·c++
程序员-King.1 小时前
二分查找——算法总结与教学指南
数据结构·算法
J_liaty1 小时前
Java工程师的JVM入门教程:从零理解Java虚拟机
java·开发语言·jvm