12-C++ object-oriented (parent class pointer, subclass pointer, polymorphism, virtual function, virtual table)

parent class pointer, child class pointer

  •         The parent class pointer can point to the subclass object, which is safe and often used in development (the inheritance method must be public)
  •         It is not safe for subclass pointers to superclass objects
#include<iostream>
using namespace std;
struct Person
{
	int m_age;
};
struct Student:Person
{
	int m_score;
};
int main() {
	//父类指针指向子类对象
	Person* p = new Student();
	p->m_age = 10;
	return 0;
}

The p pointer can only operate on the m_age variable 

 polymorphism

        By default, the compiler will only call the corresponding function according to the pointer type, and there is no polymorphism

   Polymorphism is a very important feature of object-oriented:

  •         The same operation acts on different objects, can have different interpretations, and produce different execution results
  •         At runtime, the real object type can be identified and the function in the corresponding subclass can be called

   Polymorphic elements:

  •         The subclass overrides the member function of the parent class (override)
  •         The parent class pointer points to the child class object
  •         Use the parent class pointer to call the rewritten member function

virtual function

  •         Polymorphism in C++ is implemented through virtual functions (virtual function)
  •         Virtual function: a member function modified by virtual
  •         As long as it is declared as a virtual function in the parent class, the function rewritten in the subclass will automatically become a virtual function (that is, the virtual keyword can be omitted in the subclass)
#include<iostream>
using namespace std;
//struct Person
//{
//	int m_age;
//};
//struct Student:Person
//{
//	int m_score;
//};
struct Animal
{
	virtual void run() {
		cout << "Animal::run()" << endl;
	}
	void speak() {
		cout << "Animal::speak()" << endl;
	}
};
struct Dog:Animal
{
	void run() {
		cout << "Dog::run()" << endl;
	}
	void speak() {
		cout << "Dog::speak()" << endl;
	}
};
struct Cat :Animal
{
	void run() {
		cout << "Cat::run()" << endl;
	}
	void speak() {
		cout << "Cat::speak()" << endl;
	}
};
struct Pig :Animal
{
	void run() {
		cout << "Pig::run()" << endl;
	}
	void speak() {
		cout << "Pig::speak()" << endl;
	}
};
void liu(Animal *p) {
	p->speak();
	p->run();
}
int main() {
	//父类指针指向子类对象
	/*Person* p = new Student();
	p->m_age = 10;*/
	liu(new Dog());
	liu(new Cat());
	liu(new Pig());
	return 0;
}

virtual table

        The realization principle of a virtual function is a virtual table, which stores the address of the virtual function that needs to be called eventually, and this virtual table is also called a virtual function table

        All Cat objects (whether in the global area, stack, or heap) share the same virtual table

#include<iostream>
using namespace std;
struct Animal
{
	int m_age;
	virtual void speak() {
		cout << "Animal::speak()" << endl;
	}
	virtual void run() {
		cout << "Animal::run()" << endl;
	}
};
struct Cat:Animal
{
	int m_life;
	void speak() {
		cout << "Cat::speak()" << endl;
	}
	void run() {
		cout << "Cat::run()" << endl;
	}
};

int main() {
	cout << sizeof(Cat) << endl;
	return 0;
}

Call the member function implementation of the parent class     

struct Animal
{
	virtual void speak() {
		cout << "Animal::speak()" << endl;
	}
};
struct Cat:Animal
{
	void speak() {
        Animal::speak();
		cout << "Cat::speak()" << endl;
	}
	
};

Guess you like

Origin blog.csdn.net/qq_56728342/article/details/129717172