当前位置:网站首页>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 :
 Insert picture description here
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
 Insert picture description here
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 :

  1. Cannot instantiate object
  2. 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;
}

 Insert picture description here
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

原网站

版权声明
本文为[Hezeze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270531584081.html