目录
作业要求
封装一个班级类,包含成员属性:学生分数数组,最大容量20,成员属性len,记录所使用数组的元素个数
成员方法:设置班级人数:void setLen(int len);
输入班级学生分数:void input();
对成绩进行排序:void sort();
输出班级成绩:void output();
要求成员属性为私有权限,成员方法为共有成员
//班级类的成员属性,顺序表结构体
typedef struct
{
int data[20];
int len;
}Seqlist;
//班级类的定义
class Ban_ji
{
private:
Seqlist seq;
public:
//设置班级人数
void set_len(int len)
{
seq.len = len;
}
//输入班级分数
void input()
{
int temp;
cout << "请输入班级分数:" << endl;
for (int i = 0; i < seq.len; i++)
{
cin >> temp;
seq.data[i] = temp;
}
}
//对成绩进行排序
void sort()
{
int temp;
int count;
for (int i = 0; i < seq.len - 1; i++)
{
count = 0;
for (int j = 0; j < seq.len - 1- i; j++)
{
if (seq.data[j] > seq.data[j+1])
{
temp = seq.data[j]; seq.data[j] = seq.data[j + 1]; seq.data[j + 1] = temp;
count++;
}
}
if (count == 0)
{
break;
}
}
}
//输出班级成绩
void output()
{
for (int i = 0; i < seq.len; i++)
{
cout << seq.data[i] << "\t";
}
cout << endl;
}
};
int main()
{
Ban_ji hqyj; //实例化对象
hqyj.set_len(5);
hqyj.input();
cout << "排序前" << endl;
hqyj.output();
hqyj.sort();
cout << "排序后" << endl;
hqyj.output();
return 0;
}
运行结果