Home  >  Article  >  Backend Development  >  Characteristics of Accessor function in C++

Characteristics of Accessor function in C++

藏色散人
藏色散人Original
2019-03-22 16:53:293343browse

c is an object-oriented programming language, and one of its features is the concept of encapsulation. With encapsulation, programmers define labels for data members and functions and specify whether they are accessible by other classes. When programmers mark data members as "private", they cannot be accessed and manipulated by member functions of other classes. Accessor allows access to these private data members.

Characteristics of Accessor function in C++

Accessor function

The access function and mutator function in c are similar to the set and get functions in c#. They are used as an alternative to making a class member variable public and changing it directly in the object. To access private object members, the Accessor function must be called.

Usually, for members such as Level, the function GetLevel() returns the value of Level, and SetLevel() assigns a value to it.

Characteristics of the Accessor function

Accessor requires no parameters

Accessor has the same type as the retrieved variable

Accessor's The name begins with the Get prefix

The naming convention is necessary

Mutator function

Although the Accessor function makes the data member accessible, it does not make the data Members can edit. Modifying protected data members requires a mutator function.

Because they provide direct access to protected data, mutator and accessor functions must be written and used with care.

Related recommendations: "C Tutorial"

The above is the detailed content of Characteristics of Accessor function in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn