当前位置:网站首页>Classes and objects -- polymorphic
Classes and objects -- polymorphic
2022-06-13 09:26:00 【Hezeze】
.1 The basic concept of polymorphism
- Static polymorphism : Function overloading and operator overloading . Function address binding – The compile phase determines the function address
- Dynamic polymorphism : Derived class and Virtual functions Implement runtime polymorphism . Function address late binding – The runtime determines the function address
Dynamic polymorphism satisfies the condition :
1. There is an inheritance relationship
2. Subclasses override virtual functions in the parent class
3. Pointer or reference to the parent class , Objects that execute subclasses
class Animal {
public:
virtual void speak() {
// Virtual functions
cout << " Animals talk " << endl;
}
};
class Dog :public Animal{
// Inherit
public :
void speak(){
cout << " Dogs talk " << endl;
}
};
// Pointer or reference to the parent class , Objects that execute subclasses ,
void dospeak(Animal &animal) {
animal.speak();
}
void test() {
Dog p;
dospeak(p); // Want to execute Dog Of speak function , The function address needs to be determined at run time
}
internals :
When a subclass overrides a parent class function , It will overwrite the virtual function table of the parent class with the virtual function table of the child class , Thus, dynamic polymorphic calls
Pointer implements polymorphism :
class Calculator {
public :
virtual int getre() {
return 0;
}
int m_num1;
int m_num2;
};
class add : public Calculator {
public:
int getre() {
return m_num1 + m_num2;
}
};
class sub : public Calculator {
public:
int getre() {
return m_num1 - m_num2;
}
};
void test() {
Calculator* abs = new add; // The pointer of the parent class points to the object of the child class
cout << abs->getre() << endl;
delete abs;
}
.2 Pure virtual functions and abstract classes
Because in polymorphism , The implementation of virtual functions in the parent class is meaningless , You can change to Pure virtual function virtual Return type Function name ( Parameters ) = 0;
At the same time, the parent class is called abstract class
Abstract class features :
- Cannot instantiate object
- Subclasses of abstract classes must override pure virtual functions in the parent class , Otherwise, it belongs to the abstract class
example :
class Abs {
public :
virtual void step1() = 0;
virtual void step2() = 0;
void getresult() {
step1();
step2();
}
};
class Son1 :public Abs {
public:
void step1() {
cout << "Son1 Take the first step " << endl;
}
void step2() {
cout << "Son1 Go to step two " << endl;
}
};
void test() {
Abs* p = new Son1;
p->getresult();
delete p;
}
.3 Virtual deconstruction and pure virtual deconstruction
When using polymorphism , If there are attributes in the subclass that go to the heap , Subclass destructor code cannot be called when a parent class pointer is released
class Abs {
public :
Abs() {
cout << "Abs Constructor call " << endl;
}
~Abs() {
cout << "Abs Destructor call " << endl;
}
};
class Son :public Abs {
public:
Son(string name) {
cout << "son Constructor call " << endl;
m_name = new string(name);
}
~Son() {
cout << "son Destructor call " << endl;
if (m_name != NULL) {
delete m_name;
m_name = NULL;
}
}
string* m_name;
};
void test() {
Abs* p = new Son("zsd");
delete p;
}
Subclass heap data not released , There are two ways to solve :
class Abs {
public :
Abs() {
cout << "Abs Constructor call " << endl;
}
// Virtual destructors only need to declare
virtual ~Abs() = 0 ;
};
//2
class Abs {
public :
Abs() {
cout << "Abs Constructor call " << endl;
}
// Pure virtual deconstruction
virtual ~Abs() = 0 ;
};
Abs::~Abs() {
// Need to achieve
}
summary :
1. Virtual or pure virtual destructors are used to solve the problem of releasing subclass objects through parent class pointers
2. If there is no heap data in the subclass , Don't write
3. Classes with pure virtual destructors also belong to abstract classes
边栏推荐
- (topological sorting +bfs) acwing 848 Topological sequence of digraph
- LeetCode 1143. Longest common subsequence
- Longadder of the source code of JUC atomic accumulator
- 批量读取文件夹下的全部语音文件
- Remember! Don't be too confident in writing code! Be sure to write some key log info output, or the problem will not be located.
- Instruction level parallelism (?)
- LeetCode 583. 两个字符串的删除操作
- 批量讀取文件夾下的全部語音文件
- C language: deep understanding of character functions and string functions (1)
- C language: file operation
猜你喜欢
C language: five custom types
Overview of common layers of image recognition neural network (under update)
20220606 Young's inequality for Matrices
C language: deep understanding of character functions and string functions (2)
Simulink variant model and variant subsystem usage
Use typescript to complete simple snake eating function
Jenkins access openldap user authentication
How Simulink adds modules to the library browser
Final principle
C language: minesweeping
随机推荐
LeetCode 6095. Strong password checker II
Timestamp to localdate
马斯克的「元宇宙」梦
I set up a blog
(dfs) acwing 842. Arrange numbers
Sort() sort function
Yolov5 face learning notes
Jenkins接入Openldap用户认证
Necessary and sufficient conditions for diagonalization of 20211115 matrix; The full rank matrix does not necessarily have n linearly independent eigenvectors; Symmetric matrices must be diagonalized
【最全面详细解释】背包问题详解
QML(06)——qml. Add a new folder under QRC
Figure introduction to database neo4j
Z字形变换
拜登:希望尽快签署两党枪支安全改革法案
Remember! Don't be too confident in writing code! Be sure to write some key log info output, or the problem will not be located.
LeetCode 72. Edit distance
ROS2之OpenCV人脸识别foxy~galactic~humble
LeetCode 1143. Longest common subsequence
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?