鱼C论坛

 找回密码
 立即注册
查看: 2415|回复: 1

[已解决]c++函数综合考察

[复制链接]
发表于 2018-5-1 19:59:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
C++综合实验题,定义四个类,并编写主函数进行测试,具体要求如下:
(1)定义一个抽象基类:生物类Organism,仅包括一个纯虚函数Display();
(2)定义一个Organism类的派生类:人员类Person,包括2个保护数据成员:name(姓名)、age (年龄)。四个函数成员:一个Display()函数,实现动态多态性;一个运算符“<<”的重载函数,实现Person类对象信息的直接输出,输出格式要求为(name,age);两个构造函数,一个默认构造函数,一个带参数的构造函数;
(3)定义一个Person类的派生类:学生类Student,包括一个保护数据成员 num(学号)。包括四个函数成员:一个Display()函数,实现动态多态性;一个运算符“<<”的重载函数,实现Student类对象信息的直接输出,输出格式要求为(name,age,num);两个构造函数,一个默认构造函数,一个带参数的构造函数;
(4)定义一个Student类的派生类:研究生类Graduate,包括一个私有数据成员 spe(专业)。包括四个函数成员:一个Display()函数,实现动态多态性;一个运算符“<<”的重载函数,实现Graduate类对象信息的直接输出,输出格式要求为(name,age,num,spe);两个构造函数,一个默认构造函数,一个带参数的构造函数。
最佳答案
2018-5-2 13:20:56
你也没说Display要怎么多态! 我就给你空着了。自个写吧。


  1. #include <iostream>

  2. using namespace std;

  3. class Organism {
  4. public:
  5.         virtual void Display() = 0;
  6. };

  7. class Person : public Organism {
  8. public:
  9.         Person() :name(), age(1) {}
  10.         Person(string nName, unsigned int nAge) :name(nName), age(nAge) {}

  11.         void Display() { /* code */ }
  12.         friend ostream& operator<<(ostream& o, const Person & per){
  13.                 // 奶奶个腿 VS太毒了!居然找不到string的转换!奈何我只有转成C风格字符串!
  14.                 o << '(' << (per.name).c_str() << ',' << per.age << ')';

  15.                 return o;
  16.         }

  17. protected:
  18.         string name;
  19.         unsigned int age;
  20. };

  21. class Student : public Person {
  22. public:
  23.         Student() :Person(), num(0) {}
  24.         Student(string nName, unsigned int nAge, unsigned long nNum)
  25.                 :Person(nName, nAge), num(nNum) {}

  26.         void Display() { /* code */ }
  27.         friend ostream& operator<<(ostream& o, const Student& stu){
  28.                 o << '(' << (stu.name).c_str() << ',' << stu.age << ',' << stu.num << ')';

  29.                 return o;
  30.         }

  31. protected:
  32.         unsigned long num;
  33. };

  34. class Graduate : public Student {
  35. public:
  36.         Graduate() :Student(), spe() {}
  37.         Graduate(string nName, unsigned int nAge, unsigned long nNum, string nSpe)
  38.                 :Student(nName, nAge, nNum), spe(nSpe) {}

  39.         void Display() { /* code */ }
  40.         friend ostream& operator<<(ostream& o, const Graduate& gra){
  41.                 o << '(' << (gra.name).c_str() << ',' << gra.age << ',' << gra.num << ',' << (gra.spe).c_str() << ')';

  42.                 return o;
  43.         }

  44. private:
  45.         string spe;
  46. };


  47. int main()
  48. {
  49.         Graduate a("123",5,6,"研究生");
  50.         cout << a << endl;

  51.         system("pause");

  52.         return 0;
  53. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-5-2 13:20:56 | 显示全部楼层    本楼为最佳答案   
你也没说Display要怎么多态! 我就给你空着了。自个写吧。


  1. #include <iostream>

  2. using namespace std;

  3. class Organism {
  4. public:
  5.         virtual void Display() = 0;
  6. };

  7. class Person : public Organism {
  8. public:
  9.         Person() :name(), age(1) {}
  10.         Person(string nName, unsigned int nAge) :name(nName), age(nAge) {}

  11.         void Display() { /* code */ }
  12.         friend ostream& operator<<(ostream& o, const Person & per){
  13.                 // 奶奶个腿 VS太毒了!居然找不到string的转换!奈何我只有转成C风格字符串!
  14.                 o << '(' << (per.name).c_str() << ',' << per.age << ')';

  15.                 return o;
  16.         }

  17. protected:
  18.         string name;
  19.         unsigned int age;
  20. };

  21. class Student : public Person {
  22. public:
  23.         Student() :Person(), num(0) {}
  24.         Student(string nName, unsigned int nAge, unsigned long nNum)
  25.                 :Person(nName, nAge), num(nNum) {}

  26.         void Display() { /* code */ }
  27.         friend ostream& operator<<(ostream& o, const Student& stu){
  28.                 o << '(' << (stu.name).c_str() << ',' << stu.age << ',' << stu.num << ')';

  29.                 return o;
  30.         }

  31. protected:
  32.         unsigned long num;
  33. };

  34. class Graduate : public Student {
  35. public:
  36.         Graduate() :Student(), spe() {}
  37.         Graduate(string nName, unsigned int nAge, unsigned long nNum, string nSpe)
  38.                 :Student(nName, nAge, nNum), spe(nSpe) {}

  39.         void Display() { /* code */ }
  40.         friend ostream& operator<<(ostream& o, const Graduate& gra){
  41.                 o << '(' << (gra.name).c_str() << ',' << gra.age << ',' << gra.num << ',' << (gra.spe).c_str() << ')';

  42.                 return o;
  43.         }

  44. private:
  45.         string spe;
  46. };


  47. int main()
  48. {
  49.         Graduate a("123",5,6,"研究生");
  50.         cout << a << endl;

  51.         system("pause");

  52.         return 0;
  53. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 03:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表