c++使用Score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。

定义一个Score类,其中包括私有数据成员和公有成员函数,即
mNum 学号
mMath 高等数学成绩
mEnglish 英语成绩
mProgramming 程序设计成绩
Inscore() 输入学号和各科成绩,并且计算平均成绩
Showscore() 输出学号和各科成绩
使用Score类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。

#include "pch.h"
#include "Score.h"
#include <iostream>
using namespace std;
int main()
{
	int n,a,b,c,d;
	cin >> n;
	Score *m = new Score[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a >> b >> c >> d;
	    m[i].Inscore(a,b,c,d);
	}
	cout << "num" << '\t' << "math" << '\t' << "English" << '\t' << "programming" << '\t' << "average"<<endl;
	for (int i = 0; i < n; i++)
	{
		m[i].Showscore();
	}
	delete[]m;
}
#pragma once
class Score
{
public:
	Score();
	double Inscore(double num, double math, double eng, double pro);
	void Showscore();
private:
	double mNum, mMath, mEnglish, mProgramming;
};
//Score cpp
#include "pch.h"
#include "Score.h"
#include <iostream>
using namespace std;
Score::Score()
{
}
double Score::Inscore(double num, double math, double eng, double pro)
{
	mNum = num;
	mMath = math;
	mEnglish = eng;
	mProgramming=pro;
	return ( math + eng + pro) / 3;
}
void Score::Showscore()
{
	cout << mNum << '\t' << mMath << '\t' << mEnglish << '\t' << mProgramming << '\t' << Score::Inscore(mNum, mMath, mEnglish, mProgramming)<<endl;
}

  • 0
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个可能的中文回答: 为了定义一个描述学生基本情况的Student,数据成员包括姓名、学号、C成绩、英语和数学成绩,成员函数包括输出所有信息函数(姓名、学号各科成绩)、成绩函数和平均成绩函数。要编写构造函数、拷贝构造函数,不要将所有成员函数实现写在class大括号内。编写主函数测试该。 以下是示例代码: ``` #include <iostream> #include <string> using namespace std; class Student { private: string name; // 姓名 int id; // 学号 int c_score; // C成绩 int eng_score; // 英语成绩 int math_score; // 数学成绩 public: // 构造函数 Student(string name_, int id_, int c_, int eng_, int math_) : name(name_), id(id_), c_score(c_), eng_score(eng_), math_score(math_) {} // 拷贝构造函数 Student(const Student& other) : name(other.name), id(other.id), c_score(other.c_score), eng_score(other.eng_score), math_score(other.math_score) {} // 输出所有信息 void display_info() { cout << "姓名:" << name << endl; cout << "学号:" << id << endl; cout << "C成绩:" << c_score << endl; cout << "英语成绩:" << eng_score << endl; cout << "数学成绩:" << math_score << endl; } // 成绩 int total_score() { return c_score + eng_score + math_score; } // 平均成绩 double average_score() { return total_score() / 3.0; } }; // 主函数 int main() { Student s1("张三", 1001, 90, 80, 85); s1.display_info(); cout << "总成绩:" << s1.total_score() << endl; cout << "平均成绩:" << s1.average_score() << endl; Student s2 = s1; // 测试拷贝构造函数 s2.display_info(); return 0; } ``` ### 回答2: 定义一个描述学生基本情况的Student,可以按照以下方式实现: ```cpp #include <iostream> #include <string> using namespace std; // 学生定义 class Student{ private: string name; int studentId; float cScore; float englishScore; float mathScore; public: // 构造函数 Student(string n, int id, float c, float english, float math){ name = n; studentId = id; cScore = c; englishScore = english; mathScore = math; } // 拷贝构造函数 Student(const Student& copy){ name = copy.name; studentId = copy.studentId; cScore = copy.cScore; englishScore = copy.englishScore; mathScore = copy.mathScore; } // 输出所有信息 void printInfo(){ cout << "姓名:" << name << endl; cout << "学号:" << studentId << endl; cout << "C语言成绩:" << cScore << endl; cout << "英语成绩:" << englishScore << endl; cout << "数学成绩:" << mathScore << endl; } // 成绩 float totalScore(){ return cScore + englishScore + mathScore; } // 平均成绩 float averageScore(){ return totalScore() / 3; } }; int main(){ // 创建一个学生对象 Student s1("张三", 20210001, 90, 80, 95); // 测试输出所有信息 s1.printInfo(); // 测试成绩平均成绩 cout << "总成绩:" << s1.totalScore() << endl; cout << "平均成绩:" << s1.averageScore() << endl; return 0; } ``` 运行结果: ``` 姓名:张三 学号:20210001 C语言成绩:90 英语成绩:80 数学成绩:95 总成绩:265 平均成绩:88.3333 ``` 这样就可以创建一个描述学生基本情况的Student,实现了构造函数、拷贝构造函数和成员函数,并且在主函数中进行了测试。 ### 回答3: 下面是一个描述学生基本情况的Student的实现: ```cpp #include <iostream> #include <string> using namespace std; // 定义学生 class Student { private: string name; // 姓名 string id; // 学号 float cScore; // C成绩 float englishScore; // 英语成绩 float mathScore; // 数学成绩 public: // 构造函数 Student(string n, string i, float c, float e, float m) { name = n; id = i; cScore = c; englishScore = e; mathScore = m; } // 拷贝构造函数 Student(const Student& s) { name = s.name; id = s.id; cScore = s.cScore; englishScore = s.englishScore; mathScore = s.mathScore; } // 输出所有信息函数 void printInfo() { cout << "姓名:" << name << endl; cout << "学号:" << id << endl; cout << "C成绩:" << cScore << endl; cout << "英语成绩:" << englishScore << endl; cout << "数学成绩:" << mathScore << endl; } // 成绩函数 float getTotalScore() { return cScore + englishScore + mathScore; } // 平均成绩函数 float getAverageScore() { return getTotalScore() / 3; } }; int main() { // 创建学生对象 Student student1("张三", "20210001", 90, 80, 85); Student student2 = student1; // 使用拷贝构造函数创建新对象 // 输出学生信息 student1.printInfo(); cout << "总成绩:" << student1.getTotalScore() << endl; cout << "平均成绩:" << student1.getAverageScore() << endl; // 输出拷贝构造的学生信息 student2.printInfo(); cout << "总成绩:" << student2.getTotalScore() << endl; cout << "平均成绩:" << student2.getAverageScore() << endl; return 0; } ``` 该程序定义了一个名为Student的,包含了学生的基本信息和相关函数。在主函数中,我们创建了两个学生对象,并分别调用了相关函数进行测试,并使用了拷贝构造函数创建了一个新的学生对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值