C++ polymorphism (2) - abstract classes and final and override keywords

Table of contents

1. Abstract class 

        1.Definition

        2.Form

3. Example:

Solution: Let the subclass rewrite the pure virtual function. After rewriting, the subclass will be transformed into a concrete class and can create objects.

3. The role of abstract classes

2.final and override keywords

        Method 1: Private parent class constructor

Method 2: Destructor of private parent class

2.1final keyword

        Method 3: Use the final keyword

The real usage of final:

2.2override keyword


1. Abstract class 

        1.Definition

       Write = 0 after the virtual function with virtual modification, then this function is expressed as a pure virtual function. A class that contains pure virtual functions is called an abstract class, also called an interface class. Abstract classes cannot instantiate objects, and subclasses cannot instantiate objects after inheriting from abstract parent classes. The solution: Only after the subclass rewrites the pure virtual function can the subclass instantiate the object. Pure virtual functions specify that subclasses must be rewritten. In addition, pure virtual functions also reflect interface inheritance.

        

        2.Form

class <class name>{

public:

        virtual <type><function name>(<parameter list>)=0; 

};

3. Example:

class Car {
public:
	virtual void Car_skill() = 0;
};

class Audi :public Car {

};

int main() {
	Car c;
	Audi a;
	return 0;
}

In the above code, a new Car class is created with a virtual function in it. The strange thing is that this virtual function is set to =0; inheriting Audi, a subclass of the Car class, and creating an object with the Car class in the main function, an error is reported. .

        The reason is that the Car class is an abstract class, because the Car_skill() function in the Car class is a pure virtual function, and the virtual function is virtual. Add =0 to the virtual function to become a pure virtual function.


        According to the concept, we can know that a class containing pure virtual functions is called an abstract class. Abstract classes cannot instantiate objects. However, the Audi class inherits the subclass Audi of the Car class. The Audi class also inherits the pure virtual functions of the parent class. Then the Audi class is also an abstract class, and it cannot instantiate objects.

Solution: Let the subclass rewrite the pure virtual function. After rewriting, the subclass will be transformed into a concrete class and can create objects.
class Car {
public:
	virtual void Car_skill() = 0;
};

class Audi :public Car {
	virtual void Car_skill() {
		cout << "加速性能好且操控感十足" << endl;
	}
};

int main() {
	//Car c;
	Audi a;		//此时子类Audi可以实例化出对象了,重写了从父类继承来的纯虚函数,那么Au'di也就不再是抽象类了
	return 0;
}

3. The role of abstract classes

         Although an abstract class cannot define objects, it can define pointer variables pointing to abstract class data. When the subclass becomes a concrete class, this pointer can be used to point to the subclass object. Then the virtual function is called through the pointer to achieve polymorphism. operation. Abstract class interface is a core concept in object-oriented programming and a necessary mechanism in various design patterns.

2.final and override keywords

Before understanding the final keyword, let's first talk about how a class can not be inherited by subclasses?

        In fact, the most important way for a parent class to prevent its subclasses from inheriting is to block the parent class's constructor. In this way, even if the subclass inherits the parent class, when the subclass creates an object, the subclass cannot call the parent class's constructor. The class cannot assign values ​​to the member variables inherited from the parent class, achieving the goal of not being inherited by the subclass.

        Method 1: Private parent class constructor

class A {
private:
	A() {
	}
};

class B :public A {

};

int main() {
	B bb;
	return 0;
}

operation result: 

Method 2: Destructor of private parent class

class A {
public:
	A(int a)
	:_a(a) {
		cout << "A的构造函数" << endl;
	}

private:
	~A() {
		cout << "A的析构函数" << endl;
	}
public:
	int _a;
};

class B :public A {
public:
	B(int a=10, int b=5)
		:A(a)
		, _b(b) {
		cout << "B的构造函数" << endl;
			}
	
public:
	int _b;
};

int main() {
	B* ptr=new B;	//B类无法创建对象
	B b;			//这样,B类就无法使用A类继承过来的成员了
	//delete ptr;

	return 0;
}

        If the destructor of the parent class is private, even if the subclass object can call the constructor of the parent class, it cannot call the destructor of the parent class. This is like the famous line in the novel: "If you have the ability to create this powerful life, But you can't master it forever, and you can't destroy it. Its existence may threaten us, may lead to the destruction of life and the destruction of the world, so its existence cannot be allowed to exist from the beginning!" The compiler won't let you do this in the first place! So this code will report a compilation error! 

 

There is a third way to prevent subclasses from inheriting from parent classes.

2.1final keyword

        Method 3: Use the final keyword
	class A final{
	private:
		A()
		{}
	};
	

	class B : public A		// 无法从 "A" 继承,因为它已声明为 "final"	
	{};
	
	int main(){
		B bb;
		B* ptr = new B;
		return 0;
	}

Use the final keyword to modify parent class A, then subclass B cannot inherit class A! Compilation error reported! 

The real usage of final:
//父类
class A {
public:
	 virtual void Sleep() final {		
		cout << "睡5个小时的觉" << endl;
	}
};

//子类
class B :public A {
	virtual  void Sleep() {
		cout << "睡5个小时的觉" << endl;
	}
};

In the above code, the virtual keyword is added to the virtual function of parent class A. After subclass B inherits class A, it means that the function Sleep() cannot be overridden by subclass B.

2.2override keyword

class A {
public:
	virtual void Sleep() {
		cout << "睡5个小时的觉" << endl;
	}
};

//子类
class B :public A {
	  void Sleep(int i) override{
		cout << "睡5个小时的觉" << endl;
	}
};

The role of the override keyword is opposite to that of final. Final does not want the subclass to override the virtual function of the parent class, while override allows the subclass to override the virtual function of the parent class. Its checking method also allows the system to check

        As shown in the code above: I wrote a virtual function of Sleep in class A. In subclass B, I want to rewrite the Sleep function and add override. However, the Sleep function I wrote is not exactly the same as the Sleep function of parent class A ( Function parameters are different), causing it to be checked out by the override keyword.

Summarize:

assert has the same purpose as final and override, they are both used to check the code.

Note: assert is during the execution of the code, and the system determines whether it can pass the check. The check reports a running error.


       As for final and override, during the process of writing code, the system will let the system judge whether it can pass the check. The check will report a compilation error:

        a. The function of final is to prevent the function of the parent class from being overridden by the subclass. If the subclass overrides the function, an error will be reported. This keyword is used to place it at the end of the function definition of the parent class.

        The function of b.override is that the parent class wants the subclass to ensure that it can override the parent class's function. If it is not rewritten, an error will be reported. This keyword is used to place at the end of a virtual function definition that is overridden by a subclass.

Guess you like

Origin blog.csdn.net/weixin_69283129/article/details/132030885