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

Why C++ uses class instead of struct to define classes

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

Share

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

This article mainly explains "why C++ uses class instead of struct definition class", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "why C++ uses class instead of struct definition class"!

C.8: Use class instead of struct to define classes when there are non-public members

Reason

Readability. To make it clear that something is being hidden/abstracted. This is a useful convention.

Readability. It is clear that something is hidden or abstract. This is a useful practice.

Example, bad

struct Date { int d, m;

Date(int i, Month m); // ... lots of functions ... private: int y; // year};

If you only consider the rules of the C++ language, there are no errors in this code. But from a design point of view, almost everything is wrong. Private data is also hidden away from public data. The data is scattered into different parts of the class declaration. Access attributes vary for different parts of the data. All of this reduces readability and increases maintenance complexity.

Note:

Prefer to place the interface first in a class, see NL.16.

It is best to place interfaces at the beginning of a class

Enforcement

Flag classes declared with struct if there is a private or protected member.

Prompt if a class declared with the struct keyword has private or protected members.

At this point, I believe that everyone has a deeper understanding of "why C++ uses class instead of struct definition class", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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