多态性
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;
}