1 Star 1 Fork 0

zeyes/cp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rtti1.cpp 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
zeyes 提交于 2015-07-28 22:33 +08:00 . 2015.07.28 22:33
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
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 c = 'A') : Superb(h), ch(c) {}
void Speak() const { cout << "I am magnificent class!!!\n"; }
void Say() const { cout << "I hold the character " << ch << " and the integer " << Value() << "!\n"; }
};
Grand * GetOne();
int main()
{
std::srand(std::time(0));
Grand *pg;
Superb* ps;
for(int i = 0; i < 5; i++)
{
pg = GetOne();
pg->Speak();
if( ps = dynamic_cast<Superb *>(pg))
ps->Say();
}
return 0;
}
Grand * GetOne()
{
Grand * p;
switch(std::rand() % 3)
{
case 0: p = new Grand(std::rand() % 100);
break;
case 1: p = new Superb(std::rand() % 100);
break;
case 2: p = new Magnificent(std::rand() % 100, 'A' + std::rand() % 26);
break;
}
return p;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/zeyes/cp.git
git@gitee.com:zeyes/cp.git
zeyes
cp
cp
master

搜索帮助