cpp02.cpp

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

CPP
48
字号

// Coded by plusir -- Jan.14.2003.
// Standard C++ Bible -- (P742-27-2)

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

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

class TextBox : public Control { } ;
class EditBox : public TextBox { } ;
class Button : public Control { } ;

void paint( Control & ) ;

int main()
{
	Control ct ;
	paint( ct ) ;

	Button bt ;
	paint( bt ) ;

	TextBox tb ;
	paint( tb ) ;

	EditBox eb ;
	paint( eb ) ;

	return 0 ;
}

void paint( Control &cr )
{
	try {
		TextBox& ctl = dynamic_cast<TextBox&>( cr ) ;
		cout << "Paint a TextBox" << endl ;
	}
	catch ( bad_cast ) {
		cout << "nonTextBox, can't paint" << endl ;
	}
}

⌨️ 快捷键说明

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