继承??一个关于继承的问题
你好,能不能按下面的要求帮我写一个,让我参考一下呢/
构造类层次:
结构:在册人员(Person)、学生(Student)、本科生(BStudent)、专科生( ZStudent)、职员(Employee)、教师(Teacher)、职工(Worker)。
要求:学生类必须记录3门功课以及对应成绩,教师类必须记录其任课信息。
(以上记录的信息都必须保存到文件中,能对该文件进行读/写)
*主要是体现继承的优越性*
问题点数:0、回复次数:4Top
1 楼yevv(荷柴樵夫)回复于 2004-12-04 16:59:29 得分 0
class person{
string name;
int age;
bool sex;
……
};
class student:public person{
class course_grade{
string course_name;
int coutse_grade;
}first,second,third;
……
};
class bstudent:public student{
……
};
class zstudent:public student{
……
};
class employee:public person{
……
};
class teacher:public employee{
……
};
class worker:public employee{
……
};Top
2 楼goodluckyxl(被人遗忘的狗)回复于 2004-12-04 16:59:54 得分 0
//写了学生的成绩 自己再类似的加 2门
//文件操作只要转化为char然后 write进去就行了
//看看吧
#include <iostream.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
class Person
{
public:
Person(int i_Number, char* i_pName):m_nNumber(i_Number){ i_pName == NULL? memset(m_pName,'\0',MAX_NAME_LENGTH):strlen(i_pName)>MAX_NAME_LENGTH ? memcpy(m_pName, i_pName, MAX_NAME_LENGTH) : strcpy(m_pName, i_pName);}
const char* GetName() const {return m_pName;}
int GetNumber() const {return m_nNumber;}
void SetName( const char* i_pName );
void SetNumber( int i_Number ) {m_nNumber = i_Number;};
protected:
int m_nNumber;
char m_pName[MAX_NAME_LENGTH];
};
void Person::SetName( const char* i_pName )
{
if (i_pName == NULL)
return;
strlen(i_pName)>MAX_NAME_LENGTH ? memcpy(m_pName, i_pName, MAX_NAME_LENGTH) : strcpy(m_pName, i_pName);
}
class student:public Person
{
public:
student(int i_mark, int i_Number, char* i_pName ):m_nMark(i_mark), Person(i_Number, i_pName){}
student():m_nMark(0),Person(0,NULL){};
int GetMark() const { return m_nMark; }
void SetMark( int i_mark ) { m_nMark = i_mark; }
friend ostream& operator<< ( ostream& os, student& i_student );
friend istream& operator >> ( istream& is, student& i_student );
private:
int m_nMark;
};
ostream& operator<< ( ostream& os, student& i_student )
{
os<<"The Student Infomation:"<<endl;
os<<"Name:"<<i_student.GetName()<<endl;
os<<"Number:"<<i_student.GetNumber()<<endl;
os<<"Marks:"<<i_student.GetMark()<<endl;
return os;
}
istream& operator >> ( istream& is, student& i_student )
{
char lpc_Name[MAX_NAME_LENGTH] = {0};
int li_Number = 0, li_mark = 0;
cout<<"Input Student's Information"<<endl;
cout<<"Name:"<<endl;
is >> lpc_Name;
i_student.SetName(lpc_Name);
cout<<"Number:"<<endl;
is >> li_Number;
i_student.SetNumber(li_Number);
cout <<"Marks"<<endl;
is >> li_mark;
i_student.SetMark( li_mark );
return is;
}
void main()
{
student Example;
cout << Example;
cin >> Example;
cout <<Example;
}Top
3 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-04 17:04:52 得分 0
#include <iostream>
using namespace std;
#include <string>
/////////////////////////////////////////////////////////////////
class Person
{
public:
Person(int number, string name) : P_number(number), P_name(name)
{
}
virtual void print() = 0;
string name()
{
return P_name;
}
int number()
{
return P_number;
}
protected:
string P_name;
int P_number;
};
//////////////////////////////////////////////////////////////////
class Student : public Person
{
public:
Student(int number, string name, int score) : Person(number, name), S_score(score)
{
}
virtual void print()
{
cout << P_number << " ";
cout << P_name << " ";
cout << S_score << endl;
}
int score()
{
return S_score;
}
private:
int S_score;
};
////////////////////////////////////////////////////////////////////
class Teacher : public Person
{
public:
Teacher(int number, string name, char title) : Person(number, name), T_title(title)
{
}
virtual void print()
{
cout << P_number<< " ";
cout << P_name << " ";
cout << T_title << endl;
}
char title()
{
return T_title;
}
private:
char T_title;
};
///////////////////////////////////////////////////////////////////////
int main()
{
Student student(1, string("student1"), 90);
Teacher teacher(1, string("teacher1"), 'p');
student.print();
teacher.print();
return 0;
}Top
4 楼Flood1984(峰子)回复于 2004-12-04 17:36:56 得分 0
狗都升星了,还是这么认真,
难得!Top




