【C++面向对象程序设计】CH6 多态性与虚函数

多态性与虚函数介绍
摘要由CSDN通过智能技术生成

目录

一、多态性的概念

二、一个典型的例子——【例6.1】

1.声明基类point类

2.声明派生类circle

​编辑

3.声明circle的派生类cylinder

三、虚函数

1.虚函数的作用

(1)派生类的对象可以给基类的对象赋值

(2)派生类的对象可以初始化基类引用

(3)派生类的对象的地址可以赋给指向基类的指针

(4)【例6.2】基类与派生类中有同名函数display

2.静态关联与动态关联

(1)研究虚函数

​(2)虚函数用法1

(3)虚函数用法2

(4)虚函数用法2(改)

(5)虚函数用法3

(6)虚函数用法3(改)

3.在什么情况下应当声明虚函数

4.虚析构函数

【例6.3】派生类对象析构函数的执行演示

四、纯虚函数与抽象类

1.纯虚函数

2.抽象类

3.应用实例

4.【例6.4】虚函数和抽象基类的应用


一、多态性的概念

        在C++程序设计中,多态性是指具有不同功能的函数可以用一个函数名。在面向对象程序设计中这样描述多态性:向不同的对象发送同一个消息,它们接受后会产生不同的行为(即方法)。

        多态性是指同样的消息被不同的对象接收时导致的不同行为。

        消息是指对类的成员函数的调用。

        不同的行为是指不同的实现,也就是调用了不同的函数。

        函数的重载、运算符重载都是多态现象。从系统实现的观点看,多态性分为两类:静态多态和动态多态性。以前学过的函数重载和运算符重载属于静态多态性,在编译程序时系统就可以确定调用哪个函数,因此静态多态性又称编译时的多态性。静态多态性是通过函数重载实现的。动态多态性是在程序运行中才能确定操作所针对的对象。他又称运行时的多态性。动态多态性是通过虚函数实现的。

        这章要研究的问题是:从一个基类派生出不同的派生类时,各派生类可以使用与基类成员相同的成员名,如果在运行时用相同的成员名调用类的成员,会调用哪个类的成员?也就是说,通过继承而产生了相关的不同的派生类,与基类成员同名的成员在各个派生类中有不同的含义,也就是说,多态性是一个接口多种方法。

二、一个典型的例子——【例6.1】

先建立一个点类(point),有数据成员x,y(坐标)。以它为基类派生一个圆类,增加数据成员r(半径),再以圆为直接基类派生出一个圆柱体类,再增加数据成员h(高),要求重载运算符<<和>>使之能输出以上的对象。

1.声明基类point类

#include <iostream>
using namespace std;

class Point 
{
	protected:
		float x, y;
	public:
		Point(float = 0, float = 0);
		void setPoint(float, float);
		float getX() const 
		{
			return x;
		}
		float getY() const 
		{
			return y;
		}
		friend ostream &operator<<(ostream &, const Point &);
};

//    Point的构造函数
Point::Point(float a, float b) 
{
	x = a;
	y = b;
}

//    设置x和y的坐标值
void Point::setPoint(float a, float b) 
{
	x = a;
	y = b;
}

//    输出点的坐标
ostream &operator<<(ostream &output, const Point &p) 
{
	output << "[" << p.x << "," << p.y << "]" << endl;
	return output;
}

int main() 
{
	Point p(3.5, 6.4);
	cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
	p.setPoint(8.5, 6.8);
	cout << "p(new):" << p << endl;
	return 0;
}

【注】

// friend ostream & operator<<(ostream &,const Point &);
//对"<<"运算符的重载。

         一般我们用的“<<”只能输出整型、实型等普通类型。要想输出类类型,则必须对“<<”进行重载,其中一个参数为类类型对象。为了方便对对象内部数据的操作,设置为friend友元函数。为了能达到cout<<对象<<对象<<endl;的连续输出对象的效果,设置返回类型为引用。

        参数:第一个为输出流对象;第二个为要输出的对象(为了防止产生临时对象、提高程序的效率,将参数设置为引用类型,但引用类型又能改变实参的值,所以设置为const)。

