分享興趣,傳播快樂,增長見聞,留下美好!
親愛的您,這裡是LearningYard新學苑。
今天小編為大家帶來“深入C++(十三)——成員變量”,歡迎您的通路。
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!
一、類對象作類成員
1. Class objects are class members
類對象作為類成員。例如一個學生類有一部手機,而這個手機就是手機類的對象。
Class objects serve as class members. For example, a student class has a phone, and that phone is an object of the phone class.
手機有手機品牌,還需要一個給手機品牌指派的構造函數。
Phones have a brand, and we need a constructor that assigns a value to the 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.
建立學生張三,使用的手機為小米。輸出正确,說明這個學生對象已經建立成功。
Create student Zhang SAN, the use of mobile phone for Xiaomi. The correct output indicates that the student object was created successfully.
二、靜态成員
2. Static members
靜态成員,即被static修飾的成員變量。該成員變量屬于整個類,該類的所有對象共享資料。比如井裡的水就可以作為靜态成員變量。
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.
對于靜态成員來說,類内聲明,類外初始化。初始化不需要static修飾,但要标明使哪個類下的靜态成員。如村有井,井裡有100升水。井水初始化時,在前面加上了Village。表明井水時該類下的靜态成員。
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.
村1和村2空用一口井。無論是村1打水還是村2打水,都會使這口井的水減少。
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.
是以無論是村1還是村2,井水都是80升。同樣,通過整個類的井水也是80升。
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.
三、成員的存儲
3. Storage of members
不同成員變量的存儲不同。普通成員變量和靜态成員變量分開存儲。
Member variables are stored differently. Regular and static member variables are stored separately.
一個空類的對象,占1個位元組,而不是0個。原因是為了在存儲空間上與其他對象分開,避免多個對象存放在同一存儲空間。
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.
類中存在普通的成員變量,如整型、字元串型。在建立該類的對象時,編譯器會為該對象中所有的普通成員開辟空間。
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.
再在類中添加普通成員變量,對象所占空間越多。說明普通成員變量是存放在對象中的。
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.
但在類中添加靜态成員變量,對象所占空間不變。說明靜态成員變量不是存放在對象中的。有前面所學得知,靜态成員變量應該存放在全局區。
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.
今天的分享就到這裡了,
如果您對文章有獨特的想法,
歡迎給我們留言。
讓我們相約明天,
祝您今天過得開心快樂!
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!
翻譯:網易有道翻譯
本文由LearningYard學苑整理并發出,如有侵權請背景留言溝通.
文案|Dongyang
排版|Dongyang
稽核|hong
Learning Yard 新學苑