橙橙橙橙166 2022-05-08 15:56 采纳率: 85.2%
浏览 33
已结题

c++继承与多态问题


class C {
public:
    char getClass();
     int getN();
};
class A :public C {
public:
    A(int n) {
        this->n = n;
    }
    char getClass() {
        return 'A';
    }
    int getN() {
        return n;
    }
private:
    int n;
};
class B :public C {
public:
    B(int n)  {
        this->n = n;
    }
    char getClass() {
        return 'B';
    }
    int getN() {
        return n;
    }
private:
    int n;
};
C *a[100];
void PrintInfo(C* a) {
    cout << a->getClass() << " " << a->getN();
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i) {
            char c;
            int k;
            cin >> c >> k;
            if (c == 'A')
                a[i] = new A(k);
            else
                a[i] = new B(k);
        }
        cout << n << endl;
        for (int i = 0; i < n; ++i)
            PrintInfo(a[i]);
        cout << "****" << endl;
    }
}

img


A,B继承C类,main函数不能动,这个PrintInfo函数有什么问题吗,要怎么解决

  • 写回答

1条回答 默认 最新

  • 无夜_ 2022-05-08 16:32
    关注

    没实现c类函数,继承间发生多太行为需要virtual关键字,纯虚函数应该= 0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月16日
  • 已采纳回答 5月8日
  • 创建了问题 5月8日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”