为了账号安全,请及时绑定邮箱和手机立即绑定

有什么问题?就是运行不了

有什么问题?就是运行不了

C++
慕田峪3750655 2016-09-27 14:45:47
#include <iostream> #include <cstdlib> #include <ctime> #include <typeinfo> using namespace std; class Grand { private: int hold; public: Grand(int h = 0) :hold(h){} virtual void Speak() const{ cout << "I am a grand class!\n"; } virtual int Value() const{ return hold; } }; class Superb :public Grand { public: Superb(int h = 0) :Grand(h){} void Speak() const{ cout << "I am a superb class!!\n"; } virtual void Say() const { cout << "I hold the superb value of " << Value() << "!\n"; } }; class Magnificent :public Superb { private: char ch; public: Magnificent(int h = 0, char cv = 'A') :Superb(h), ch(cv){} void Speak() const{ cout << "I am a magnificent class!!!\n"; } void Say() const{ cout << "I hold the character " << ch << " and the integer " << Value() << "!\n"; } }; Grand * GetOne(); int main() { srand(time(0)); Grand * pg; Superb * ps; for (int i = 0; i < 5; i++) { pg = GetOne(); cout << "Now processing type " << typeid(*pg).name() << ".\n"; pg->Speak(); if (ps = dynamic_cast<Superb *>(pg)) ps->Say(); if (typeid(Magnificent) == typeid(*pg)) cout << "Yes, you're really magnificent.\n"; } return 0; } Grand * GetOne() { Grand * p; switch (rand() % 3) { case 0: p = new Grand(rand() % 100); break; case 1: p = new Superb(rand() % 100); break; case 2: p = new Magnificent(rand() % 100, 'A' + rand() % 26); break; } return p; }
查看完整描述

1 回答

?
Crafon

TA贡献63条经验 获得超30个赞

可以也运行啊。。。

查看完整回答
反对 回复 2016-09-28
  • 慕田峪3750655
    慕田峪3750655
    warning C4244: “参数”: 从“time_t”转换到“unsigned int”,可能丢失数据 error C4703: 使用了可能未初始化的本地指针变量“p”
  • Crafon
    Crafon
    dev cpp运行了 Now processing type 5Grand. I am a grand class! Now processing type 11Magnificent. I am a magnificent class!!! I hold the character O and the integer 4! Yes, you're really magnificent. Now processing type 6Superb. I am a superb class!! I hold the superb value of 52! Now processing type 5Grand. I am a grand class! Now processing type 11Magnificent. I am a magnificent class!!! I hold the character B and the integer 97! Yes, you're really magnificent.
  • 1 回答
  • 2 关注
  • 1013 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信