13.1 对虚函数、多态性和抽象类的理解

本文通过三个C++源程序示例介绍了多态性的概念及其应用。探讨了虚函数和纯虚函数的作用,展示了如何使用指向基类的指针来调用派生类中的成员函数,实现了动态绑定。

* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:第十三周 任务一

* 作 者:杨森
* 完成日期: 2012 年 05 月 15 日
* 版 本 号: V1.0

源程序 1:

#include <iostream> 
using namespace std;

class Vehicle 
{
public: 
	void run() const { cout << "run a vehicle. "<<endl; } //(2) run()为虚函数
}; 

class Car: public Vehicle 
{
public: 
#include <iostream> 
using namespace std;

class Vehicle 
{
public: 
	virtual void run() const =0; //(2) run()为虚函数
}; 

class Car: public Vehicle 
{
public: 
	void run() const {cout << "run a car. "<<endl; 	} 
}; 

class Airplane: public Vehicle 
{
public: 
	void run() const {cout << "run a airplane. "<<endl;} 
}; 

int main() 
{
	cout<<"(a) 直接用对象访问成员函数: "<<endl;
	//Vehicle v;编译器在此行提示错误!
	//v.run();编译器在此行提示错误!
	Car car; 
	Airplane airplane; 
	car.run();
	airplane.run();

	cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
	Vehicle *vp;
	vp=&car;
	vp->run();
	vp=&airplane;
	vp->run();
	system("pause");
	return 0;
} 

void run() const {cout << "run a car. "<<endl; } }; class Airplane: public Vehicle {public: void run() const {cout << "run a airplane. "<<endl;} }; int main() {cout<<"(a) 直接用对象访问成员函数: "<<endl;Vehicle v;v.run();Car car; Airplane airplane; car.run();airplane.run();cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;Vehicle *vp;vp=&car;vp->run();vp=&airplane;vp->run();system("pause");return 0;}

 

运行结果:

源程序 2:

#include <iostream> 
using namespace std;

class Vehicle 
{
public: 
	virtual void run() const { cout << "run a vehicle. "<<endl; } //(2) run()为虚函数
}; 

class Car: public Vehicle 
{
public: 
	void run() const {cout << "run a car. "<<endl; 	} 
}; 

class Airplane: public Vehicle 
{
public: 
	void run() const {cout << "run a airplane. "<<endl;} 
}; 

int main() 
{
	cout<<"(a) 直接用对象访问成员函数: "<<endl;
	Vehicle v;
	v.run();
	Car car; 
	Airplane airplane; 
	car.run();
	airplane.run();

	cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
	Vehicle *vp;
	vp=&car;
	vp->run();
	vp=&airplane;
	vp->run();
	system("pause");
	return 0;
} 


运行结果:

源程序 3:

#include <iostream> 
using namespace std;

class Vehicle 
{
public: 
	virtual void run() const =0; //(2) run()为虚函数
}; 

class Car: public Vehicle 
{
public: 
	void run() const {cout << "run a car. "<<endl; 	} 
}; 

class Airplane: public Vehicle 
{
public: 
	void run() const {cout << "run a airplane. "<<endl;} 
}; 

int main() 
{
	cout<<"(a) 直接用对象访问成员函数: "<<endl;
	//Vehicle v;编译器在此行提示错误!
	//v.run();编译器在此行提示错误!
	Car car; 
	Airplane airplane; 
	car.run();
	airplane.run();

	cout<<"(b) 用指向基类的指针访问成员函数: "<<endl;
	Vehicle *vp;
	vp=&car;
	vp->run();
	vp=&airplane;
	vp->run();
	system("pause");
	return 0;
} 


运行结果:

 

小感:

一、多态性

指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动作

 

二、虚函数

虚函数是在基类中被声明为virtual,并在派生类中重新定义的成员函数。可实现成员函数的动态重载。

 

三、纯虚函数

纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己的实现方法

 






                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值