//小型学生成绩管理程序
#include
#include
// Declaration and definition of class base_Student
class base_Student
{
public:
base_Student(); //constructor
base_Student(base_Student&); //copy constructor
char* operator =(char Name1[]) //overload operator =
{
return Name1;
}
void Initial(void);
float ShowScore(void);
void Show(void);
private:
int ID;
char Name[20];
float Score;
};
//constructor
base_Student::base_Student()
{
ID=0;
Score=0;
}
//copy constructor
base_Student::base_Student(base_Student& Student1)
{
Student1.ID=ID;
strcpy(Student1.Name,Name);
Student1.Score=Score;
}
//initialize student's data
void base_Student::Initial(void)
{
cout<<"ID:";
cin>>ID;
cout<<"Name:";
cin>>Name;
cout<<"Score:";
cin>>Score;
}
//return student's score
float base_Student::ShowScore(void)
{
return Score;
}
//display student's information
void base_Student::Show(void)
{
cout<
cout<<"ID:"<
cout<<"Name:"<
cout<<"Score:"<
}
// Declaration and definition of class Student derived from class base_Student
class Student:public base_Student
{
friend class StudentManager; //definite StudentManager as friend class
public:
Student();
Student(Student&);
void ChangeOrder(int i){Order=i;};
int ShowOrder();
void Show(void);
private:
int Order;
};
//constructor
Student::Student()
{
Order=0;
}
//copy constructor
Student::Student(Student& Student1)
{
Student1.Order=Order;
}
//return Order
int Student::ShowOrder()
{
return Order;
}
//return student's information
void Student::Show()
{
base_Student::Show();
cout<<"Order:"<
}
// Declaration and definition of class StudentManager for student management
class StudentManager
{
public:
StudentManager();
void SetClassName(char*);
int SetSize(int i);
void Input(void);
float Average(void);
int Swap(Student&,Student&);
void SetOrder(void);
void SortAsOrder(void);
int Show(void);
private:
char* ClassName;
Student students[60];
int Size;
float Aver_Score;
};
StudentManager::StudentManager()
{
ClassName=NULL;
}
void StudentManager::SetClassName(char* newClassName)
{
ClassName=newClassName;
}
//set class size
int StudentManager::SetSize(int i)
{
Size=i;
return 0;
}
//input every student's information of the class
void StudentManager::Input(void)
{
int i;
cout<<"Please input students' information:"<
for(i=0;i
{
cout<
students[i].Initial();
students[i].Order=i+1;
}
}
//calculate average score of the class
float StudentManager::Average(void)
{
int i(0);
float sum(0.0);
for(i=0;i
sum+=students[i].ShowScore();
Aver_Score=sum/Size;
cout< return Aver_Score;} //swap two students's dataint StudentManager::Swap(Student& Student1,Student& Student2) { Student Temp; Temp=Student1; Student1=Student2; Student2=Temp; return 0;} //set student's order according to his scorevoid StudentManager::SetOrder(void) { int i(0),j(0); for(i=0;i for(j=i;j if(students[j].ShowScore()>students[i].ShowScore()) { students[i].ChangeOrder(j+1); students[j].ChangeOrder(i+1); }} //exchange student's position according to his scorevoid StudentManager::SortAsOrder(void) { int i; for(i=0;i Swap(students[i],students[students[i].ShowOrder()-1]);} //display the whole class's informationint StudentManager::Show() { int i; cout< for(i=0;i students[i].Show(); return 0;} //main entry of the programint main(void){ int n; char ClassName[10]; StudentManager stuManager; cout<<"Please input your class's name:"< cin>>ClassName; stuManager.SetClassName(ClassName); cout<<"How many students are there in your class?"< cin>>n; stuManager.SetSize(n); stuManager.Input(); stuManager.Average(); stuManager.SetOrder(); stuManager.Show(); stuManager.SortAsOrder(); cout< stuManager.Show(); return 0;}