多态的概念
多态分为两类 静态多态:函数重载和运算符重载属于静态多态,复用函数名 动态多态:派生类和虚函数实现运行时多态 静态多态和动态多态的区别 静态函数的函数地址早绑定——编译阶段确定函数地址 动态函数的函数地址晚绑定——运行阶段确认函数地址 下面通过案例进行讲解多态
地址早绑定
#include<iostream>
using namespace std;
class animal
{
public:
void speak()
{
cout << "动物在说话" << endl;
}
};
class cat :public animal
{
public:
void dospeak()
{
cout << "小猫在说话" << endl;
}
};
void speak(animal& a)
{
a.speak();
}
int main()
{
cat b;
speak(b);
}
地址晚绑定
使用关键字virtual,使父类函数变为虚函数
#include<iostream>
using namespace std;
class animal
{
public:
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
class cat :public animal
{
public:
void speak()
{
cout << "小猫在说话" << endl;
}
};
void speak(animal& a)
{
a.speak();
}
int main()
{
cat b;
speak(b);
}
总结: 动态多态满足条件
- 1.有继承关系
- 2.子类重写父类的虚函数
动态多态的使用 - 1.父类的指针或者引用,指向子类对象
重写:函数返回类型,函数名,参数列表要与父类完全一致
原理剖析
当子类出些重写时,在虚函数表内子类重写函数的地址会将父类的虚函数覆盖
class animal
{
public:
void speak()
{
cout << "动物在说话" << endl;
}
};
像这样的空类,计算其内存大小,大小为一个字节
class animal
{
public:
virtual void speak()
{
cout << "动物在说话" << endl;
}
};
产生虚函数时,他的大小将变为4个字节,原因是当使用关键字virtual时,会产生一个虚函数表,里面存放的的时函数的地址,地址的大小为4个字节。
多态的优点以及实例
- 代码组织结构清晰
- 可读性强
- 利于前期和后期的扩展以及维护
实例:
#include<iostream>
using namespace std;
class Calculator
{
public:
int m_num1;
int m_num2;
virtual int getResult()
{
return 0;
}
};
class Add :public Calculator
{
public:
int getResult()
{
return m_num1 + m_num2;
}
};
class Sub :public Calculator
{
public:
int getResult()
{
return m_num1 - m_num2;
}
};
class Mul :public Calculator
{
public:
int getResult()
{
return m_num1 * m_num2;
}
};
int main()
{
Calculator* abc;
abc = new Add;
abc->m_num1 = 100;
abc->m_num2 = 100;
cout << abc->m_num1 << " + " << abc->m_num2 << " = " << abc->getResult() << endl;
delete abc;
abc = new Sub;
abc->m_num1 = 100;
abc->m_num2 = 100;
cout << abc->m_num1 << " - " << abc->m_num2 << " = " << abc->getResult() << endl;
delete abc;
abc = new Mul;
abc->m_num1 = 100;
abc->m_num2 = 100;
cout << abc->m_num1 << " * " << abc->m_num2 << " = " << abc->getResult() << endl;
delete abc;
return 0;
}
|