dragonir 2015-06-02 10:49 采纳率: 100%
浏览 1674
已采纳

C++一道关于继承的题目,求大神解答,感激不尽

  1. Dynamic_cast

Total: 65 Accepted: 22

Time Limit: 1sec Memory Limit:256MB
Description

Three classes A, B and C are shown below:

class A {
public:
virtual ~A() {};
};
class B: public A {};
class C: public B {};

You are to implement a function string verify(A *), such that it returns "grandpa" if the passed-in argument points to a class A object, and "father" for a class B object , "son" for a class C object.

Your submitted source code should include the whole implementation of the function verify, but without any class defined above.
No main() function should be included.

  • 写回答

6条回答 默认 最新

  • threenewbee 2015-06-02 11:24
    关注
     #include <iostream>
    #include <string>
    using namespace std;
    
    class A {
    public:
    virtual ~A() {};
    public: virtual string gettype()
    {
        return "grandpa";
    }
    public: static string verify(A * a)
    {
        return a->gettype();
    }
    };
    
    class B: public A 
    {
    public: virtual string gettype()
    {
        return "father";
    }
    };
    
    class C: public B
    {
    public: virtual string gettype()
    {
        return "son";
    }
    };
    
    int main(int argc, char* argv[])
    {
        A a;
        B b;
        C c;
        cout << A::verify(&a) << endl;
        cout << A::verify(&b) << endl;
        cout << A::verify(&c) << endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失