多态——郭炜

多态的实现

虚函数

在类的定义中,前面有 virtual 关键字的成员函数就是虚函数
构造和静态函数不能是虚函数
虚函数可以参与多态,普通的函数不能参与多态
class base {
	virtual int get() ;
};
int base::get() //这里不用
{ }
virtual 关键字只用在类定义里的函数声明中,写函数体时不用。

多态的表现形式一

派生类的指针可以赋给基类指针
通过基类引用调用基类和派生类中的同名虚函数时: 
	(1)若该指针指向的是一个基类的对象,那么被调用是基类的虚函数; 
	(2)若该指针指向的是一个派生类的对象,那么被调用的是派生类的虚函数。
class CBase { 
public:
	virtual void SomeVirtualFunction() { } 
};
class CDerived:public CBase {
public :
	virtual void SomeVirtualFunction() { }
};
int main() {
	CDerived ODerived;
	CDerived O;
	CBase * p = & ODerived;
	p = O;//这样也可以让p指向O对象
	p -> SomeVirtualFunction(); //调用哪个虚函数取决于p指向哪种类型的对象
	return 0;
}

多态的表现形式二

派生类的对象可以赋给基类引用
通过基类引用调用基类和派生类中的同名虚函数时: 
	(1)若该引用引用的是一个基类的对象,那么被调用是基类的虚函数; 
	(2)若该引用引用的是一个派生类的对象,那么被调用的是派生类的虚函数。
class CBase { 
public:
	virtual void SomeVirtualFunction() { } 
};
class CDerived:public CBase {
public :
	virtual void SomeVirtualFunction() { }
};
int main() {
	CDerived ODerived;
	CBase & r = ODerived;
	r.SomeVirtualFunction(); //调用哪个虚函数取决于r引用哪种类型的对象
	return 0;
} 

例子

#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
class CShape{
public:
	virtual double Area() = 0; //纯虚函数
	virtual void PrintInfo() = 0;
}; 

class CRectangle:public CShape{
public:
	int w,h; 
	virtual double Area();
	virtual void PrintInfo();
};
class CCircle:public CShape {
public:
	int r; 
	virtual double Area();
	virtual void PrintInfo();
};
class CTriangle:public CShape {
public:
	int a,b,c; 
	virtual double Area();
	virtual void PrintInfo();
};
CShape * pShapes[100];//这个是常用的办法,使指针数组中各个指针指向派生类————多态
int MyCompare(const void * s1, const void * s2);
int main()
{ 
	int i; int n;
	CRectangle * pr; CCircle * pc; CTriangle * pt;
	cin >> n;
	for( i = 0;i < n;i ++ ) {
		char c;
		cin >> c;
		switch(c) {
			case 'R':
			pr = new CRectangle();
			cin >> pr->w >> pr->h;
			pShapes[i] = pr; 
			break;
			case 'C':
			pc = new CCircle();
			cin >> pc->r;
			pShapes[i] = pc;
			break;
			case 'T':
			pt = new CTriangle();
			cin >> pt->a >> pt->b >> pt->c;
			pShapes[i] = pt; 
			break;
		} 
	}
	qsort(pShapes,n,sizeof( CShape*),MyCompare);//更加强大的排序函数
	for( i = 0;i <n;i ++)
		pShapes[i]->PrintInfo(); 
	return 0;
}
int MyCompare(const void * s1, const void * s2)
{
	double a1,a2;
	CShape * * p1 ; // s1,s2 是 void * ,不可写 “* s1”来取得s1指向的内容
	CShape * * p2;
	p1 = ( CShape * * ) s1; //s1,s2指向pShapes数组中的元素,数组元素的类型是CShape *
	p2 = ( CShape * * ) s2; // 故 p1,p2都是指向指针的指针,类型为 CShape ** 
	a1 = (*p1)->Area(); // * p1 的类型是 Cshape * ,是基类指针,故此句为多态
	a2 = (*p2)->Area();
	if( a1 < a2 ) 
	return -1;
	else if ( a2 < a1 )
	return 1;
	else
	return 0;
}

其他注意!

