//1.定义一个学生类,包含姓名、年龄、绩点,要求能够初始化学生信息(定义常规构造函数和拷贝构造函数)、显示学生信息、设置学生信息;
//
//2.在主函数中定义一个学生数组(普通数组或者动态数组都可以),大小为5,然后利用设置学生信息的成员函数输入每个学生信息;
//
//3.定义student类的友元函数Sort实现按绩点排序,以对象指针(或对象数组)和数组大小为参数,对学生按绩点由高到低排序。
// 在主函数中调用函数sort,输出排序后的学生信息。
//
//在此基础上,为student类增加:
//
//(1)静态数据成员 count, 负责统计学生总数,并实现其值的更新;
//
//(2)Date 类的成员对象 birthday, 并为student类定义相应的构造函数;
#include<iostream>
#include<string>
using namespace std;
class Date
{
int year;
int month;
int day;
public:
Date(int year = 0, int month = 0, int day = 0);
int Getyear() { return year; }
int Getmonth() { return month; }
int Getday() { return day; }
};
//************************
Date::Date(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
//*************************
class Student
{
static int Count;
string Name;
int Age;
double Point;
Date Birthday;
public:
Student();
Student(string name, int age, double point, Date birthday);
Student(const Student& a);
~Student();
int Getcount();
void Show();
void Set(string name, int age, double point, Date birthday);
friend void Sort(Student a[], int b);
};
//****************************
int Student::Count = 0;
Student::Student()
{
this->Name = "0";
this->Age = 0;
this->Point = 0;
this->Birthday = 0;
}
Student::Student(string name, int age, double point, Date birthday)
{
this->Name = name;
this->Age = age;
this->Point = point;
this->Birthday = birthday;
}
Student::Student(const Student& a)
{
this->Name = a.Name;
this->Age = a.Age;
this->Point = a.Point;
this->Birthday = a.Birthday;
}
Student::~Student()
{
}
int Student::Getcount()
{
Count = 5;
return Count;
}
void Student::Show()
{
cout << this->Name << " " << this->Age << " "
<< " " << this->Point << endl;
}
void Student::Set(string name, int age, double point, Date birthday)
{
this->Name = name;
this->Age = age;
this->Point = point;
this->Birthday = birthday;
}
void Sort(Student a[], int b)
{
for (int i = 0; i < b; i++)
{
for (int j = i + i; j < b; j++)
{
if (a[i].Point < a[j].Point)
{
Student temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
//**********************
int main()
{
cout << "请输入学生信息:" << endl;
string name; int age; double point;
int year, month, day;
Student a[5];
for (int i = 0; i < 5; i++)
{
cin >> name >> age >> point >> year >> month >> day;
Date birthday(year, month, day);
a[i].Set(name, age, point, birthday);
}
Sort(a, 5);
cout << "共有学生" << a[0].Getcount() << "名。" << endl;
cout << "分别为:" << endl;
for (int i = 0; i < 5; i++)
{
a[i].Show();
}
return 0;
}
学生类的创建与完善,如何解决
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- apples_kk 2023-03-29 15:08关注
以下是实现该功能的代码示例(包含对Date类的定义):
#include <iostream> #include <string> #include <algorithm> using namespace std; // Date 类定义,用于保存学生的出生日期 class Date { public: Date(int y, int m, int d) : year(y), month(m), day(d) {} friend bool operator< (const Date& lhs, const Date& rhs); friend bool operator== (const Date& lhs, const Date& rhs); friend ostream& operator<< (ostream& os, const Date& date); private: int year; int month; int day; }; bool operator< (const Date& lhs, const Date& rhs) { if (lhs.year != rhs.year) { return lhs.year < rhs.year; } else if (lhs.month != rhs.month) { return lhs.month < rhs.month; } else { return lhs.day < rhs.day; } } bool operator== (const Date& lhs, const Date& rhs) { return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day; } ostream& operator<< (ostream& os, const Date& date) { os << date.year << "/" << date.month << "/" << date.day; return os; } // Student 类定义,用于保存学生的信息 class Student { public: Student() : name(""), age(0), score(0.0), birthday(2000, 1, 1) { ++count; } Student(const string& name_, int age_, double score_, const Date& birthday_) : name(name_), age(age_), score(score_), birthday(birthday_) { ++count; } Student(const Student& other) { name = other.name; age = other.age; score = other.score; birthday = other.birthday; ++count; } ~Student() { --count; } // 访问器和修改器 string getName() const { return name; } void setName(const string& name_) { name = name_; } int getAge() const { return age; } void setAge(int age_) { age = age_; } double getScore() const { return score; } void setScore(double score_) { score = score_; } // 友元函数 friend void Sort(Student* arr, int size); friend ostream& operator<< (ostream& os, const Student& student); private: string name; int age; double score; Date birthday; static int count; }; int Student::count = 0; // 比较函数,用于排序 bool cmp(const Student& lhs, const Student& rhs) { return lhs.score > rhs.score; } // Sort 函数,用于按照绩点排序 void Sort(Student* arr, int size) { sort(arr, arr + size, cmp); } ostream& operator<< (ostream& os, const Student& student) { os << "姓名:" << student.name << ",年龄:" << student.age << ",绩点:" << student.score << ",出生日期:" << student.birthday; return os; } int main() { // 定义学生数组 Student students[5]; // 输入学生信息 for (int i = 0; i < 5; ++i) { string name; int age; double score; int year, month, day; cout << "请输入第" << i + 1 << "个学生的信息:" << endl; cout << "姓名:"; cin >> name; cout << "年龄:"; cin >> age; cout << "绩点:"; cin >> score; cout << "出生年份:"; cin >> year; cout << "出生月份:"; cin >> month; cout << "出生日期:"; cin >> day; students[i] = Student(name, age, score, Date(year, month, day)); } // 按照绩点排序 Sort(students, 5); // 输出学生信息 cout << endl; for (int i = 0; i < 5; ++i) { cout << students[i] << endl; } // 输出学生总数 cout << endl << "学生总数为:" << Student::count << endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 在不同的执行界面调用同一个页面
- ¥20 基于51单片机的数字频率计
- ¥50 M3T长焦相机如何标定以及正射影像拼接问题
- ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
- ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
- ¥15 C++map释放不掉
- ¥15 Mabatis查询数据
- ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
- ¥15 关于E22-400T22S的LORA模块的通信问题
- ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路