C++ Virtual Functions and Polymorphisms
Virtual Functions and Virtual Function Tables
Normal functions do not affect class size
Virtual function table: Because this pointer points to all virtual function header addresses to store memory segments, it is called a table
- What is a virtual function? A member function modified with virtual is called a virtual function
- The Effect of Virtual Functions on Classes
- Add a pointer's memory, 32-bit 4 bytes, 64-bit 8 bytes
- Virtual function table (see): is the first address where a pointer stores all virtual functions
+
#include <iostream> using namespace std; class GG { public: virtual void print1() { cout << "Virtual Function 1" << endl; } virtual void print2() { cout << "Virtual Function 2" << endl; } virtual void print3(); protected: }; void GG::print3() { cout << "Virtual Function 3" << endl; } class TEST { int age; }; void testVirtual() { //C language does not allow empty structures cout << sizeof(GG) << endl; //Empty class or structure takes up 1 byte cout << sizeof(TEST) << endl; GG gg; //By Virtual Function Table Calls Virtual Function // You don't have to make it more difficult for yourself int** vptr = (int**)≫ typedef void(*PF)(); PF func = (PF)vptr[0][0]; func(); //Call the first virtual function func = (PF)vptr[0][1]; func(); //Call the second virtual function } int main() { testVirtual(); return 0; }
Virtual Functions and Polymorphisms
- Polymorphic Definition: Different results from the same behavior (call)
Different results because of different objects
Crash: Men and women use the toilet differently depending on who they are - The Necessity Principle of Polymorphism
- Must have virtual function in parent class
- Subclasses must inherit from public
- A reference to the pointer must exist (use)
#include <iostream> using namespace std; class Man { public: void WC1() { cout << "General function: men go to the toilet" << endl; } virtual void WC2() //Parent must have virtual { cout << "Virtual function: Dirty men go to the toilet" << endl; } protected: }; class Woman :public Man { public: void WC1() // General function { cout << "General function: women go to the toilet" << endl; } void WC2()// General function { cout << "General function: women go to the toilet" << endl; } protected: }; void testVirtual() { //No polymorphism in normal access cout << "Normal access, proximity principle" << endl; Man man; man.WC1(); man.WC2(); cout << "Pointer abnormal assignment:Subclass object initializes parent pointer" << endl; Man* parent = new Woman; //There is virtual viewing object type, no virutal viewing pointer parent->WC1(); //Not a virtual function parent->WC2(); //Is a virtual function parent = new Man; //fangwen man parent->WC2(); } int main() { testVirtual(); return 0; }
Benefits and applications: Unified interfaces are possible; Unified Behavior
For example: Unified Interface Function
void printInfo(Man* parent) { parent->WC2(); }
Pure virtual functions and ADT
Pure virtual functions are ADT(abstract data type abstract data type) processes
-
Pure virtual functions are also virtual functions. Pure virtual functions are not functional bodies.
virutal void print()=0; //Writing functions like this in classes
-
Abstract class: A class with at least one pure virtual function, called an abstract class
- Abstract classes cannot construct objects
- Abstract classes can construct object pointers
-
Pure virtual functions are not overridden. They are pure virtual functions no matter how many times they are inherited. Virtual functions are virtual functions no matter how many times they are inherited.
#include <iostream> using namespace std; //abstract class class Parent { public: virtual void print() = 0; //Pure virtual function protected: }; void testAbstract() { //Parent object; Unable to build object cout << "testAbstract()" << endl; Parent* parent = nullptr; } //Pure virtual functions are ADT(abstract data type abstract data type) processes //Subclasses that want to create objects must override the pure virtual functions of the parent class //ADT: Forced, all subclass override functions must be identical to the parent class //Pure virtual functions are not overridden, no matter how many times they are inherited. //Virtual functions are virtual no matter how many times they are inherited class A { public: virtual void print() = 0; protected: }; class B :public A { public: void print() { cout << "B" << endl; } }; class C :public B { public: void print() { cout << "C" << endl; } }; void Abtract() { //B b; C c; //General abstract classes are overridden only once inherited B* pc = new C; pc->print(); } int main() { Abtract(); return 0; }
- General abstract classes are overridden only once inherited
- Don't rewrite to implement it His inheritance can only be pure fiction
Override in parent B, implemented in C here?
- Subclasses that want to create objects must override the pure virtual functions of the parent class
- ADT: Forced, all subclass override functions must be identical to the parent class
- <<<<< Stack I'll make up for it
virtual destructor
Reason: Abnormal assignment caused release problem --"
#include <iostream> using namespace std; class parent { public: //Virtual destructor, no virtual constructor exists virtual ~parent() { cout << "Parent Destruction" << endl; } void print() { name = "ALBANIYA"; cout << name << endl; } protected: string name = new char[10]; }; class son :public parent { public: void print() { cout << name << endl; } ~son() { cout << "Subclass Destruction" << endl; } string name = "baby"; }; int main() { //Initializing the parent pointer with a subclass object requires a virtual destructor for memory release //Virtual~parent() does not release subclasses without virtual parent* p = new son; p->print(); son s; //s.print(); // Normal Assignment Call delete p; s.print(); //Normal Assignment Call return 0; }
Summary:
Cout << "Pointer abnormal assignment: subclass object initializes parent pointer"<< endl;
Man* parent = new Woman;
//There is virtual viewing object type, no virutal viewing pointer
Parent->WC1();// Not a virtual function
Parent->WC2();// Is a virtual function
- Initializing the parent pointer with a subclass object requires a virtual destructor for memory release
- Virtual~parent() does not release subclasses without virtual
The final destructor is to free memory; Subclass objects are created on the basis of the parent class, so the parent object can only be released after the child object has been released.
************************Tips
Construction: Cover house from foundation
Destruction: Demolition starts from the roof
Say your Neptune is definitely a fisherman himself