C++一道关于继承的题目,求大神解答,感激不尽-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

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.

展开
收起
a123456678 2016-03-06 12:59:34 1650 0
1 条回答
写回答
取消 提交回答
  • a123456678
     #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;
    }
    2019-07-17 18:54:23
    赞同 展开评论 打赏
问答分类:
C++
问答标签:
相关产品:
问答排行榜
最热
最新
相关电子书
更多
继承与功能组合
立即下载
移动与复制
立即下载
C++课程-对象模型
立即下载