术语“多态”是“poly”+“morphs”的组合,意思是多种形式。这是一个希腊词。在面向对象编程中,我们使用 3 个主要概念:继承、封装和多态。
多态的真实例子
让我们考虑一个多态的真实例子。一位女士在课堂上表现得像老师,在家中表现得像母亲或女儿,在市场上表现得像顾客。在这里,一个人的行为会因情况而异。
C++中有两种多态:
- 编译时多态:通过匹配参数的类型和数量来调用重载函数。此信息在编译时可用,因此编译器会在编译时选择适当的函数。它是通过函数重载和运算符重载来实现的,也称为静态绑定或早期绑定。现在,让我们考虑函数名称和原型相同的情况。
class A // base class declaration.
{
int a;
public:
void display()
{
cout<< "Class A ";
}
};
class B : public A // derived class declaration.
{
int b;
public:
void display()
{
cout<<"Class B";
}
};
在上面的例子中, display() 函数的原型在基类和派生类中是相同的。因此,不能应用静态绑定。如果在运行时选择了适当的功能,那就太好了。这称为运行时多态性。
- 运行时多态性:当对象的方法在运行时而不是编译时被调用时,就实现了运行时多态性。它是通过方法覆盖实现的,也称为动态绑定或后期绑定。
b/w 编译时和运行时多态性的差异。
C++ 运行时多态性示例
让我们看一个 C++ 中运行时多态的简单示例。
// 一个没有 virtual 关键字的例子。
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
输出:
Eating bread...
C++ 运行时多态性示例:通过使用两个派生类
让我们看看 C++ 中运行时多态性的另一个示例,其中我们有两个派生类。
// 一个带有 virtual 关键字的例子。
#include <iostream>
using namespace std;
class Shape { // base class
public:
virtual void draw(){ // virtual function
cout<<"drawing..."<<endl;
}
};
class Rectangle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing rectangle..."<<endl;
}
};
class Circle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing circle..."<<endl;
}
};
int main(void) {
Shape *s; // base class pointer.
Shape sh; // base class object.
Rectangle rec;
Circle cir;
s=&sh;
s->draw();
s=&rec;
s->draw();
s=?
s->draw();
}
输出:
drawing... drawing rectangle... drawing circle...
具有数据成员的运行时多态性
运行时多态可以通过 C++ 中的数据成员来实现。让我们看一个示例,其中我们通过引用变量访问字段,该变量引用派生类的实例。
#include <iostream>
using namespace std;
class Animal { // base class declaration.
public:
string color = "Black";
};
class Dog: public Animal // inheriting Animal class.
{
public:
string color = "Grey";
};
int main(void) {
Animal d= Dog();
cout<<d.color;
}
输出:
Black