鱼C论坛

 找回密码
 立即注册
查看: 2146|回复: 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要怎么多态! 我就给你空着了。自个写吧。

#include <iostream>

using namespace std;

class Organism {
public:
        virtual void Display() = 0;
};

class Person : public Organism {
public:
        Person() :name(), age(1) {}
        Person(string nName, unsigned int nAge) :name(nName), age(nAge) {}

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

                return o;
        }

protected:
        string name;
        unsigned int age;
};

class Student : public Person {
public:
        Student() :Person(), num(0) {}
        Student(string nName, unsigned int nAge, unsigned long nNum)
                :Person(nName, nAge), num(nNum) {}

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

                return o;
        }

protected:
        unsigned long num;
};

class Graduate : public Student {
public:
        Graduate() :Student(), spe() {}
        Graduate(string nName, unsigned int nAge, unsigned long nNum, string nSpe)
                :Student(nName, nAge, nNum), spe(nSpe) {}

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

                return o;
        }

private:
        string spe;
};


int main()
{
        Graduate a("123",5,6,"研究生");
        cout << a << endl;

        system("pause");

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

#include <iostream>

using namespace std;

class Organism {
public:
        virtual void Display() = 0;
};

class Person : public Organism {
public:
        Person() :name(), age(1) {}
        Person(string nName, unsigned int nAge) :name(nName), age(nAge) {}

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

                return o;
        }

protected:
        string name;
        unsigned int age;
};

class Student : public Person {
public:
        Student() :Person(), num(0) {}
        Student(string nName, unsigned int nAge, unsigned long nNum)
                :Person(nName, nAge), num(nNum) {}

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

                return o;
        }

protected:
        unsigned long num;
};

class Graduate : public Student {
public:
        Graduate() :Student(), spe() {}
        Graduate(string nName, unsigned int nAge, unsigned long nNum, string nSpe)
                :Student(nName, nAge, nNum), spe(nSpe) {}

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

                return o;
        }

private:
        string spe;
};


int main()
{
        Graduate a("123",5,6,"研究生");
        cout << a << endl;

        system("pause");

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 21:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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