多态知识点

多态知识点

一、什么是多态

​ 多态性(polymorphism)据说最早源自希腊语,从字面上理解就是多种形态,多种形式。具体到C++这种面向对象(OOP)的语言中,其实就是“一种接口,多种实现(方法)”。
​ 多态可分为静态多态动态多态,静态多态和动态多态的区别其实只是在什么时候将函数实现和函数调用关联起来,是在编译时期还是运行时期,即函数地址是早绑定还是晚绑定的?

二、静态多态

静态多态是指在编译期间就可以确定函数的调用地址,并生产代码,这就是静态的,也就是说地址是早早绑定的,静态多态也往往被叫做静态联编。

例一:复数类的加减(成员函数)

include "pch.h"

include

using namespace std;

class Complex
{
public:
Complex (double r = 0.0, double i = 0.0) :real(r), imag(i) {}
Complex operator+ (const Complex &c2) const;
Complex operator- (const Complex &c2) const;
void display() const;
private:
double real;
double imag;
};

Complex Complex::operator+(const Complex &c2)const
{
return Complex(real + c2.real, imag + c2.imag);
}

Complex Complex::operator-(const Complex &c2)const
{
return Complex(real - c2.real, imag - c2.imag);
}

void Complex::display() const
{
cout << "(" << real << "," << imag << ")" << endl;
}

int main()
{
Complex c1(5, 4), c2(2, 10), c3;
cout << "c1="; c1.display();
cout << "c2="; c2.display();
c3 = c1 - c2;
cout << "c3=c1-c2="; c3.display();
c3 = c1 + c2;
cout << "c3=c1+c2="; c3.display();
return 0;
}

运行截图:

例二:clock类的++

include "pch.h"

include

using namespace std;

class Clock
{
public:
Clock(int hour = 0, int minute = 0, int second = 0);
void showTime() const;
Clock& operator++();
Clock operator++(int);
private:
int hour, minute, second;
};

Clock::Clock(int hour, int minute, int second)
{
if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 60)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
else
{
cout << "time error!" << endl;
}
}

void Clock::showTime ()const
{
cout << hour << ":" << minute << ":" << second << endl;
}

Clock & Clock::operator++()
{
second++;
if (second >= 60)
{
second -= 60;
minute++;
if (minute >= 60)
{
minute -= 60;
hour = (hour + 1) % 24;
}
}
return * this;
}

Clock Clock::operator++(int)
{
Clock old = this;
++(
this);
return old;
}

int main()
{
Clock myClock(23, 59, 59);
cout << "初始时间:";
myClock.showTime();
cout << "时间++:";
(myClock++).showTime();
cout << "++时间:";
(myClock++).showTime();
return 0;
}

运行截图:

三、动态多态

​ 动态多态则是指函数调用的地址不能在编译器期间确定,必须需要在运行时才确定,这就属于晚绑定,动态多态也往往被叫做动态联编。

​ 所谓的动态多态是通过继承+虚函数来实现的,只有在程序运行期间(非编译期)才能判断所引用对象的实际类型,根据其实际类型调用相应的方法。具体格式就是使用virtual关键字修饰类的成员函数时,指明该函数为虚函数,并且派生类需要重新实现该成员函数,编译器将实现动态绑定。
在上面的代码如果我们在基类的fun函数前加virtual即可实现动态绑定:
例三:

include "pch.h"

include

using namespace std;
class Base1 {
public:
virtual void display()const;//虚函数

};
void Base1::display()const {
cout << "Base1::display()" << endl;

}
class Base2 :public Base1 {
public:
virtual void display()const;

};
void Base2::display()const {
cout << "Base2::display()" << endl;

}
class Derived :public Base2 {
public:
virtual void display()const;

};
void Derived::display()const {
cout << "Derived::display()" << endl;

}
void fun(Base1* p) {
p->display();

}
int main() {
Base1 base1;
Base2 base2;
Derived derived;
fun(&base1);
fun(&base2);
fun(&derived);
return 0;

}

运行截图:

posted on 2019-10-26 17:43  yclwzx  阅读(167)  评论(0编辑  收藏  举报