Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use class Abstract Class in abstract

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report