中国大学MOOC程序设计与算法(三):C++ 面向对象程序设计 第六周 多态 笔记 之 更多多态程序实例

第六周 多态
1.虚函数和多态的基本概念
2.多态实例:魔法门之英雄无敌
3.更多多态程序实例
4.多态的实现原理
5.虚析构函数、纯虚函数和抽象类

3.更多多态程序实例

实例1 几何形体处理程序:输入若干个几何形体的参数,要求按面积排序输出。输出时要指明形状。

Input:
第一行是几何形体数目n(不超过100).下面有n行,每行以一个字母c开头.
若 c 是 ‘R’,则代表一个矩形,本行后面跟着两个整数,分别是矩形的宽和高;
若 c 是 ‘C’,则代表一个圆,本行后面跟着一个整数代表其半径
若 c 是 ‘T’,则代表一个三角形,本行后面跟着三个整数,代表三条边的长度
Output:
按面积从小到大依次输出每个几何形体的种类及面积。每行一个几何形体,输
出格式为:
形体名称:面积

Sample Input:
3
R 3 5
C 9
T 3 4 5
Sample Output
Triangle:6
Rectangle:15
Circle:254.34

程序:

#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();
};
double CRectangle::Area() {
	return w * h;
}
void CRectangle::PrintInfo() {
	cout << "Rectangle:" << Area() << endl;
}
double CCircle::Area() {
	return 3.14 * r * r ;
}
void CCircle::PrintInfo() {
	cout << "Circle:" << Area() << endl;
}
double CTriangle::Area() {
	double p = ( a + b + c) / 2.0;
	return sqrt(p * ( p - a)*(p- b)*(p - c));
}
void CTriangle::PrintInfo() {
	cout << "Triangle:" << Area() << endl;
}
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;
}

好处:如果添加新的几何形体,比如五边形,则只需要从CShape派生出CPentagon,以及在main中的switch语句中增加一个case,其余部分不变。
注意:用基类指针数组存放指向各种派生类对象的指针,然后遍历该数组,就能对各个派生类对象做各种操作,是很常用的做法。

实例2 多态的另一个例子

class Base {
	public:
		void fun1() { 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;
}

输出: Derived:fun2()
输出不是"Base::fun2()"的原因是:void fun1() { fun2(); }等价于void fun1() { this->fun2(); } ,this是基类指针,fun2是虚函数,所以是多态,而pBase 指向的是一个派生类对象d,因此调用的是派生类的虚函数。即输出“Derived:fun2()”。
结论:在非构造函数,非析构函数的成员函数中调用虚函数,是多态!!!

构造函数和析构函数中调用虚函数

在构造函数和析构函数中调用虚函数,不是多态。编译时即可确定,调用的函数是自己的类或基类中定义的函数,不会等到运行时才决定调用自己的还是派生类的函数。
例子:

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

注意:派生类中和基类中虚函数同名同参数表的函数,不加virtual也自动成为虚函数

虚函数的访问权限

class Base {
	private:
		virtual void fun2() { cout << "Base::fun2()" << endl; }
};
class Derived:public Base {
	public:
		virtual void fun2() { cout << "Derived:fun2()" << endl; }
};
Derived d;
Base * pBase = & d;
pBase -> fun2(); // 编译出错

编译出错是因为 fun2() 是Base的私有成员。即使运行到此时实际上调用的应该是Derived的公有成员 fun2()也不行,因为语法检查是不考虑运行结果的。
如果 将Base中的 private换成public,即使Derived中的fun2() 是private的,编译依然能通过,也能正确调用Derived::fun2()。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值