码迷,mamicode.com
首页 > 编程语言 > 详细

C++之虚函数

时间:2016-03-14 19:00:42      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

<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>

运行结果:

技术分享

C++之虚函数

标签:

原文地址:http://blog.csdn.net/morixinguan/article/details/50880822

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!