当前位置: 首页 > news >正文

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;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • C数据结构:排序
  • Mock数据
  • 第2章 Rust初体验5/8:match表达式和模式匹配:更富表达力:猜骰子冷热游戏
  • 181.二叉树:验证二叉树(力扣)
  • STM32CUBEIDE使用技巧
  • docker——基础知识
  • 08_第八章 微头条项目开发
  • Spring系统学习 - Bean的作用域
  • 震坤行坤合供应链荣获“2024 LOG低碳供应链物流-最具影响力品牌商”
  • 快捷键专栏 IDEA、Navicat、电脑、Excle、Word等
  • SpringCash
  • Java--数组小结
  • 【Spine学习06】之IK约束绑定,制作人物待机动画,图表塞贝尔曲线优化动作
  • Java之等待唤醒方法
  • 如何成为一名黑客?小白必学的12个基本步骤
  • 230. Kth Smallest Element in a BST
  • 4. 路由到控制器 - Laravel从零开始教程
  • github从入门到放弃(1)
  • input实现文字超出省略号功能
  • JSDuck 与 AngularJS 融合技巧
  • laravel 用artisan创建自己的模板
  • Map集合、散列表、红黑树介绍
  • QQ浏览器x5内核的兼容性问题
  • Selenium实战教程系列(二)---元素定位
  • SpringCloud集成分布式事务LCN (一)
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • Yii源码解读-服务定位器(Service Locator)
  • 深度学习中的信息论知识详解
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • 带你开发类似Pokemon Go的AR游戏
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 函数计算新功能-----支持C#函数
  • ​TypeScript都不会用,也敢说会前端?
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • ## 1.3.Git命令
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (9)目标检测_SSD的原理
  • (bean配置类的注解开发)学习Spring的第十三天
  • (ibm)Java 语言的 XPath API
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (剑指Offer)面试题34:丑数
  • (亲测有效)推荐2024最新的免费漫画软件app,无广告,聚合全网资源!
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (三)模仿学习-Action数据的模仿
  • (四)库存超卖案例实战——优化redis分布式锁
  • (译)2019年前端性能优化清单 — 下篇
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)我也是一只IT小小鸟
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .net开发日常笔记(持续更新)
  • .NET微信公众号开发-2.0创建自定义菜单