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;
}
相关推荐
野生的编程萌新27 分钟前
从冒泡到快速排序:探索经典排序算法的奥秘(二)
c语言·开发语言·数据结构·c++·算法·排序算法
Full Stack Developme27 分钟前
Java后台生成多个Excel并用Zip打包下载
java·开发语言·excel
Brookty29 分钟前
【Java学习】锁、线程死锁、线程安全2
java·开发语言·学习·java-ee
weixin_307779131 小时前
VS Code配置MinGW64编译backward库
开发语言·c++·vscode·算法
Crazy_eater1 小时前
C++继承(1)
c++
百锦再1 小时前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
花开富贵ii2 小时前
代码随想录算法训练营四十三天|图论part01
java·数据结构·算法·深度优先·图论
破刺不会编程3 小时前
socket编程UDP
linux·运维·服务器·网络·c++·网络协议·udp
布朗克1683 小时前
Java 10 新特性及具体应用
java·开发语言·新特性·java10
code小毛孩3 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode