[C++] Illustration of classes and objects (Part 2)

Illustrating classes and objects (Part 2)



1. Initialization list

Example: pandas is a tool based on NumPy that was created to solve data analysis tasks.
Here we refer to the initialization list! ! !


(1) Definition

Initialization list: Begins with a colon, followed by a comma-separated list of data members, each "member variable" followed by
An initial value or expression placed in parentheses.
Insert image description here


(2) Precautions

Insert image description here

Insert image description here


(3) explicit keyword

The code is as follows (example):

int main()
{
    
    
   Date d1(2022); // 直接调用构造
   Date d2 = 2022; // 隐式类型转换:构造+拷贝构造+优化--》直接调用构造函数
}

In order to avoid unnecessary implicit type conversions, C++ introduced the explicit keyword!

The code is as follows (example):

explicit Date(int year)
 :_year(year)
 {
    
    }

(4 Conclusion

1.const must be initialized where it is defined, and there is only one chance
2. For custom type members, it is recommended to use the initialization list to initialize
3. The initialization list can be considered as the place where member variables are defined
4. It is also recommended to use the initialization list for built-in types. Of course, there is no obvious problem in initializing built-in types within the function body
5. Unified suggestion: Use the initialization list if you can. There is basically nothing wrong with using the initialization list
Core: The initialization list is where members are defined


2. static members

1.Definition

Class members declared as static are called static members of the class. Member variables modified with static are called static member variables; member functions modified with
static are called static members. Is a static member function. Static member variables must be initialized outside the class

2.Characteristics

  1. Static members are shared by all class objects and do not belong to a specific object. They are stored in the static area.
  2. Static member variables must be defined outside the class. The static keyword is not added when defining. It is just declared in the class.
  3. Class static members can be accessed using classname::static member or object.static member
  4. Static member functions have no hidden this pointer and cannot access any non-static members.
  5. Static members are also members of the class and are restricted by public, protected, and private access qualifiers.
  6. Can static member functions call non-static member functions? No, this
  7. Can non-static member functions call static member functions of a class? Yes, enter the entire class

3. Youyuan

Friends provide a way to break through encapsulation, which sometimes provides convenience. However, friends will increase coupling and destroy encapsulation, so friends should not be used more than once.
Friends are divided into: friend functions and friend classes

(1) Friend function

Friend function can directly access the private members of the class. It is an ordinary function defined outside the class and does not belong to any class, but it needs to be declared inside the
class. When declaring You need to add the friend keyword.

Insert image description here


(2) Friend class

This part of Youyuan is mainly for understanding the content, and I will explain and learn it when I use it later!
Insert image description here


4. Internal classes

(1) Concepts and precautions

Concept: If a class is defined inside another class, this inner class is called an inner class. The inner class is an independent class. It does not belong to the outer class, and the members of the inner class cannot be accessed through the objects of the outer class. The outer class does not have any superior access to the inner class.
Insert image description here


(2) Understand

1. If class B is defined in class A, use A :: B when accessing B
Insert image description here


2. If you want to calculate the byte size of class A, has nothing to do with B, only to A
Insert image description here


3.B is defined in A: (1) A’s class domain restrictions and access qualifiers are required; (2) B is naturally a friend of A
Insert image description here


Summarize

The above is what I will talk about today. This article introduces the knowledge under classes and objects, including initialization list, Static members, friends and other knowledge!
If my work is helpful to you, remember to like and follow it, thank you everyone!
Insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/131147774