《C++面向对象程序设计》.docx
《《C++面向对象程序设计》.docx》由会员分享,可在线阅读,更多相关《《C++面向对象程序设计》.docx(39页珍藏版)》请在冰点文库上搜索。
《C++面向对象程序设计》
《C++面向对象程序设计》
课程设计报告
(2011—2012学年第2学期)
题目:
学校人员信息管理系统
专业:
计算机科学与技术
班级:
11计算机科学与技术
姓名学号:
XX
1XX
指导教师:
XX
成绩:
计算机科学与技术系
2012
年
6
月
21
日
一、设计目的
课程设计是学习C++程序设计的一个重要环节。
通过课程设计的综合训练,可以使学生在学习理论知识并在实验中进行了一系列基本训练的基础上,进一步加强学生对实际问题分析、编程和动手能力的提高,强化综合应用能力,扩充知识,开阔视野。
学生在熟练运用VisualC++6.0以后,通过使用VisualC++6.0的训练,扩展程序设计的知识面,提高综合应用C++知识能力的要求。
二、设计内容
设计一个学校人员信息管理系统,可以实现对学校人事的信息管理。
基本功能要求如下:
(1)建立人员信息数据。
包括学号(教工号)、姓名、性别、专业(工作系别部门)、出生时间、年龄。
其中,
1)对学生,还包括3门课的成绩(Chinese,English,Mathematics)。
三门课的成绩为int型数据。
2)对教师,还包括职称(如教授/副教授/讲师),工资(如1432.50)。
工资为double型数据。
(2)使用继承的方法构造3个类,对相应的对象放置10个学生(教师)信息。
ØPeople类——虚基类
ØStudent类——派生类
ØTeacher类——派生类
(3)数据基本功能:
Ø增加数据:
AddData()函数。
Ø删除数据:
DeleteData()函数。
根据学号(教工号),删除该人员的记录。
Ø修改数据:
UpdateData()函数。
对学生,根据学号,能够修改3门课的成绩;对教师,根据教工号,能够修改“职称”和“工资”。
Ø查找功能:
1)SearchData()函数。
要求能够根据“学号(教工号)”、“姓名”、“性别”三项实现查询,并能在屏幕上显示满足条件的数据记录。
2)FindMaxData()函数。
对学生,查找“English”课程成绩最高的学生记录并打印;对教师,查找“工资”最高的教师记录并打印。
要求:
采用函数模板实现。
Ø显示功能:
DisplayData()函数。
在屏幕上打印所有的数据记录;打印时,先打印学生的记录,后打印教师的记录。
并且按学生(教工号)升序打印。
(4)重载功能:
Ø要求对“<<”和“>>”运算符进行重载。
考虑到输人学生(教工)姓名时,名字中可能引入空格,所以重载“>>”运算符时,需要满足这个要求。
(5)异常处理功能:
Ø设置异常处理,对教师工资为负数的情况给出提示。
(6)文件的输入输出功能:
Ø能够从文件中读入数据。
外部文件为Student_ext.dat,Teacher_ext.dat。
Ø能够将数据保存到相应目录的文件中。
保存数据时,分两个文件分别保存Student.dat,Teacher.dat。
(7)参考系统界面如示:
Ø1-增加一位人员(教师、学生)记录
Ø2-删除一个人员记录
Ø3-修改一个人员记录
Ø4-根据“学号(教工号)”、“姓名”、“性别”查找记录
Ø5-根据人员类别(学生、教师)查找“English”课程成绩最高的学生,或最高工资的教师
Ø6-显示全部人员记录
Ø7-从外部文件追加数据
Ø8-将所有数据保存到磁盘文件
Ø9-退出系统。
三、总体设计
1、系统类的层次关系
2、类的主要框架描述
classPeople{
protected:
stringname,sex,birthday;
intnumber,age;
};
classStudent:
virtualpublicPeople{
public:
voidset();//初始化信息,第一次对信息的录入
voidAddData();//添加学生信息
voidDeleteData();//删除某学生信息
voidUpdateData();//修改某学生信息
voidSearchData();//查询某学生信息
voidDisplayData();//显示全部学生信息//在类中定义
private:
staticintnum;
stringmajor;
intchinese,english,math;
};
classTeacher:
virtualpublicPeople{
public:
voidset();//初始化信息,第一次对信息的录入
voidAddData();//添加老师信息
voidDeleteData();//删除某老师信息
voidUpdateData();//修改某老师信息
voidSearchData();//查询某老师信息
voidDisplayData();//显示全部老师信息//在类中定义
private:
staticintnum1;
stringdepartmemt,title;
doublesalary;
};
voidmain()
{inti;
Studentstu;
Teachertea;
charch;
voidmenu();
menu();
cout<<"请输入选择:
";
cin>>ch;
while(ch!
='0')
{system("cls");
switch(ch){}
menu();
cin>>ch;
}
}
四、系统测试用例
1、系统菜单
2、选择(学生或老师)输入信息
3、选择添加(学生或老师)信息
4、选择查找方式
5、显示(老师或学生)所有信息
五、错误记录及原因分析
1、cannotopenDebug/x.exeforwriting:
提示不能打开x.exe来执行程序由于系统已经在
运行了,所以当你在重新组建,调试时,就会出现这条提示语句。
2、x.cpp(115):
errorC2065:
'setw':
undeclaredidentifier:
这条语句提示没有定义
setw的头文件#include,没有声明其头文件,就不能用,setw必须带参数。
六、源程序
#include
#include
#include
#include
usingnamespacestd;
#defineM100
intL=0;//老师总量
intN=0;//学生总量
classPeople{
protected:
stringname,sex,birthday;
intnumber,age;
};
classStudent:
virtualpublicPeople{
public:
voidset();//初始化信息,第一次对信息的录入
voidAddData();//添加学生信息
voidDeleteData();//删除某学生信息
voidUpdateData();//修改某学生信息
voidSearchData();//查询某学生信息
voidDisplayData();//显示全部学生信息//在类中定义
private:
staticintnum;
stringmajor;
intchinese,english,math;
};
intStudent:
:
num=0;///初始化文件
voidStudent:
:
set()
{
inti;
stringsetna,setse,setbirth,setmaj;
intsetnu,setag,setchi,seteng,setma;
ofstreamoutfile;
outfile.open("Student_ext.dat.",ios:
:
out);//----打开文件----
if(!
outfile)
{
cout<<"cannotopen!
\n"<exit
(1);
}
//----依次录入相应个数的学生信息----
for(i=0;i{cout<<"姓名输入为'0',停止输入"<cout<<"输入第"<
"<cout<<"姓名:
";
cin>>setna;
name=setna;
if(setna=="0")break;
if(setna!
="0")
{
N++;
num++;
outfile<cout<<"学号:
";cin>>setnu;number=setnu;outfile<outfile<cout<<"性别:
";cin>>setse;sex=setse;outfile<cout<<"年龄:
";cin>>setag;age=setag;outfile<cout<<"出身时间:
";cin>>setbirth;birthday=setbirth;outfile<cout<<"专业:
";cin>>setmaj;major=setmaj;outfile<cout<<"英语成绩:
";cin>>seteng;english=seteng;outfile<cout<<"数学成绩:
";cin>>setma;math=setma;outfile<cout<<"语文成绩:
";cin>>setchi;chinese=setchi;outfile<}
}
outfile.close();//----关闭文件----
}
///添加文件
voidStudent:
:
AddData()
{
stringaname,asex,abirthday,amajor;
intanumber,aage,achinese,aenglish,amath;
//----以追加的方式录入信息,直接将信息追加到以前文件的末尾----
ofstreamoutfile("Student_ext.dat",ios:
:
app);
if(!
outfile)
{
cout<<"cannotopen!
\n"<exit
(1);
}
N=N+1;
//----依次输入相应的数据,并且追加到之前的文件中去----
cout<<"请输入您要添加的数据"<num++;
outfile<cout<<"学号:
";cin>>anumber;outfile<cout<<"姓名:
";cin>>aname;outfile<cout<<"性别:
";cin>>asex;outfile<cout<<"年龄:
";cin>>aage;outfile<cout<<"出身时间:
";cin>>abirthday;outfile<cout<<"专业:
";cin>>amajor;outfile<cout<<"数学成绩:
";cin>>amath;outfile<cout<<"英语成绩:
";cin>>aenglish;outfile<cout<<"语文成绩:
";cin>>achinese;outfile<outfile.close();
cout<<"已添加成功!
"<}
///删除文件
voidStudent:
:
DeleteData()
{
intk,i;
intnum[M];
stringname[M],sex[M],major[M],birthday[M];
intnumber[M],age[M],math[M],english[M],chinese[M];
ifstreaminfile("Student_ext.dat",ios:
:
in);
if(!
infile)
{
cout<<"cannotopen!
\n"<exit
(1);
}
for(i=0;i{
infile>>num[i];
infile>>number[i];cout<infile>>name[i];cout<infile>>sex[i];cout<infile>>age[i];cout<infile>>birthday[i];cout<infile>>major[i];cout<infile>>math[i];cout<infile>>english[i];cout<infile>>chinese[i];cout<}
cout<<"删除第几个?
"<cin>>k;k=k-1;
infile.close();
ofstreamoutfile("Student_ext.dat",ios:
:
out);
if(!
outfile)
{
cout<<"cannotopen!
\n"<exit
(1);//退出程序
}
//----将数组存取的信息输出到文件中去----
for(i=0;i{
if(i!
=k)
{
outfile<outfile<outfile<outfile<outfile<outfile<outfile<