#include<iostream.h> #define SIZE 3 class student { //属性 private: int sid; char name[20]; int age; char sex; float chinese; float math; float english; float total; //行为 public: Input() { cout<<"请输入一个学生信息!"<<endl; cout<<"sid = "; cin>>this->sid; cout<<"name = "; cin>>this->name; cout<<"age = "; cin>>this->age; cout<<"sex = "; cin>>this->sex; cout<<"chinese = "; cin>>this->chinese; cout<<"math = "; cin>>this->math; cout<<"english = "; cin>>this->english; cout<<endl<<endl; stutotal(); } stutotal() { this->total = this->chinese + this->math +this->english; } Output() { cout<<this->sid<<endl; cout<<this->name<<endl; cout<<this->age<<endl; cout<<this->sex<<endl; cout<<this->total<<endl<<endl; } //构造函数与析构函数 student() { cout<<"调用了一个构造函数"<<endl; } ~student() { cout<<"析构了一个函数"<<endl; } Gettotal() { return this->total; } }; studentInput(student str[],int n) { for(int i = 0;i<n;i++) { str[i].Input(); } } studentTotal(student str[],int n) { student sr; for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) { if(str[j].Gettotal <str[j+1].Gettotal) { str[j] = sr; str[j] = str[j+1]; sr=str[j+1]; } } } } studentOutput(student str[],int n) { for(int i=0;i<n;i++) { str[i].Output(); } } void main() { student str[SIZE]; studentInput(str,SIZE); studentTotal(str,SIZE); studentOutput(str,SIZE); }