在非构造函数,非析构函数的成员函数中调用虚函数,是多态!!!
class Base {
public:
	void fun1() { this->fun2(); } //this是基类指针,fun2是虚函数,所以是多态
	virtual void fun2() { cout << "Base::fun2()" << endl; }
};
class Derived:public Base {
public:
	virtual void fun2() { cout << "Derived:fun2()" << endl; }
};
int main() {
	Derived d;
	Base * pBase = & d;
	pBase->fun1();
	return 0;
}
在构造函数和析构函数中调用虚函数,不是多态。
派生类中和基类中虚函数同名同参数表的函数,不加virtual也自动成为虚函数
class myclass {
public:
	virtual void hello(){cout<<"hello from myclass"<<endl; };
	virtual void bye(){cout<<"bye from myclass"<<endl;}
};
class son:public myclass{ public:
	void hello(){ cout<<"hello from son"<<endl;};//不用加virtual
	son(){ hello(); };
	~son(){ bye(); };
};
class grandson:public son{ public:
	void hello(){cout<<"hello from grandson"<<endl;};
	void bye() { cout << "bye from grandson"<<endl;}
	grandson(){cout<<"constructing grandson"<<endl;};
	~grandson(){cout<<"destructing grandson"<<endl;};
};
int main(){
	grandson gson;
	son *pson;
	pson=&gson; 
	pson->hello(); //多态
	return 0;
}
输出:
hello from son
constructing grandson
hello from grandson
destructing grandson
bye from myclass

多态的实现原理

每一个有虚函数的类(或有虚函数的类的派生类)都有一个虚函数表
该类在生成对象的时候,在对象的首地址中都放着虚函数表的指针(虚函数表的首地址)
当执行虚函数的时候会对应着虚函数表去查找

代价
空间:每个类都会多出来四个字节来存放虚函数的指针
时间:查虚函数表
#include <iostream>
using namespace std;
class A {
public: virtual void Func() { cout << "A::Func" << endl; } 
};
class B:public A {
public: virtual void Func() { cout << "B::Func" << endl; } 
};
int main() {
	A a;
	A * pa = new B();
	pa->Func();//输出B::Func
	//64位程序指针为8字节
	long long * p1 = (long long * ) & a; 
	long long * p2 = (long long * ) pa;
	* p2 = * p1; //这里将pa 首地址指向类B虚函数表的指针 换成指向类A虚函数表的指针
	pa->Func();//输出A::Func
	return 0; 
}

虚析构函数

问题提出

通过基类的指针删除(delete)派生类对象时,通常情况下只调用基类的析构函数
但是,删除一个派生类的对象时,应该先调用派生类的析构函数,然后调用基类的析构函数。

解决办法

把基类的析构函数声明为virtual
派生类的析构函数可以virtual不进行声明————自动为虚析构函数
通过基类的指针删除派生类对象时,首先调用派生类的析构函数,然后调用基类的析构函数

注意:
不允许以虚函数作为构造函数
一般来说,一个类如果定义了虚函数,则应该将析构函数也定义成虚函数。或者,一个类打算作为基类使用,也应该将析构函数定义成虚函数。

纯虚函数和抽象类

纯虚函数————没有函数体的虚函数

class A {
private:
	int a;
public:
	virtual void Print( ) = 0 ; //纯虚函数
	void fun() { cout << "fun"; } 
};

抽象类

抽象类只能作为基类来派生新类使用,不能创建抽象类的对象
抽象类的指针和引用可以指向由抽象类派生出来的类的对象

A a ; // 错,A 是抽象类,不能创建对象
A * pa ; // ok,可以定义抽象类的指针和引用
pa = new A ; //错误, A 是抽象类,不能创建对象

在抽象类的成员函数内可以调用纯虚函数————这样调用的话就是多态
但是在构造函数或析构函数内部不能调用纯虚函数————构造函数或析构函数中不能调用虚函数

如果一个类从抽象类派生而来,那么当且仅当它实现了基类中的所有纯虚函数,它才能成为非抽象类。

静态函数不能是虚函数————多态是针对对象的,我们总说对象的多态,静态函数是先于对象的函数,是类级别的函数

重载、多态、覆写

同一个类中
重载:只用函数名字一样就行了
多个类中
覆盖:多态————函数签名全部相同
遮蔽:覆写————不是多态就是遮蔽

多态的前提条件

必须存在一个继承体系结构
继承体系结构一些类必须具有相同函数签名的virtual成员函数。
至少有一个基类类型的指针或基类类型的引用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值