定义一个学生类CStudent,私有数据成员包括name (姓名)、id(学号)、math (数学成绩)
t题目要求
#include
#include<string.h>
using namespace std;
class CStudent
{
private:
int id;
string name;
int math;
int english;
static int count;
static int mathc;
static int englishc;
public:
void setms()
{
cout << "name id math english" << endl;
cin >> name >> id >> math >> english;
count++;
mathc = mathc + math;
englishc = englishc + english;
}
int add()
{
return math + english;
}
void outms()
{
cout << "id"<<id << endl;
cout << "math :" << math << endl << "english :" << english << endl;
}
void outave()
{
cout << " math :" << mathc << endl;
cout << " english :" << englishc << endl;
cout << " count :" << count << endl;
cout << " math average :" << mathc / count << endl;
cout << " english average :" << englishc / count << endl;
}
};
int CStudent::count = 0;
int CStudent::mathc = 0;
int CStudent::englishc = 0;
int main()
{
CStudent S[10], * p = new CStudent;;
int i, j, k = 0;
for (i = 0; i < 10; i++)
{
S[i].setms();
}
for (j = 0; j < 9; j++)
{
for (i = 0; i < 9 - j; i++)
{
if (S[i].add() > S[i + 1].add())
{
*p = S[i];
S[i] = S[i + 1];
S[i + 1] = *p;
}
}
}
for (i = 9; i > 7; i--)
{
S[i].outms();
}
delete p;
return 0;
}
原文链接:https://blog.csdn.net/qq_46798874/java/article/details/106896255