当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Codeforces Round #359 (Div. 2) C. Robbers' watch 暴力枚举
【Docker】Docker安装MySQL
高德地图JS - 已知经纬度来获取街道、城市、详细地址等信息
【ROS2原理8】节点到参与者的重映射
way of thinking problem-solving skills
Use baidu EasyDL intelligent bin
用tensorflow.keras模块化搭建神经网络模型
C语言的内置宏(定义日志宏)
P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
先序遍历,中序遍历,后序遍历,层序遍历
The division principle summary within the collection
2019南昌网络赛 C题,Hello 2019
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
stm32定时器之简单封装
查看日志常用命令
MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
灵活好用的sql monitoring 脚本 part7
RK3568商显版开源鸿蒙板卡产品解决方案
TCP段重组PDU