CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

继承??一个关于继承的问题

楼主FIFA1982()2004-12-04 16:47:37 在 C/C++ / C++ 语言 提问

你好,能不能按下面的要求帮我写一个,让我参考一下呢/  
   
  构造类层次:  
      结构:在册人员(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

相关问题

  • 继承
  • 继承
  • 继承??
  • 继承!!!
  • 继承控件
  • 关于继承
  • 继承问题
  • 代码继承!
  • 继承问题
  • javascript的继承

关键词

  • 文件
  • 学生
  • null
  • pname
  • student
  • 继承
  • nmark
  • person
  • nnumber
  • number

得分解答快速导航

  • 帖主:FIFA1982

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo