1、声明一个基类Student(学生)类,声明一个派生出新类college_student (大学生)类,并继承Student(学生)类,要求:
(1) 在Student类中包括:num(学号),name(姓名),sex(性别)等成员数据,viod display();(输出学生学号,姓名,性别)函数成员,构造函数自己设计。
(2) 在派生出新类college_student (大学生)类中包括:Chinese(语文分),math(数学分),computer(计算机分)等成员数据,float total_score();(统计一个大学生的三科总分)。
(3) 建立一个对象数组,内放5位大学生的数据(学号、姓名、性别、语文分、数学分、计算机分),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中总成绩最高者,并输出该学生的所有信息(学号、姓名、性别、语文分、数学分、计算机分,及总分)。
(4) 对5位大学生按总分进行排名,并输出排名后的结果。
#include <string>
#include <iostream>
using namespace std;
class Student {
public:
Student(int num, string name, bool is_male): num_(num), name_(name), sex_(is_male) {}
virtual ~Student() {}
virtual void display(void) {
cout << "学号:" << num_ << " 姓名:" << name_ << " 性别:" << sex() << endl;
}
string sex()
{
return sex_ ? "男" : "女";
}
int num() { return num_; }
string name() { return name_; }
private:
int num_ = 0;//序号
string name_;//姓名
bool sex_;//性别, true 男 false 女
};
class college_student
: public Student {
public:
college_student(int num, string name, bool is_male, float chinese, float math, float computer)
: Student(num, name, is_male)
, chinese_(chinese)
, math_(math)
, computer_(computer) {}
~college_student() {}
void display() {
Student::display();
cout << "chinese = " << chinese_ << " math = " << math_ << " computer = " << computer_ << " total = " << total_score() << endl;
}
float total_score() { return chinese_ + math_ + computer_; }
private:
float chinese_ = 0.0;
float math_ = 0.0;
float computer_ = 0.0;
};
college_student students[] = {
{1, "张三丰", true, 88.5, 90.0, 77.0},
{2, "王晓红", false, 88, 91.0, 87.0},
{3, "段誉", true, 68.5, 90.0, 76.0},
{4, "王重阳", true, 83.5, 93.0, 97.0},
{5, "林黛玉", false, 82.5, 92.0, 97.0},
};
void max(college_student* students, int count)
{
float score = 0.0;
college_student* pStu = students;
for (int i=0;i<count; ++i)
{
if (pStu->total_score() > students[i].total_score())
{
pStu = &students[i];
}
}
pStu->display();
}
void sort(college_student* students, int count)
{
college_student** tempArray = new college_student*[count];
//copy
for (int i=0; i<count; ++i)
{
tempArray[i] = &students[i];
}
//sort
for (int i=0; i<count - 1; ++i)
{
for (int j= 0; j<count - 1 - i; ++j)
{
if (tempArray[j] < tempArray[j + 1])
{
college_student* temp = tempArray[j];
tempArray[j] = tempArray[j + 1];
tempArray[j + 1] = temp;
}
}
}
//print
for (int i = 0; i < count; ++i)
{
tempArray[i]->display();
}
delete[] tempArray;
}
int main() {
//max
max(students, sizeof(students) / sizeof(college_student));
//sort
sort(students, sizeof(students) / sizeof(college_student));
//wait
int i = 0;
cin >> i;
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Student
{
private:
string num;
string name;
bool sex;
public:
Student(string num, string name, bool sex);
void display();
string GetName() { return name; }
};
class college_student :public Student
{
public:
float Chinese;
float math;
float computer;
float total_score();
college_student(string num, string name, bool sex, float chinese, float math, float computer);
void display();
string GetName() { return Student::GetName(); }
};
Student::Student(string num, string name, bool sex)
{
this->num = num;
this->name = name;
this->sex = sex;
}
void Student::display()
{
cout << "学号:" << num << " ";
cout << "姓名:" << name << " ";
if (sex) cout << "性别:男" << " ";
else cout << "性别:女" << " ";
}
float college_student::total_score()
{
return Chinese + math + computer;
}
college_student::college_student(string num, string name, bool sex, float chinese, float math, float computer):Student(num,name,sex)
{
this->Chinese = chinese;
this->math = math;
this->computer = computer;
}
void college_student::display()
{
Student::display();
cout << "语文:" << Chinese << " ";
cout << "数学:" << math << " ";
cout << "电脑:" << computer << " ";
cout << "总分:" << total_score() << endl;
}
void max(college_student** student, int num)
{
int id;
float max_num=0;
for (int i = 0; i < num; i++)
{
if (student[i]->total_score() > max_num)
{
max_num = student[i]->total_score();
id = i;
}
}
cout << "总分最高:" << endl;
student[id]->display();
}
bool CMP(college_student* s1, college_student* s2)
{
return s1->total_score() > s2->total_score();
}
int main()
{
college_student* student[5];
student[0] = new college_student("0001", "李明", true,95.6,94.6,85.7);
student[1] = new college_student("0002", "张三", true, 95.6, 92.6, 85.7);
student[2] = new college_student("0003", "赵雪", false, 95.6, 91.2, 85.7);
student[3] = new college_student("0004", "陈丽", false, 93, 94.6, 85.7);
student[4] = new college_student("0005", "王强", true, 75.4, 94.6, 85.7);
cout << "学生原始信息:" << endl;
cout <<"-----------------------------------------------------------------"<<endl;
cout << "学号\t姓名\t性别\t语文\t数学\t电脑\t总分" << endl;
cout << "0001\t" << "庄周\t" << "女\t" << 98.6 << "\t" << 90 << "\t" << 85 <<"\t"<<student[0]->total_score() << endl;
cout << "0002\t" << "庄徳彪\t" << "男\t" << 97 << "\t" << 96 << "\t" << 87 <<"\t"<< student[1]->total_score() << endl;
cout << "0003\t" << "庄大包\t" << "女\t" << 95 << "\t" << 91 << "\t" << 75 <<"\t"<< student[2]->total_score() << endl;
cout << "0004\t" << "庄如来\t" << "女\t" << 93 << "\t" << 96 << "\t" << 77 <<"\t"<< student[3]->total_score() << endl;
cout << "0005\t" << "庄云龙\t" << "男\t" << 75 << "\t" << 94 << "\t" << 57 <<"\t"<< student[4]->total_score() << endl;
cout << endl;
cout <<"------------------------------------------------------------------"<<endl;
max(student, 5);
cout << endl;
sort(student, student + 5, CMP);
cout <<"---------------------------------------------------------------------"<<endl;
cout << "总分排名:" << endl;
cout <<"----------------------------------------------------------------------"<<endl;
for (int i = 0; i < 5; i++)
{
cout << i + 1 << " ";
student[i]->display();
}
}