当前位置:网站首页>Classes and objects (1)
Classes and objects (1)
2022-06-11 21:44:00 【Code loving students】
Catalog
1. C and C++ What's the main difference between ?
Two methods of defining classes
4. Class access qualifier and encapsulation
7.1 Find the size of the class
1. C and C++ What's the main difference between ?
C Language is Process oriented Of , It's about The process , Analyze the steps to solve the problem , Solve the problem step by step through the function call . C++ Is based on object-oriented Of , Focus on the object , Split a thing into different objects , It depends on the interaction between objects .
2. class
stay C In language struct Only variables can be defined , And in the C++ in struct You can define variables , You can also define functions .

And in the C++ I prefer to use class Instead of struct.
3. The definition of a class
class Student {
// The class body : Constructed by member variables and member functions
};class yes Class keyword ,student yes Class name ,{ } Inside is The class body , Note that you should add a semicolon after defining the class .
The elements in a class are called members of the class : The data in the class is called Attributes of a class perhaps Member variables ; The functions in the class are called Class method perhaps Member functions .
Two methods of defining classes
1. The declaration and definition are in the class
If you define a function in a class , It is generally considered to be an inline function .
struct Student
{
void SetStudentInfo(const char* name, const char* gender, int age)
{
strcpy(_name, name);
strcpy(_gender, gender);
_age = age;
}
void PrintStudentInfo()
{
cout << _name << " " << _gender << " " << _age << endl;
}
char _name[20];
char _gender[3];
int _age;
};2. The statement in .h In file , It's defined in .cpp In file
Generally speaking, the definition is this format , But the scope of the function defined in the class is in the class , When defining outside a class, it should be explained that it is the function in the class , So we need to add Class name ::

In general, we recommend the second method .
4. Class access qualifier and encapsulation
4.1 Access qualifier
stay C++ There are three kinds of access qualifiers, namely :1. public 2. protected 3. private, Being modified by different qualifiers has different effects :
1. public Decorated members can be accessed directly outside the class
2. protected and private Decorated members cannot be directly accessed outside the class ( here protected and private It's similar )
3. The access scope starts from where the access qualifier appears until the next access qualifier appears

If only public A qualifier , From : Until } All are public Embellished

We can see that variables can be used outside the class , Because he is public modification Of
When the member variable is added with private when , It means that the following are private Embellished , Naturally, it is not accessible outside the class .
4. class The default access rights of are private,struct by public( because struct To be compatible with C)
At the beginning , adopt struct Class... Is introduced , that class and struct What's the difference between ?
because C++ Is based on C Generated so C++ Compatibility required C Language , therefore C++ in struct It can be used as a structure . in addition C++ in struct It can also be used to define classes . and class Yes, the definition class is the same , The difference is that struct The default access method for members is public,class Yes, the default access method for members is private.
4.2 encapsulation
There are three characteristics of object-oriented : encapsulation 、 Inherit 、 polymorphic , These three are also the essence of object-oriented .
What is encapsulation ?
encapsulation : Organically combine data and methods of operating data , Hide object properties and implementation details , Only expose interfaces to interact with objects , Encapsulation can help us enhance the robustness of our code , There is no need to consider the technical level of the person using the interface .
Encapsulation is a kind of management , Compare a class to a museum ,public The decoration is what tourists can watch ,private What is embellished is what cannot be seen , We take out what we want others to do , Hide what you don't want others to see or modify .
5. Scope of class
stay C perhaps C++ Variables or functions defined in are found from the bottom up , Do classes also follow this rule ?
I found that if you follow the above rules , that Print The function cannot access the following three variables , But in the class, you can access , The variables and functions defined in the class can be used anywhere in the class .
6. Class instantiation
When creating objects using class types , It is called instantiation of a class .
1. Class is just a model thing , Defines which members of the class , Defining a class does not allocate the actual memory space to store it .
2. A class can instantiate multiple objects , The instantiated object will use the actual physical space , Store class member variables .
3. Class is an architectural drawing , Creating objects is equivalent to implementing architecture , If you don't create objects , That kind is a model .
7. Class size
7.1 Find the size of the class
A class contains functions and variables , What does the created object contain ?
1. Object contains functions and variables
2. Object contains only variables , Functions are contained in common code snippets
First, let's review the calculation of the size of the structure :
1. The first member is offset from the structure by 0 The address of .
2. Other member variables are aligned to a number ( Align numbers ) An integral multiple of the address of .
Be careful : Align numbers = The compiler defaults to an alignment number and the smaller value of the member size . VS The default alignment number in is 8
3. The total size of the structure is : Maximum number of alignments ( The maximum of all variable types and the minimum of default alignment parameters ) Integer multiple .
4. If the structure is nested , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is All maximum alignments ( The number of alignments with nested structures ) Integer multiple .

Calculate the size of the class by calculating the structure , It is found that the same value is obtained

Through disassembly, we can also see that the function called in the object is also at the same address .
We can find that the created object should only contain variables , The function is contained in a common code segment .

We find that when there are only function members or no members in the class , The size of the class will not be 0, The compiler will give an empty class a byte to represent the class .
边栏推荐
- [v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
- 69. x的平方根
- Codeforces Round #740 Div. 2 解题报告
- JVM | introduction
- 领先企业推进智慧财务的同款效率工具,赶快了解一下?
- LeetCode-98-验证二叉搜索树
- 一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
- Codeworks round 740 Div. 2 problem solving Report
- Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?
- The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success
猜你喜欢

Leetcode-98- validate binary search tree

LeetCode-155-最小栈

LeetCode-104-二叉树的最大深度
![Analysis on the development history and market development status of China's system integration industry in 2020 [figure]](/img/3c/b53c2a3f59ff6784f128cb98a5a7a2.jpg)
Analysis on the development history and market development status of China's system integration industry in 2020 [figure]

Leetcode-43- string multiplication

LabVIEW控制Arduino实现红外测距(进阶篇—6)
![[Part 14] source code analysis and application details of completionservice class [key]](/img/41/9f5383d6eafb32723e29c15da3a1af.jpg)
[Part 14] source code analysis and application details of completionservice class [key]

LeetCode-43-字符串相乘

多态的所有特征

如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
随机推荐
RPA超自动化 | 农耕记携手云扩加速财务智能化运营
Binary search - Learning
一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
String copy function
如何查看win系统的安装日期
实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
LeetCode-98-验证二叉搜索树
网络连接正常但百度网页打不开显示无法访问此网站解决方案
BZOJ3189 : [Coci2011] Slika
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Codeforces Round #740 Div. 2 解题报告
多态的所有特征
Leetcode - 第2天
Leetcode-155-minimum stack
Redis transaction
238.除自身以外数组的乘积
How does the chief financial officer of RPA find the "super entrance" of digital transformation?
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称