2.声明派生类circle

        在1的基础上,再声明派生类circle的部分。

#include <iostream>
using namespace std;

class Point 
{
	protected:
		float x, y;
	public:
		Point(float = 0, float = 0);
		void setPoint(float, float);
		float getX() const 
		{
			return x;
		}
		float getY() const 
		{
			return y;
		}
		friend ostream &operator<<(ostream &, const Point &);
};

//    Point的构造函数
Point::Point(float a, float b) 
{
	x = a;
	y = b;
}

//    设置x和y的坐标值
void Point::setPoint(float a, float b) 
{
	x = a;
	y = b;
}

//    输出点的坐标
ostream &operator<<(ostream &output, const Point &p) 
{
	output << "[" << p.x << "," << p.y << "]" << endl;
	return output;
}

class Circle: public Point 
{
	protected:
		float radius;
	public:
		Circle(float x = 0, float y = 0, float r = 0);
		void setRadius(float);
		float getRadius() const;
		float area () const;
		friend ostream &operator<<(ostream &, const Circle &);
};

Circle::Circle(float a, float b, float r): Point(a, b), radius(r) {}

void Circle::setRadius(float r) 
{
	radius = r;
}

float Circle::getRadius() const 
{
	return radius;
}

float Circle::area() const 
{
	return 3.14159 * radius * radius;
}

ostream &operator<<(ostream &output, const Circle &c) 
{
	output << "Center=[" << c.x << "," << c.y << "],Radius = " << c.radius << ", area = " << c.area() << endl;
	return output;
}

int main() 
{
	Circle c(3.5, 6.4, 5.2);
	cout << "original circle:\nx=" << c.getX() << ",y = " << c.getY() << ", r = " << c.getRadius()  << ",area = " <<
	     c.area() << endl;
	c.setRadius(7.5);
	c.setPoint(5, 5);
	cout << "new circle:\n" << c;
	Point &pRef = c;
	cout << "pRef:" << pRef;
	return 0;
}

3.声明circle的派生类cylinder

        以circle为基础,从circle类派生出cylinder类。

#include <iostream>
using namespace std;
class Point 
{
	protected:
		float x, y;
	public:
		Point(float = 0, float = 0);
		void setPoint(float, float);
		float getX() const 
		{
			return x;
		}
		float getY() const 
		{
			return y;
		}
		friend ostream &operator<<(ostream &, const Point &);
};
//    Point的构造函数
Point::Point(float a, float b) 
{
	x = a;
	y = b;
}
//    设置x和y的坐标值
void Point::setPoint(float a, float b) 
{
	x = a;
	y = b;
}
//    输出点的坐标
ostream &operator<<(ostream &output, const Point &p) 
{
	output << "[" << p.x << "," << p.y << "]" << endl;
	return output;
}
class Circle: public Point 
{
	protected:
		float radius;
	public:
		Circle(float x = 0, float y = 0, float r = 0);
		void setRadius(float);
		float getRadius() const;
		float area () const;
		friend ostream &operator<<(ostream &, const Circle &);
};
Circle::Circle(float a, float b, float r): Point(a, b), radius(r) {}
void Circle::setRadius(float r) 
{
	radius = r;
}
float Circle::getRadius() const 
{
	return radius;
}
float Circle::area() const 
{
	return 3.14159 * radius * radius;
}
ostream &operator<<(ostream &output, const Circle &c) 
{
	output << "Center=[" << c.x << "," << c.y << "],Radius = " << c.radius << ", area = " << c.area() << endl;
	return output;
}
class Cylinder: public Circle 
{
	public:
		Cylinder (float x = 0, float y = 0, float r = 0, float h = 0);
		void setHeight(float);
		float getHeight() const;
		float area() const;
		float volume() const;
		friend ostream &operator<<(ostream &, const Cylinder &);
	protected:
		float height;
};
Cylinder::Cylinder(float a, float b, float r, float h)
	: Circle(a, b, r), height(h) {}
void Cylinder::setHeight(float h) 
{
	height = h;
}
float Cylinder::getHeight() const 
{
	return height;
}
float Cylinder::area() const 

	return 2 * Circle::a
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值