橙橙橙橙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日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan