继承与多态
在C++中继承与多态是两个很重要的概念,而这两个概念也是面向对象很重要的概念,所以有必要对这两个概念有一个很清楚的理解.继承是面向对象中,能从基类继承派生出很多的类,这种继承自父类的机制就叫做继承.
多态(Polymorphism)按字面的意思就是“多种状态”。在面向对象语言中,接口的多种不同的实现方式即为多态。引用Charlie Calverts对多态的描述——多态性是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作(摘自“Delphi4 编程技术内幕”)。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。多态性在Object Pascal和C++中都是通过虚函数(Virtual Function) 实现的。
用一个例子展示继承与多态:
形状类的定义:
#ifndef W1_EXAMPLE1_SHAPE_H
#define W1_EXAMPLE1_SHAPE_H
////////////////////
// 继承 和 多态 实例
////////////////////
//形状类
class CShape
{
public:
CShape();
~CShape();
public:
virtual float GetArea(); //基类中定义的虚函数
void GetType();
void GetCommon(); //公共的属性
};
//三角形
class CTriangle : public CShape //继承
{
public:
CTriangle(float h, float w);
~CTriangle();
public:
float GetArea(); //重载基类的虚函数,实现多态
void GetType();
private: //封装的信息隐藏
float H,W;
};
//圆形
class CCircle: public CShape
{
public:
CCircle(float r);
~CCircle();
float GetArea();
void GetType();
private:
float R;
};
#endif
形状类的实现
#include "StdAfx.h"
#include "stdio.h"
#include "Shape.h"
///////////////////////////
// CShape
/////////////////////////
CShape::CShape()
{
}
CShape::~CShape()
{
}
float CShape::GetArea()
{
printf("area=0.0\n");
return 0.0;
}
void CShape::GetType()
{
printf("This is a Shape\n");
}
void CShape::GetCommon()
{
printf("This is a Common atribute\n");
}
////////////////////////////
// CTriangle
///////////////////////////
CTriangle::CTriangle(float h, float w)
{
H = h;
W = w;
}
CTriangle::~CTriangle()
{
}
float CTriangle::GetArea()
{
printf("area=%f\n",H*W*0.5);
return H*W*0.5;
}
void CTriangle::GetType()
{
printf("This is a Triangle\n");
}
///////////////////////////
// 圆形
///////////////////////
CCircle::CCircle(float r)
{
R = r;
}
CCircle::~CCircle()
{
}
float CCircle::GetArea()
{
printf("area=%f\n",3.14159*R*R);
return 3.14159*R*R;
}
void CCircle::GetType()
{
printf("This is a Circle\n");
}
主函数:
// W1_Example1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "Shape.h"
int main(int argc, char* argv[])
{
//继承性
CShape myShape;
CTriangle myTriangle(1.5,1);
CCircle myCricle(1.5);
printf("1111--------------\n");
myShape.GetCommon();
myTriangle.GetCommon();
myCricle.GetCommon();
//多态:
//编译时多态 (针对非虚函数的重载)
//运行时多态 (针对虚函数的重载)
CShape *pShape;
CTriangle *pTriangle;
CCircle *pCricle;
printf("2222--------------\n");
pShape = &myShape;
pShape->GetType(); //基类 -> 基类 = 基类
pShape->GetArea();
pCricle = &myCricle;
pCricle->GetType(); //子类 -> 子类 = 子类
pCricle->GetArea();
printf("3333--------------\n");
//运行时多态
pShape = &myCricle; //基类 -> 子类
pShape->GetType(); //基类的GetType不是虚函数,仍调用基类方法,而非子类方法
pShape->GetArea(); //基类的GetArea 是虚函数 ,运行时多态,调用子类方法
pShape = &myTriangle;
pShape->GetType();
pShape->GetArea(); //多态
printf("4444--------------\n");
//编译时多态
pCricle = (CCircle*)&myShape; //子类 -> 基类
pTriangle = (CTriangle*)&myShape;
pCricle->GetType(); //子类重载了基类的GetType方法,
pTriangle->GetType(); //因此将基类指针强制转换为子类指针后,将调用子类重载后的方法,为编译时多态
pCricle->GetArea(); //这里需要注意 仍然是运行时的多态,调用的是基类的方法
pTriangle->GetArea(); //不常用,不需要记住
printf("--------------\n");
system("pause");
return 0;
}
源代码下载
0 评论 :
发表评论