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

📄 pr27001.cpp

📁 c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出版社的光盘
💻 CPP
字号:
////////////////////////////////////////
// File Name: pr27001.cpp
////////////////////////////////////////
#include <typeinfo>
#include <iostream>

////////////////////////////////////////
// Define the Shape class.
////////////////////////////////////////
class Shape
{
public:
    virtual void foo() {}  // To enable rtti.
};

////////////////////////////////////////
// Declare classes derived from Shape.
////////////////////////////////////////
class Circle    : public Shape { };
class Rectangle : public Shape { };

////////////////////////////////////////
// Process Circle and Rectangle objects.
////////////////////////////////////////
void Process(Shape* sp)
{
    // Downcast Shape* to Circle*.
    Circle* cp = dynamic_cast<Circle*>(sp);
    if (cp != 0)
    {
        std::cout << "Processing a Circle" << std::endl;
        return;
    }

    // Downcast Shape* to Rectangle*.
    Rectangle* rp = dynamic_cast<Rectangle*>(sp);
    if (rp != 0)
    {
        std::cout << "Processing a Rectangle" << std::endl;
        return;
    }

    std::cout << "Unknown Shape, cannot process" << std::endl;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // Instantiate and process a Circle.
    Circle circle;
    Process(&circle);

    // Instantiate and process a Rectangle.
    Rectangle rect;
    Process(&rect);

    // Instantiate and process a generic Shape.
    Shape shape;
    Process(&shape);

    return 0;
}

⌨️ 快捷键说明

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