class Grand{ //has virtual methods};
class Super:public Grand {...}
class Magnificent : public Superb{...}
Grand *pg = new Grand ; Grand *ps = new Superd; Grand *pm = new Manificent;
Superb pm = dynamic_cast< Superb > (pg) ;
// test1002.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include<iostream>
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 a magnificent class !!!! \n";
}
void Say() const
{
cout << "I hold the character " << ch << " and the integer " << Value() <<"! \n";
}
};
Grand * GetOne();
int main()
{
std::srand(static_cast<unsigned int>(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();
}
}
system("pause");
return 0;
}
Grand * GetOne()
{
Grand * p = new Grand();
switch (std::rand() % 3)
{
delete p;
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, std::rand() % 26); break;
}
return p;
}
运行结果:
I am a superb class !
I hold the superb value of 3!
I am a magnificent class !!!!
I hold the character and the integer 5!
I am a grand class
I am a grand class
I am a magnificent class !!!!
I hold the character and the integer 87!
请按任意键继续. . .
// test1002.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include<iostream>
#include <typeinfo>
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 a magnificent class !!!! \n";
}
void Say() const
{
cout << "I hold the character " << ch << " and the integer " << Value() <<"! \n";
}
};
Grand * GetOne();
int main()
{
std::srand(static_cast<unsigned int>(std::time(0)));
Grand * pg;
Superb * ps;
for (int i = 0; i < 5; i++)
{
pg = GetOne();
cout << "Now Process type " << typeid (*pg).name() << ". \n"; //显示
pg->Speak();
if (ps = dynamic_cast<Superb *>(pg)) {
ps->Say();
}
}
system("pause");
return 0;
}
Grand * GetOne()
{
Grand * p = new Grand();
switch (std::rand() % 3)
{
delete p;
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, std::rand() % 26); break;
}
return p;
}
运行结果:
Now Process type class Superb.
I am a superb class !
I hold the superb value of 86!
Now Process type class Grand.
I am a grand class
Now Process type class Superb.
I am a superb class !
I hold the superb value of 48!
Now Process type class Grand.
I am a grand class
Now Process type class Magnificent.
I am a magnificent class !!!!
I hold the character and the integer 75!
请按任意键继续. . .
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有