Member functions of C++ classes

The nature of C++ member functions

In C++, the member function of a class is a kind of function, which has a return value and a function type. The difference between it and a general function is only:

The members belonging to a class appear in the class body.
Can be specified as private, public or protected.

When C++ uses a class function, pay attention to the permission to call it and its scope. Private member functions can only be called by other member functions in this class, and cannot be called outside the class. Member functions can access this class Any member can refer to valid data in this scope.

The general approach is to designate the member functions that need to be called by the outside world as public. They are the external interface of the class, but it should be noted that not all member functions are required to be designated as public.

In C++, some functions are not intended to be called for the outside world, but are called by member functions in this class, so they should be designated as private. The function of this kind of function is to support the operation of other functions. It is a function of other members of the class, and users outside the class cannot call these private functions.

The member function of a class is a very important part of the class body. If a class does not contain member functions, it is equivalent to a structure in the C language, and readers need to pay special attention.

C++ class definition member function

The member functions mentioned above are defined in the class body. In C++, you can also write only the member function declaration in the class body, and define the function outside the class.

Define the print function outside the class:

struct Student //用struct来声明一个类类型 
{
    
    
  private: //声明以下为私有的 
    int number; //学号 
    char name[10];//姓名 
    char sex; //性别 
  public: //声明以下为共有的 
    void print_info(); //函数声明 
};
void Studnet::print_info() //在类外定义打印函数 
    {
    
    
      cout<<number<<endl;
      cout<<name<<endl;
      cout<<sex<<endl;
    };
Student stu1,stu2;//定义了Student类对象

When C++ directly defines a function in the class body, there is no need to add the class name in front of the function name, but when the member function is defined outside the class, the class name must be added in front of the function name.

//作用域限定符

In C++, a scope qualifier is used to declare which class a function belongs to. If there is no class name before the scope operator, or there is neither a class name nor a scope qualifier before the function name, it means that the function does not belong to any Class, this function is not a member function, but a global function.

C++ class functions must be declared in the class body first, and then defined outside the class, which means that the position of the class body should be before the function definition, otherwise an error will occur during compilation.

Although the function is defined outside the class, when a member function is called, the definition of the function is found according to the function prototype declared in the class, and the function is executed.

Above, if you read and think it is helpful to you, please give Xiaolin a compliment, so Xiaolin also has the motivation to update, thank you fathers and fellows~ More cases of
C++ member functions
can go to the public account: C Language entry to proficiency

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112761715