定义一个CPerson类及其派生类学生类和教师类

2025-11-24 22:48:09

1、1.定义一个cperson类,实现学工号,姓名,性别,生日等基本信息的录入。

    2.采用公有继承方式定义cperson类的子类ctudent,添加班级,学号,课程,分数等学生特有的信息。

    3.采用共有继承方式定义cperson类的子类cteacher,添加职称,工号,课程,学生成绩等教师特有信息。

    4.对相关函数进行定义实现各种信息的键入和输出。

    5.主函数定义实现上述功能的实现。

 (2)详细设计(要求详细写出每个模块的功能实现,关键技术,关键代码解释等。)

定义一个CPerson类及其派生类学生类和教师类

2、1.父类与子类声明模块

主要完成功能为:声明父类和子类的所包含的各种信息以及输入输出函数声明,构建出该工程功能实现的基本框架。

使用技术:派生与继承技术

关键代码如下:

class Cstudent : public Cperson

{

private:

      char * classname;

      char * xuehao;

      string keming[20];

      double score[20];

public:

      Cstudent();

      ~Cstudent();

      void input();

      void inputCourse();

      void displays();

};

class Teacher : public Cperson

{

private:

      string zhicheng;

      string gonghao;

      string bumen;

      string keming;

      string kechenghao;

      double chengji[300];

public:

      Teacher();

      ~Teacher();

      void input();//录入教师相关信息

      void setscore(int n);//录入学生相关信息

      void dispscore(int m, int n);//用于指定或所有学生成绩信息

       void sortcore(int n);//对学生成绩进

3、.函数实现模块

    主要完成功能:实现函数声明要求的功能,实现各种信息的键入和输出。

    使用技术:数组储存数据,拷贝函数,文件储存。

    关键代码:

Cperson::Cperson()

{

      id = new char[10];

      strcpy_s(id, 9, "00000000");

      Tsex  sex;

      birthday = "20000101";

}

Cperson::~Cperson()

{

      delete[] id;

      delete[] name;

}

void Cperson::input()

{

      id = new char[10], name = new char[20];

      cout << "输入: id   name   sex(输入0为男,1为女)\n";

      cin >> id >> name >> (int&)sex;

      cout << id << "  " << name << "  ";

      if (sex == man){ cout << "男" << "\n"; }

      else cout << "女" << "\n";

      cout << "10位学生成绩为:\n";

}

4、#include "stdafx.h"

#include<iostream>

#include<string>

#include<algorithm>

using namespace std;

#ifndef CPERSON_H

#define CPESON_H

#endif

#include<fstream>

enum Tsex{ man, woman };

class Cperson

{

private:

char* id;

char* name;

Tsex sex;

string birthday;

public:

Cperson();

~Cperson();

void input();//录入人员信息

void displayP();//显示人员信息

};

class Cstudent : public Cperson

{

private:

char * classname;

char * xuehao;

string keming[20];

double score[20];

定义一个CPerson类及其派生类学生类和教师类

5、public:

Cstudent();

~Cstudent();

void input();

void inputCourse();

void displays();

};

class Teacher : public Cperson

{

private:

string zhicheng;

string gonghao;

string bumen;

string keming;

string kechenghao;

double chengji[300];

public:

Teacher();

~Teacher();

void input();//录入教师相关信息

void setscore(int n);//录入学生相关信息

void dispscore(int m, int n);//用于指定或所有学生成绩信息

void sortcore(int n);//对学生成绩进行排序

void setstuscore(double &s1, int n);//用于修改指定学生

void display(int n);//用于显示教师信息

};

Cperson::Cperson()

{

id = new char[10];

定义一个CPerson类及其派生类学生类和教师类

6、strcpy_s(id, 9, "00000000");

Tsex  sex;

birthday = "20000101";

}

Cperson::~Cperson()

{

delete[] id;

delete[] name;

}

void Cperson::input()

{

id = new char[10], name = new char[20];

cout << "输入: id   name   sex(输入0为男,1为女)\n";

cin >> id >> name >> (int&)sex;

cout << id << "  " << name << "  ";

if (sex == man){ cout << "男" << "\n"; }

else cout << "女" << "\n";

cout << "10位学生成绩为:\n";

}

void Cperson::displayP()

{

}

Cstudent::Cstudent()

{

classname = new char[20], xuehao = new char[20];

strcpy_s(xuehao, 9, "00000000");

int i;

for (i = 0; i < 20; i++)

定义一个CPerson类及其派生类学生类和教师类

7、Cstudent::~Cstudent()

{

delete[] classname;

delete[] xuehao;

}

void Cstudent::input()

{

classname = new char[20], xuehao = new char[20];

cin >> classname >> xuehao;

}

void Cstudent::inputCourse()

{

for (int i = 0; i < 8; i++)

{

cin >> keming[i] >> score[i];

}

}

void Cstudent::displays()

{

cout << " " << classname << " " << xuehao << "\n";//输出到屏幕

Cperson::displayP();

ofstream fout;

fout.open("学生信息库.txt");

if (!fout)

{

fout << " " << classname << " " << xuehao << endl;

return;

}

定义一个CPerson类及其派生类学生类和教师类

8、 fout.close();

}

Teacher::Teacher()

{

gonghao = "00000000";

kechenghao = "00000000";

int i;

for (i = 0; i < 300; i++)

chengji[i] = -1;

}

void Teacher::input()

{

cout << "分别输入教师: 职称  工号  部门   课名   课程号\n";

cin >> zhicheng >> gonghao >> bumen >> keming >> kechenghao;

cout << "教师信息:" << zhicheng << " " << gonghao << " " << bumen << " " << keming << " " << kechenghao << "\n";

Cperson::input();

ofstream fout;

fout.open("教师信息库.txt");

if (fout)

{

fout << zhicheng << " " << gonghao << " " << bumen << " " << keming << " " << kechenghao << "\n";

return;

}

fout.close();

}

void Teacher::setscore(int n)

{

int i;

for (i = 0; i < n; i++)

cin >> chengji[i];

}

void Teacher::dispscore(int m, int n)

{

if (m == 0)

cout << chengji[n];

else

{

定义一个CPerson类及其派生类学生类和教师类

9、else

{

int i;

for (i = 0; i < n; i++)

cout << chengji[i] << " ";

cout << endl;

}

}

void Teacher::sortcore(int n)

{

cout << "10位学生成绩为:\n";

sort(chengji, chengji + n);

}

void Teacher::setstuscore(double &s1, int n)

{

chengji[n] = s1;

}

void Teacher::display(int n)

{

Cperson::displayP();

int i;

cout << "每个老师" << n << "名学生成绩\n";

for (i = 0; i < n; i++)

{

cout << chengji[i] << " ";

cout << endl;

}

}

Teacher::~Teacher()

{

}

int _tmain(int argc, _TCHAR* argv[])

{

Cstudent *ti1 = new Cstudent[10];

int i;

for (i = 0; i < 10; i++)

{

cout << "\n班级名" << " 和 " << "学号\n";

ti1[i].input();

cout << "分别输入每位学生对应的8个课名和课程成绩  例如a90"  <<endl;

ti1[i].inputCourse();

}

for (i = 0; i < 8; i++)

ti1[i].displays();

Teacher * ti2 = new Teacher[4];

for (i = 0; i < 4; i++)

{

ti2[i].input();

ti2[i].setscore(10);

}

for (i = 0; i <4; i++)

ti2[i].display(10);

return 0;

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