How to use class Abstract Class in abstract
It is believed that many inexperienced people have no idea about how to use class abstract class in abstract. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Definition of Abstract Class
First of all, the virtual function.
Virtual functions are member functions declared as virtual in the base class and redefined in the derived class, which can realize the dynamic overloading of the function.
Dynamic overloading means that the specific implementation function can be selected according to the type of the class passed in later.
The declaration of a pure virtual function has a special syntax format: virtual returns the value type member function name (parameter table) = 0
Including virtual functions is a necessary and sufficient condition for becoming an abstract class.
Classes that contain virtual functions are called abstract classes, and abstract classes can have pure virtual functions or other non-virtual functions; because abstract classes are all undefined virtual functions, objects cannot be defined.
In C++, we can set an abstract class (Abstract Class) that can only be used for inheritance and cannot directly create objects.
Give me a little chestnut.
For example, today, I want to write a code that can automatically help me stir-fry meat dishes. The effect I need to achieve is: if chicken is introduced, Kung Pao chicken is made; if ribs are introduced, potato stewed ribs are made; if beef is imported, it is made into black pepper beef ribs; only one dish at a time
Because you can't predict the type of meat that may be passed in when you use it, you can't create all three objects? At this time, you can create abstract meat; as well as derived chicken, ribs, and beef
All three classes need to have corresponding cooking functions, such as cook ()
# includeusing namespace std;class Meat / / Abstract class: meat {public: virtual void cook () = 0 public / pure virtual function}; class Chicken:public Meat / / derived class: chicken {public: void cook () {cout