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

C++学习笔记26,虚函数

时间:2014-06-22 20:59:57      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:c++   虚函数   

在C++里面,虚函数是一类重要的函数!可以通过虚函数定义不同对象同一行为的不同实现。

举一个简单的例子:

#include <iostream>
#include <string>
using namespace std;
class Animal{
protected:
	string name;
public:
	Animal(const string &s):name(s){
	}
	virtual ~Animal(){
	}
	virtual void speak()const{
		cout<<"I'm a Animal!"<<endl;
	}
};
class Dog:public Animal{
public:
	Dog(const string &s):Animal(s){
	}
	virtual ~Dog(){
	}
	virtual void speak()const override{
		cout<<"This's a Dog!"<<endl;
	}
};
int main(){
	Animal a("AnimalOne");
	Dog d1("DogOne");

<span style="white-space:pre">	</span>//用指针调用speak()
	Animal *p1=&a;
	Animal *p2=&d1;
	p1->speak();
	p2->speak();	
<span style="white-space:pre">	</span>//用引用调用speak()
	Animal &r1=a;
	Animal &r2=d1;
	r1.speak();
	r2.speak();

	return 0;

}
结果:

bubuko.com,布布扣

可以看出,通过指针和引用可以调用对应的虚函数.即便指针和引用都声明为Animal 类型,但是却可以调用相应的函数(Dog::speak()).

因此,如果需要在派生类中重新定义基类的方法,应该将该方法设置为虚方法.

需要注意的是只有指针和引用才能正确引发相应的虚函数.同时函数必须声明为虚的.如果不是的话,将只会调用相应的类成员函数.

例如:

#include <iostream>
#include <string>
using namespace std;
class Animal{
protected:
	string name;
public:
	Animal(const string &s):name(s){
	}
	virtual ~Animal(){
	}
	void speak()const{
		cout<<"I'm a Animal!"<<endl;
	}
};
class Dog:public Animal{
public:
	Dog(const string &s):Animal(s){
	}
	virtual ~Dog(){
	}
	void speak()const {
		cout<<"This's a Dog!"<<endl;
	}
};
int main(){
	Animal a("AnimalOne");
	Dog d1("DogOne");

	Animal *p1=&a;
	Animal *p2=&d1;
	p1->speak();
	p2->speak();	

	Animal &r1=a;
	Animal &r2=d1;
	r1.speak();
	r2.speak();

	return 0;

}
运行结果:

bubuko.com,布布扣
如果成员函数不是虚的,就不能达到这样的效果.这就是动态绑定.

再看一个例子:

#include <iostream>
#include <string>
using namespace std;
class Animal{
protected:
	string name;
public:
	Animal(const string &s):name(s){
	}
	virtual ~Animal(){
	}
	
	//非虚函数
	void eat()const{
		cout<<"Animal eat!"<<endl;
	}	

	//不被重写的虚函数
 	virtual void run()const{
		cout<<"Animal run!"<<endl;
	}

	//会被重写的虚函数
	virtual void speak()const{
		cout<<"I'm a Animal!"<<endl;
	}
};
class Dog:public Animal{
public:
	Dog(const string &s):Animal(s){
	}
	virtual ~Dog(){
	}
	
	//新定义的函数eat,将掩盖旧的版本,非重写(重写是指重写virtual函数)
	void eat()const{
		cout<<"Dog eat!"<<endl;
	}


	//重写speak()
	virtual void speak()const override{
		cout<<"This's a Dog!"<<endl;
	}
};
int main(){
	Animal a("AnimalOne");
	Dog d1("DogOne");

	Animal *p1=&a;
	Animal *p2=&d1;
	p1->speak();	
	p2->speak();	
	p1->eat();
	p2->eat();	//call Animal::eat()
	p1->run();
	p2->run();	//call Animal::run()
	
	Animal &r1=a;
	Animal &r2=d1;
	r1.speak();
	r2.speak();
	r1.eat();
	r2.eat();
	r1.run();
	r2.run();
	
	return 0;

}

结果:

bubuko.com,布布扣




C++学习笔记26,虚函数,布布扣,bubuko.com

C++学习笔记26,虚函数

标签:c++   虚函数   

原文地址:http://blog.csdn.net/qq844352155/article/details/32074737

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