C++面向对象:多态性

多态性

1.概念

多态性是面向对象的程序设计的一个重要特征。在面向对象的方法中一般是这样表述多态的:向不同的对象发送同一个信息,不同的对象在接收时会产生不同的行为。也就是说,每个对象用自己的方式去响应共同的消息。

2.典例

下面这段程序是两段基础的面向对象的程序,他主要包含了类的定义和运算符重载和派生类的使用,我们将用这个例子来讨论多态性,不明白这个程序的小伙伴们请点击下方链接出门左转,链接顺序由浅入深,自己选择合适难度进行复习

更适合滑湿宝宝的类的定义与使用笔记-CSDN博客

面向对象教程pro:对象的初始化_面向对象将对象初始化-CSDN博客

运算符重载:妈妈我会自己写运算符了-CSDN博客

点类的定义与输出
#include<iostream>
#include<iomanip>//设置输出保留小数点后两位,防止宝宝的生日输出不完整
using namespace std;
class Point {
protected:
	float x, y;
public:
	Point(float x = 0, float y = 0) {//默认构造函数
		this->x = x;
		this->y = y;
	}
	void setPoint(float x, float y) {//重新设置某点坐标值
		this->x = x;
		this->y = y;
	}
	float getX()const { return x; };//读取x坐标
	float getY()const { return y; };//读取y坐标
	friend ostream& operator<<(ostream&output, const Point&p) {//运算符重载(只能以友元函数方式重载)
		output << "(" << p.x << "," << p.y << ")" << endl;
		return output;
	}
};
int main() {
	Point p(11.14, 11.20);
	cout << setiosflags(ios::fixed) << setprecision(2) << p;
	//这行输出与下面的输出等价,此时的流插入运算符为重载后的运算符
	p.setPoint(5.20, 13.14);//重新设置该点的坐标值
	cout << "(" << p.getX() << "," << p.getY() << ")" << endl;//此时的流插入运算符未被重载
}
 点类派生圆类并输出
#include<iostream>
#include<iomanip>//输出格式控制头文件
using namespace std;
const double pi = 3.14159;//全局变量圆周率

class Point {//基类点类
protected:
	float x, y;
public:
	Point(float x = 0, float y = 0) {//默认构造函数
		this->x = x;
		this->y = y;
	}
	void setPoint(float x, float y) {//重新设置某点坐标值
		this->x = x;
		this->y = y;
	}
	float getX()const { return x; };//读取x坐标
	float getY()const { return y; };//读取y坐标
	friend 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):Point(x,y),radius(r) {	}//构造函数
	void setRadius(float r) {
		this->radius = r;
	}
	float getRadius() const {
		return radius;
	}
	float Area() const{
		//需要注意这个函数需要定义为常函数,一方面防止数据内篡改,一方面友元重载函数调用时要求函数参数为常对象
		return pi * radius * radius;
	}
	friend ostream& operator<<(ostream& output, const Circle&c) {//运算符重载(只能以友元函数方式重载)
		output << "Center: (" << c.x << "," << c.y << "), ",
			output << "r=" << c.radius,
			output << ", area=" << c.Area() << endl;
		return output;
	}
};

int main() {
	Circle c(11.14, 11.20, 5.26);
	cout << setiosflags(ios::fixed) << setprecision(2);//设置输出保留小数点后两位,防止宝宝的生日输出不完整
	cout << "the data of the origin circle is: " << endl;
	cout << "Center: (" << c.getX() << "," << c.getY() << "), r="
		<< c.getRadius() << ", area=" << c.Area() << endl;
	//因为Circle是Point类的公有派生类,因此可以调用Point中的公有权限函数
	c.setRadius(13.14);//重新设置半径
	c.setPoint(5.20, 5.21);//重新设置圆心坐标
	cout <<  c;
}

3.多态

当我们需要再派生一个圆柱类时,我们就需要计算圆柱的底面(base area)与侧面(side area),此时我们在Cylinder类中定义一个在Circle类中同名的函数:Area()const,用于计算圆柱体的侧面积,这两个函数的关系不同于重载,他们的函数名,函数参数都相同,只有函数体不同,因此我们在函数调用前需要加上我们要调用的函数所在的类名和域运算符(::),这样就确定了我们要调用的是哪个类内的函数啦~

#include<iostream>
#include<iomanip>//输出格式控制头文件
using namespace std;
const double pi = 3.14159;//全局变量圆周率

class Point {//基类点类
protected:
	float x, y;
public:
	Point(float x = 0, float y = 0) {//默认构造函数
		this->x = x;
		this->y = y;
	}
	void setPoint(float x, float y) {//重新设置某点坐标值
		this->x = x;
		this->y = y;
	}
	float getX()const { return x; };//读取x坐标
	float getY()const { return y; };//读取y坐标
	friend 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):Point(x,y),radius(r) {	}//构造函数
	void setRadius(float r) {
		this->radius = r;
	}
	float getRadius() const {
		return radius;
	}
	float Area() const{
		//需要注意这个函数需要定义为常函数,一方面防止数据内篡改,一方面友元重载函数调用时要求函数参数为常对象
		return pi * radius * radius;
	}
	friend ostream& operator<<(ostream& output, const Circle&c) {//运算符重载(只能以友元函数方式重载)
		output << "Center: (" << c.x << "," << c.y << "), ",
		output << "r=" << c.radius,
		output << ", area=" << c.Area() << endl;
		return output;
	}
};
class Cylinder :public Circle {//公有方式派生类圆柱类
protected:
	float height;//高
public:
	Cylinder(float x = 0, float y = 0, float r = 0, float h = 0) :Circle(x, y, r), height(h) {	};
	void setHeight(float h=0) {
		this->height = h;
	}
	float getHeight()const {
		return height;
	}
	float Area() const{
		return 2 * pi * radius * height;
	}
	float Volume()const {
		return 2 * Circle::Area() + Cylinder::Area();//此处Cylinder可省略,运行结果同
	}
	friend ostream& operator<<(ostream& output, const Cylinder& c) {//运算符重载(只能以友元函数方式重载)
		output << "Center: (" << c.x << "," << c.y << "), ",
		output << "r=" << c.radius,
		output << ", base area=" << c.Circle::Area(),
		output << ", height=" << c.height,
		output << ", side area=" << c.Cylinder::Area() << endl;
		return output;
	}
};

int main() {
	Cylinder c(11.14, 11.20, 5.20, 13.14);
	cout << c;
}

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜜汁博哥

我是大学生,给钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值