⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtti.cpp

📁 候老师讲的RTTI 资源难得 希望大家喜欢
💻 CPP
字号:
// RTTI.cpp - a demonstration of RTTI in C++
#include <typeinfo.h>
#include <iostream.h>
#include <string.h>

class graphicImage
{
protected:
    char name[80];

public:
    graphicImage()
    {
        strcpy(name,"graphicImage");
    }

    virtual void display()
    {
        cout << "Display a generic image." << endl;
    }

    char* getName()
    {
        return name;
    }
};
//----------------------------------------------------------------
class GIFimage : public graphicImage
{
public:
    GIFimage()
    {
        strcpy(name,"GIFimage");
    }

    void display()
    {
        cout << "Display a GIF file." << endl;
    }
};

class PICTimage : public graphicImage
{
public:
    PICTimage()
    {
        strcpy(name,"PICTimage");
    }

    void display()
    {
        cout << "Display a PICT file." << endl;
    }
};
//----------------------------------------------------------------
void processFile(graphicImage *type)
{
    if (typeid(GIFimage) == typeid(*type))
    {
        ((GIFimage *)type)->display();
    }
    else if (typeid(PICTimage) == typeid(*type))
    {
        ((PICTimage *)type)->display();
    }
    else
        cout << "Unknown type! " << (typeid(*type)).name() << endl;
}

void main()
{
    graphicImage *gImage = new GIFimage();
    graphicImage *pImage = new PICTimage();

    //processFile(gImage);
    //processFile(pImage);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -