首先 创建基类 :明确基类属性:教师 学生只有姓名共同
/* SchoolBase.h*/
#pragma once
#include<iostream>
using namespace std;
class SchoolBace
{
public:
SchoolBace(string name);
virtual ~SchoolBace(){}
virtual void display() = 0;
protected:
string name;//姓名
};
/* SchoolBase.cpp*/
#include "SchoolBace.h"
SchoolBace::SchoolBace(string name)
{
this->name = name;
}
第二部,设计学生类,只涉及了中学,其他按照这个修改即可
/*student.h*/
#pragma once
#include"SchoolBace.h"
class mStudent :public SchoolBace
{
public:
void display();
mStudent(int n, string name);
~mStudent();
protected:
int Id = 0;
int n = 0;
double sumScore = 0;//总分
double* courseScore;//单科得分
string* courseName;//科目名
string className; //班级
static int stuNum;
};
/*student.cpp*/
#include "Student.h"
int mStudent::stuNum = 1000;
void mStudent::display()
{
cout << "学生姓名" << this->name << endl;
cout << "学生编号为" << this->Id << endl;
for (int i = 0; i < this->n; i++) {
cout << "学生" << name << "的第" << i + 1 << "门课程名为:"<<this->courseName[i]<<"对应分数为 "<<this->courseScore[i] << endl;
}
cout << "学生" << name << "所在班级为" <<this->className<< endl;
cout << "学生" << name << "总成绩为" << this->sumScore << endl;
}
mStudent::mStudent(int n, string name) :SchoolBace(name)
{
this->courseName = new string[n];
this->courseScore = new double[n];
this->n = n;
for (int i = 0; i < n; i++) {
cout << "请输入" << name << "的第"<<i+1<<"门课程名及对应分数 如张xx 20.0: " ;
cin >> this->courseName[i] >> this->courseScore[i];
this->sumScore += courseScore[i];
}
cout << "请输入" << name << "所在班级 如xxx122: " ;
cin >> this->className;
this->Id = ++stuNum;
}
mStudent::~mStudent()
{
cout << "系统退出" << endl;
delete[] this->courseName;
delete[] this->courseScore;
}
最后 主函数方法
#include"Student.h"
int main()
{
SchoolBace* Stu = new mStudent(2, "张三");
/*这里n表示科目,第二个为姓名 可以自定义*/
Stu->display();
delete Stu;
getchar();
return 0;
}
有问题错误请留言或私信