题目内容:编写一个输出学生和教师数据的程序,学生数据有编号、姓名、年龄、班号和成绩;教室数据有编号、姓名。年龄、职称和部门。要求声明一个person类,并作为学生数据操作类student和教师数据操作类teacher的基类。
题目分析:
学生数据有编号、姓名、年龄、班号和成绩;教室数据有编号、姓名。年龄、职称和部门,共有的对象是编号、姓名和年龄。所以将编号、姓名和年龄设置为person基类。
储存方式:将编号、年龄、班号用int型存储(实际上此处班号使用char型数组存储更为合适);
将成绩使用char型存储;
将姓名,职称,部门使用char型数组存储。
于是可以画出如下UML图:
这里使用的是dev C++进行编程,报错信息代码段中Instruction部分。代码段中person类内定义了disp(),在teacher类和student类中再次定义了disp()。teacher这个派生类内的disp()会把基类person的disp()屏蔽掉,在teacher类的disp()的函数定义中再通过person::disp()调用person类的函数。这样做的目的在于减少函数名数量,便于记忆和使用。
编程时犯了一些小问题,比如没写“using namepace std;”,报错时不知道错在哪里很是麻烦,所以也把他贴在此处。我自己在编程时犯的错误的错因及改正同代码段中Instruction。
//EXERCISE_4 derived.cpp
/*=====================Instruction==============================
error in this coding:
1: all class definition should follow with ";" or get a message
[Error] "expected';' after class definition.".
2: without operator overloading,you couldn't assign a literal constant to a character array but you
could use #include <string.h> and strcpy function to rewrite it instead.
[Error] incompatible types in assignment of 'const char [5]' to 'char [10]'
3: using #include<iostream> and " using namespace std;" before you use io class.
[Error] 'cout' was not declared in this scope
4: "char* p imply that the parametric could be change,but a literal constant like "abc" could'nt be change. You should turn "char*p" to "const char*p"."
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
*///============================================================
//==============================================================
#include <iostream>
#include <string.h>
using namespace std;
//==============================================================
class person
{
public:
3493




被折叠的 条评论
为什么被折叠?



