声明一个Course类,类中包含课程代码、课程名称、课程学分信息。声明一个Score类,类中包含课程代码、成绩。声明一个Student类,类中包含学号、姓名、性别、专业、班级、电话、Course对象成员、Score对象成员。另外total_course(选课门数)用于统计该学生选课情况、total_score(总成绩)用于统计该学生总成绩、avg_score平均成绩、total_student(总学生人数)用于统计该班有多少学生。
要求:适当的程序输入,实现并完成:
(1)打印学生信息
(2)打印该学生的课程信息
(3)打印该学生的成绩
(4)统计该学生所修课程门数
(5)该学生平均成绩
(6)统计总共有多少学生
本次实验代码量比较多,而且有一点小小的复杂;
/*各个student对象中的对象成员中的total_tem_course ,total_tem_score都是静态数据成员为了在student对象中互不影响,
必须进行一定的改进,我选择把要统计的总课程数和总成绩放在course和score对象中,,将静态数据成员作为一个统计方法,
最后一个course和score对象中的数据就是总成绩和总课程数,初始化一个对象后,就将静态数据成员归零;*/
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
class Course {//课程类
public:
//重载Course构造函数 为有参和无参构造函数赋值
Course(int id, string name, double credit);
Course()
{
c_id = 0;
c_name = "0";
c_credit = 0;
total_course = total_tem_course;
}
//提供函数使student对象的总课程数互不影响
static void Rewind_total_course()
{
total_tem_course = 0;
}
//打印学生课程信息
void Show_Course();
//提供函数返回总课程数
double Show_total_course()
{
return total_course;
}
private:
int c_id;
string c_name;
double c_credit;
double total_course;
static double total_tem_course;
};
class Score {//成绩类
public:
//重载Score函数对有参和无参对象赋值
Score(int id, double score);
Score()
{
s_id = 0;
s_score = 0;
total_score = total_tem_score;
}
//打印学生成绩信息
void Show_Score();
//计算学生总成绩并返回
double Show_total_score()
{
return total_score;
}
//提供函数使student对象的总成绩互不影响
static void Rewind_total_score() { total_tem_score = 0; }
private:
int s_id;
double s_score;
double total_score;
static double total_tem_score;
};
class Student {//学生类
public:
static int total_student; //重载构造函数 分别用来初始化一门课或两门课de对象
Student(int id, string name, string sex, string major, int classes, string tel,
int c_id, string c_name, double c_credit,
int s_id, double s_score);//学生的信息有点多,构造函数参数就显得很多,别被吓住了,
//学生只有一门课程的构造函数
Student(int id, string name, string sex, string major, int classes, string tel,
int c1_id, string c1_name, double c1_credit, int s1_id, double s1_score,
int c2_id, string c2_name, double c2_credit, int s2_id, double s2_score);
//学生有两门课程的构造函数,理论上可以有无数多门课,也就有无数多个构造函数了,
//为了方便只写两个
void Show_Student();//打印学生信息
void Show_Student_Course();//Student对象打印学生课程信息
void Show_Student_Score();//Student对象打印学生成绩信息
void Show_total_Course()
{
cout << "选课门数:";
cout << C2.Show_total_course();
}//调用成员对象的成员函数 获取student对象的总课程数
void Show_ave_Score();//打印学生平均成绩
private:
int s_id;
string s_name;
string s_sex;
string s_major;
int s_class;
string s_tel;
Score S1, S2;//成绩类对象成员,储存学生的成绩
Course C1, C2;//课程类对象成员,储存学生课程信息
};
//*********************以下为各个函数的定义
Course::Course(int id, string name, double credit)
{
c_id = id;
c_name = name;
c_credit = credit;
++total_tem_course;
total_course = total_tem_course;
}
Score::Score(int id, double score)
{
s_id = id;
s_score = score;
total_tem_score += s_score;
total_score = total_tem_score;
}
//重载构造函数 分别用来初始化一门课或两门课的对象
Student::Student(int id, string name, string sex, string major, int classes, string tel,
int c_id, string c_name, double c_credit,
int s1_id, double s_score) :C1(c_id, c_name, c_credit), S1(s1_id, s_score)
{
s_id = id;
s_name = name;
s_sex = sex;
s_major = major;
s_class = classes;
s_tel = tel;
++total_student;
}
Student::Student(int id, string name, string sex, string major, int classes, string tel,
int c1_id, string c1_name, double c1_credit, int s1_id, double s1_score,
int c2_id, string c2_name, double c2_credit, int s2_id, double s2_score) :C1(c1_id, c1_name, c1_credit), C2(c2_id, c2_name, c2_credit), S1(s1_id, s1_score), S2(s2_id, s2_score)
{
s_id = id;
s_name = name;
s_sex = sex;
s_major = major;
s_class = classes;
s_tel = tel;
++total_student;
}
void Course::Show_Course()
{
if (c_id != 0)
{
cout << "课程代码:" << c_id << endl;
cout << "课程名称:" << c_name << endl;
cout << "课程学分:" << c_credit << endl;
}
}
void Score::Show_Score()
{
if (s_id != 0)
{
cout << "课程代码:" << s_id << endl;
cout << "课程成绩为:" << s_score << endl;
}
}
void Student::Show_Student()
{
cout << endl;
cout << "学号:" << s_id << endl;
cout << "姓名:" << s_name << endl;
cout << "性别:" << s_sex << endl;
cout << "专业:" << s_major << endl;
cout << "班级:" << s_class << endl;
cout << "电话号码:" << s_tel << endl;
}
void Student::Show_Student_Course()
{
cout << "学号:" << s_id << endl;
cout << "姓名:" << s_name << endl;
C1.Show_Course();
C2.Show_Course();
}
void Student::Show_Student_Score()
{
cout << "学号:" << s_id << endl;
cout << "姓名:" << s_name << endl;
cout << "该生成绩信息:" << endl;
S1.Show_Score();
S2.Show_Score();
}
void Student::Show_ave_Score()
{
cout << "该生平均成绩";
cout << S2.Show_total_score() / C2.Show_total_course() << endl;
}
//**********************定义到此终于定义完了,2333
//对所有静态数据成员初始化
double Course::total_tem_course = 0;
double Score::total_tem_score = 0;
int Student::total_student = 0;
int main()
{//学号、姓名、性别、专业、班级、电话号码、课程代码、课程名称、课程学分、课程代码、成绩
Student stu1(181503301, "大辉", "女", "网络工程", 3, "136 **** ****", 3651155, "面向对象程序设计", 1, 3651155, 95);
Course::Rewind_total_course();
Score::Rewind_total_score();
Student stu2(181503302, "小李子", "男", "网络工程", 3, "136 **** ****", 3651155, "面向对象程序设计", 1, 3651155, 90, 15651654, "大学英语", 1, 3651155, 95);
Course::Rewind_total_course();
Score::Rewind_total_score();
Student stu3(181503302, "李伟", "男", "网络工程", 3, "135 **** ****", 3651155, "面向对象程序设计", 1, 3651155, 90, 15651654, "高等数学", 1, 3651155, 59);
char a;
//界面打印相应信息并获取操作对象
while (true)
{
cout << endl << endl << endl;
cout << "(1)打印学生信息" << endl;
cout << "(2)打印学生的课程信息" << endl;
cout << "(3)打印学生的成绩" << endl;
cout << "(4)统计学生所修课程门数" << endl;
cout << "(5)该学生平均成绩" << endl;
cout << "(6)统计总共有多少学生" << endl;
cout << "(0)退出查看" << endl;
cout << "选择相应数字后会执行相应功能" << endl;
a = _getch();
a = a - '0';
if (a == 1)
{
system("ClS");//清除当前页面所有信息
cout << endl << endl << "学生信息" << endl;
stu1.Show_Student();
cout << "-------------------" << endl;
stu2.Show_Student();
cout << "-------------------" << endl;
stu3.Show_Student();
}
if (a == 2)
{
system("ClS");
cout << endl << endl << "学生课程信息" << endl;
stu1.Show_Student_Course();
cout << "------------------" << endl;
stu2.Show_Student_Course();
cout << "------------------" << endl;
stu3.Show_Student_Course();
}
if (a == 3)
{
system("ClS");
cout << endl << endl << "学生成绩" << endl;
stu1.Show_Student_Score();
cout << "------------------" << endl;
stu2.Show_Student_Score();
cout << "------------------" << endl;
stu3.Show_Student_Score();
}
if (a == 4)
{
system("ClS");
stu1.Show_Student();
stu1.Show_total_Course();
cout << "------------------" << endl;
stu2.Show_Student();
stu2.Show_total_Course();
cout << "------------------" << endl;
stu3.Show_Student();
stu3.Show_total_Course() ;
}
if (a == 5)
{
system("ClS");
stu1.Show_Student();
stu1.Show_ave_Score();
cout << "------------" << endl;
stu2.Show_Student();
stu2.Show_ave_Score();
cout << "------------" << endl;
stu3.Show_Student();
stu3.Show_ave_Score();
}
if (a == 6)
{
system("ClS");
cout <<endl<<endl<< "共有" << Student::total_student << "个学生" << endl;
}
if (a == 0)
break;
}
return 0;
}