C ++-the data fields of classes and objects encapsulate the difference between global variables, local variables, static global variables, and static local variables

Data field encapsulation

First put a section of the previous class definition code.

#ifndef CIRCLE_H
 #define CIRCLE_H class Circle {
 public :
     // The radius of the circle double radius; // No parameter constructor     Circle (); // Parameter constructor 
    Circle ( double ); // Get the area of ​​the circle double getArea () ; 
}; #endif


    
    
    

    
    
    
    
    

Before we put all the data fields and functions in the public. Public means public, that is, anyone can access these data and functions.

For example, if you want to modify the radius of a circle object c1, you can modify it by directly accessing radius

c1.radius = 2;

But this style is not good. Specifically in:

  • The data may be messed up. Suppose a function wants to use an attribute of a class. But this non-encapsulated property can be modified. This may cause the function to perform different results.
  • Not easy to maintain. The radius of the circle must be greater than 0, but if the data is not encapsulated and can be modified directly, we cannot meet this condition.

We should encapsulate our data, and functions are generally not encapsulated.

The keyword representing encapsulation is private, which is similar to public. Private data or functions can only be accessed inside the class. If you access them directly at this time, the compiler will report an error. See the code below:

#ifndef CIRCLE_H
 #define CIRCLE_H class Circle {
 private :
     // the radius of the circle double radius; public :
     // the parameterless constructor     Circle (); // the parameterized constructor 
    Circle ( double ); // the area of ​​the circle double getArea (); // Get the radius of the circle double getRadius (); // Set the radius of the circle and judge whether it is greater than 0 void setRadius ( double newRadius); 
}; #endif


    



    
    
    
    
    

    
    

    
    

In the above code, we encapsulated the data radius and provided getter and setter methods, that is, getRadius () and setRadius (double newRadius). These two methods are respectively responsible for obtaining and modifying data, and the naming methods are generally getXxx () and setXxx (), which use the first letter to distinguish two words.

The getter method is like an accessor, and the setter is like a changer.

Not all data requires getter and setter methods, it depends on the situation.

Variable scope

The variables of a program can be roughly divided into two types: global variables and local variables.

Local variables are defined in the function, and their scope is limited to the function. The compiler will report an error after leaving the function and then use this variable.
Global variables are variables defined outside the function. It does not belong to which function, it belongs to a source program file, and its scope is the entire source program.

About variable scope, you can refer to this blog:

The difference between global variables, local variables, static global variables, static local variables

The variables in the class also comply with these rules.

In particular, if a data field or function is declared as static (that is, static data or function), the data or function does not belong to a specific object instance, but belongs to this class. Therefore, the static data of all objects is the same.

 

Guess you like

Origin www.cnblogs.com/bwjblogs/p/12686831.html