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 does C++ define abstract classes as interfaces in the class architecture

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how C++ defines abstract classes as interfaces in the class system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ defines abstract classes as interfaces in the class system.

I.25: Prefer abstract classes as interfaces to class hierarchies (it is better to define an abstract class as an interface in a class architecture)

Translator's note:

Abstract class: the base class of the example cannot be generated directly.

Interface: an abstract class that does not contain data members.

Reason (reason)

Abstract classes are more likely to be stable than base classes with state.

Abstract classes are more likely to be more stable than base classes that contain states.

Translator's note: combined with the full text, the abstract class here should refer to the interface.

Example, bad (negative example)

You just knew that Shape would turn up somewhere: -)

All you know is that Shape will say somewhere: -)

Class Shape {/ / bad: interface class loaded with datapublic: Point center () const {return c;} virtual void draw () const; virtual void rotate (int); / /... private: Point c; vector outline; Color col;}

This will force each derived class to calculate the central location-even those special graphics that have never been used by the center. Similarly, not all graphics have colors, and it is best not to include a contour line defined by a Points sequence. Abstract classes are invented to encourage users to write the following classes:

Class Shape {/ / better: Shape is a pure interfacepublic: virtual Point center () const = 0; / / pure virtual functions virtual void draw () const = 0; virtual void rotate (int) = 0; / /... / /. No data members... / /... Virtual ~ Shape () = default;}; Enforcement (implementation recommendation)

(Simple) Warn if a pointer/reference to a class C is assigned to a pointer/reference to a base of C and the base class contains data members.

(simple) alarm if a pointer or reference to class C is copied to a pointer or reference of the base class type of C, and the base class contains data members.

Translator's note: in other words, the master's opinion is that the base class basically does not have data members for abstract classes.

At this point, I believe you have a deeper understanding of "how C++ defines abstract classes as interfaces in the class system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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