laitimes

Korean Sharing (46): In-depth C++ (13) - Member Variables

Korean Sharing (46): In-depth C++ (13) - Member Variables

Share interests, spread happiness, increase knowledge, and leave a good stay!

Dear you, this is LearningYard.

Today, Xiaobian brings you "In-depth C++ (13) - Member Variables", welcome to your visit.

Share interest, spread happiness, increase knowledge, and leave beautiful.

Dear, this is the LearingYard Academy!

Today, Dive into C++ (13) - Member variables ,welcome to visit!

Korean Sharing (46): In-depth C++ (13) - Member Variables

1. The class object is a member of the class

1. Class objects are class members

The class object is a member of the class. For example, a student class has a mobile phone, and this mobile phone is the object of the mobile phone class.

Class objects serve as class members. For example, a student class has a phone, and that phone is an object of the phone class.

The phone has a phone brand, and it also needs a constructor that assigns a value to the phone brand.

Phones have a brand, and we need a constructor that assigns a value to the brand.

Korean Sharing (46): In-depth C++ (13) - Member Variables

The student has a name and a mobile phone to use, and the mobile phone here uses the object of the mobile phone class as a member variable of the student class. To create a student object, you assign values to the name and phone brand.

The student has a name and a cell phone, and the cell phone is the object of the cell phone class as a member variable of the student class. To create a student object, assign values to your name and phone brand.

Korean Sharing (46): In-depth C++ (13) - Member Variables

Created by student Zhang San, the mobile phone used is Xiaomi. If the output is correct, the student object has been created.

Create student Zhang SAN, the use of mobile phone for Xiaomi. The correct output indicates that the student object was created successfully.

Korean Sharing (46): In-depth C++ (13) - Member Variables

2. Static members

2. Static members

Static members, i.e., member variables that are statically modified. The member variable belongs to the entire class, and all objects in that class share data. For example, water in a well can be used as a static member variable.

A static member is a member variable that is static. The member variable belongs to the entire class, and all objects of that class share data. For example, the water in a well can be used as a static member variable.

For static members, in-class declaration, out-of-class initialization. Initialization does not require static modifications, but indicates which class to make under static members. If there is a well in the village, there are 100 liters of water in the well. When the well water is initialized, a village is added in front of it. Indicates the static members under the class when well water.

For static members, they are declared inside the class and initialized outside the class. The initialization does not need static, but it does indicate which class to make a static member of. If the village has a well, there are 100 liters of water in the well. When we initialize the well, we add Village in front of it. Indicates the static member under this class when well water is used.

Korean Sharing (46): In-depth C++ (13) - Member Variables

Village 1 and Village 2 are empty with a well. Whether it is village 1 or village 2 fetching water, the water in this well will be reduced.

Village 1 and Village 2 use a single well. Whether village 1 draws water or village 2 draws water, it will reduce the water of this well.

Korean Sharing (46): In-depth C++ (13) - Member Variables

So whether it is Village 1 or Village 2, the well water is 80 liters. Similarly, the well water that passes through the entire class is 80 liters.

So whether it's village 1 or village 2, the well water is 80 liters. Similarly, the well water that passes through the entire class is also 80 liters.

Korean Sharing (46): In-depth C++ (13) - Member Variables

3. Storage of Members

3. Storage of members

Different member variables are stored differently. Normal and static member variables are stored separately.

Member variables are stored differently. Regular and static member variables are stored separately.

An object of an empty class that occupies 1 byte instead of 0. The reason for this is to separate the storage space from other objects and avoid multiple objects being stored in the same storage space.

An object of an empty class that occupies one byte instead of zero. The reason is to keep the storage space separate from other objects and avoid having multiple objects in the same storage space.

Korean Sharing (46): In-depth C++ (13) - Member Variables

There are common member variables in the class, such as integers and strings. When you create an object of this class, the compiler makes room for all the normal members of the object.

Ordinary member variables exist in classes, such as integers and strings. When an object of this class is created, the compiler makes space for all regular members of the object.

Korean Sharing (46): In-depth C++ (13) - Member Variables

Then add normal member variables to the class, and the more space the object will occupy. Description: Common member variables are stored in objects.

When you add regular member variables to the class, the object takes up more space. Indicates that regular member variables are stored in an object.

Korean Sharing (46): In-depth C++ (13) - Member Variables
Korean Sharing (46): In-depth C++ (13) - Member Variables

However, by adding a static member variable to the class, the space occupied by the object remains the same. Indicates that static member variables are not stored in an object. As we have learned, static member variables should be stored in the global area.

But when a static member variable is added to a class, the object takes up the same space. Indicates that static member variables are not stored in an object. As you learned earlier, static member variables should be stored in the global area.

Korean Sharing (46): In-depth C++ (13) - Member Variables

That's all for today's sharing,

If you have a unique idea for the article,

Feel free to leave us a message.

Let's meet tomorrow,

Have a great day!

That's all for today's sharing.

If you have a unique idea about the article,

please leave us a message,

and let us meet tomorrow.

I wish you a nice day!

Translation: NetEase Youdao Translation

This article is organized and issued by LearningYard, if there is any infringement, please leave a message in the background to communicate.

文案|Dongyang

Dongyang

Audit|hong

Learning Yard 新学苑