您的位置:首页 > 编程语言 > C语言/C++

C++之虚函数

2016-03-13 22:14 288 查看
<span style="font-size:18px;">#include <iostream>

using namespace std ; 

class   AA 
{
	public:
		int a ;

		//虚函数
		virtual void say_hello(void)
		{
			cout << "this is your parent " << endl ; 
		}

};

class  BB : public  AA
{
	public:
		int b ; 

//如果子类没有实现虚函数,则多态中会调用父类的虚函数
//如果子类有重新实现虚函数,则多态中会调用子类的虚函数 
//__weak   相似

//		void say_hello(void)
//		{
//			cout << "this is your son " << endl ; 
//		}
};

int main(void)
{
	BB bb ; 

	AA * aa = NULL ;

	aa =  (AA *)&bb ; 

	aa->say_hello();
	

	return  0 ; 
}

</span>

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: