Introduction to polymorphism
- Polymorphic basis
- New object-oriented
- Polymorphism solution provided by C + + compiler
- There are three conditions for polymorphism meaning and its establishment
- Theoretical basis of polymorphism
- Multiple interview questions reinforcement
- Understanding of polymorphism
- How to realize polymorphism in C + + compiler
- Overload override redefine
- virtual destructor
- Can ordinary member functions of each class be defined as virtual functions
- Can virtual function be called in constructor to realize polymorphism?
- Virtual function table pointer vptr pointer step-by-step initialization
- Two different concepts of the step length of the parent pointer and the child pointer and the parent pointer pointing to the child object
- On the principle of polymorphism
- Summary memory 1: C + + compiler layout in advance, adding vptr pointer and virtual function entry address to class object to store virtual function table
- Summing up memory 2: C + + compiler is not to block the numerator class object and the parent class object, but to find the vptr pointer according to the object pointer, and then find the virtual function entry, to achieve late binding and support polymorphism
- There are three necessary conditions for polymorphism
- There should be inheritance.
- There must be a virtual function override.
- A parent class pointer or reference points to a child class object.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//yue buqun
class Yuebuqun
{
public:
Yuebuqun( string kongfu)
{
this->kongfu = kongfu;
}
virtual void fight() //Identity decorating a member method is a virtual function.
{
cout << "yue buqun" << "I did" << kongfu << "Strike" << endl;
}
void print()
{
}
string kongfu;
};
//Lin Pingzhi inherits Yue buqun
class Linpingzhi :public Yuebuqun
{
public:
Linpingzhi(string kongfu) :Yuebuqun(kongfu)
{
}
//If there is a virtual function in the parent class that is fit (), the child class will rewrite the virtual function.
void fight()
{
cout << "Lin Pingzhi" << "I did" << kongfu << "Strike" << endl;
}
void print()
{
}
};
class Linghuchong :public Yuebuqun
{
public:
Linghuchong(string kongfu) :Yuebuqun(kongfu)
{
}
void fight()
{
cout << "Linghu Chong " << "Used" << kongfu << endl;
}
};
//Provide a way to fight globally
void fightPeople(Yuebuqun *hero)//Yuebuqun *hero = xiaopp; Yuebuqun *hero = xiaoyy;
{
cout << "Call the hit method" << endl;
hero->fight();//If you want to pass in a subclass, call the fight of the subclass
//If the passed in is a parent, call the parent's fit
//This behavior is called polymorphism.
}
int main(void)
{
Yuebuqun *xiaoyy = new Yuebuqun("Sunflower collection");
//xiaoyy->fight();
Linpingzhi *xiaopp = new Linpingzhi("Secluded evil sword manual");
//xiaopp->fight();
Linghuchong *xiaoll = new Linghuchong("Dugu nine Swords");
fightPeople(xiaoyy);
fightPeople(xiaopp);
fightPeople(xiaoll);
//The compiler does a safe handling by default. The compiler considers that no matter whether the subclass object or the parent object is passed,
//If the parent class d method is executed uniformly, it can be executed successfully.
delete xiaoyy;
delete xiaopp;
delete xiaoll;
return 0;
}
Polymorphic cases and their significance
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//Heroes
//1999
class Hero
{
public:
virtual int getAd() {
return 10;
}
};
//1999
class AdvHero :public Hero
{
public:
virtual int getAd()
{
return 1001;
}
};
//Monsters
//1999
class Monster
{
public:
int getAd() {
return 1000;
}
};
//Methods of combat
//Architecture function written in 1999 / / can call the future.
void playerFight(Hero *hp, Monster *mp)
{
//Polymorphism is also a dynamic binding and a late binding for compilers.
if (hp->getAd() > mp->getAd()) { //HP - > getad polymorphism
cout << "Heroes win, monsters are killed" << endl;
}
else {
cout << "The hero's gone. The monster won" << endl;
}
}
//2020
class BugHero :public Hero
{
public:
virtual int getAd()
{
cout << "Called bugHero Method" << endl;
return 66666;
}
};
int main(void)
{
Hero h;
Monster m;
playerFight(&h, &m);
AdvHero advH;
playerFight(&advH, &m);
BugHero bH;
playerFight(&bH, &m);
int a = 10;
int b = 20;
cout << a << endl;
if (a > 10) { //Chi Bangding
cout << "a>10" << endl;
}
else {
cout << "a < 10" << endl;
}
return 0;
}