当前位置:网站首页>Classes and Structures
Classes and Structures
2022-08-09 07:04:00 【Want to be the front end of the back end】
Classes and Structures in C++
In C++, a struct is a special kind of class.
The only difference between a structure and a class is: Structures and classes have different default access control properties.
- In the class, for members without specified access control attributes, their access control attributes are private type (private)
- In the structure, for members that do not specify any access control attributes, their access control attributes are public type (public)
In C++, the absence of structures does not affect the expressiveness of the program at all.The reason why C++ introduces structures is to maintain compatibility with C programs.
But sometimes structures are still used in C++, because structures can be used to combine different types of data into a whole, which is convenient for saving data.(If you use a class to save, because the members in the class are private by default, it is more troublesome to read and rewrite each attribute with a specific function for each data member.)
Structure in C
Structures in C language are not allowed to define function members, and there is no concept of access control attributes.
C++ introduces object-oriented features such as member functions, access control rights, inheritance, and polymorphism to structures in C language.
C++ introduces the class keyword in addition to struct, but in order to maintain compatibility with C programs, C++ retains the struct keyword and specifies that the default access control authority of the structure is a public type.
In addition, in the C language, the size of an empty structure is 0, while the size of a C++ hollow structure (belonging to an empty class) is 1.
The reason why the size of an empty class in C++ is 1:
Empty classes can also be instantiated, and each object instantiated by the class needs to have a different memory address.The address is different in the class, so an implicit byte is added to the class.
边栏推荐
猜你喜欢
随机推荐
差分约束-图论
单例 DCL(double check lock) 饱汉模式和饿汉模式
Example of using the cut command
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决
Lottie系列三 :原理分析
移远EC20 4G模块拨号相关
XxlJobConfig distributed timer task management XxlJob configuration class, replace
bzoj 5333 [Sdoi2018]荣誉称号
力扣 636. 函数的独占时间
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
Zero shift of leetcode
Neural Network Optimizer
postgresql Window Functions
TCP段重组PDU
2019南昌网络赛 C题,Hello 2019
MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
leetcode 之盛水问题
Lottie系列二:高级属性
Built-in macros in C language (define log macros)
浅识微服务架构