cpp01.cpp

来自「C++参考书」· C++ 代码 · 共 50 行

CPP
50
字号

// Coded by plusir -- Jan.14.2003.
// Standard C++ Bible -- (P740-27-1)

#include <iostream>
#include <typeinfo>
using namespace std ;

class Shape
{
	public:
		virtual void foo( void ) const { } ;
} ;

class Circle : public Shape { } ;
class Rectangle : public Shape { } ;

void process( Shape * ) ;

int main()
{
	Circle circle ;
	process( &circle ) ;

	Rectangle rect ;
	process( &rect ) ;

	Shape shape ;
	process( &shape ) ;

	return 0 ;
}

void process( Shape *sp )
{
	Circle *cp = dynamic_cast<Circle*>( sp ) ;
	if ( cp != NULL ) {
		cout << "Processing a Circle." << endl ;
		return ;
	}

	Rectangle *rp = dynamic_cast<Rectangle*>( sp ) ;
	if ( rp != NULL ) {
		cout << "Processing a Rectangle." << endl ;
		return ;
	}

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

⌨️ 快捷键说明

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