Home > Article > Backend Development > What are class member variables in c++
C class member variables are stored in object memory and belong to a specific class. They can be declared in the definition of the class and initialized at declaration time or using a constructor. They can be accessed using the dot operator (.) and can be any C data type.
Class member variables in C
Class member variables are stored in object memory and belong to a specific class data item. They are declared and initialized in the class definition.
Declaring member variables
Member variables can be declared in the definition of the class, using the following syntax:
<code class="cpp">class ClassName { public: // 成员变量声明 int member_variable; };</code>
int member_variable;
An integer member variable named member_variable
is declared.
Initialize member variables
Member variables can be initialized at declaration time or using a constructor.
Initialized at declaration:
<code class="cpp">class ClassName { public: // 成员变量声明并初始化为 10 int member_variable = 10; };</code>
Initialized using constructor:
<code class="cpp">class ClassName { public: // 构造函数 ClassName(int initial_value) : member_variable(initial_value) {} // 成员变量 int member_variable; };</code>
Access member variables
You can use the dot operator (.) to access member variables in the object:
<code class="cpp">ClassName object; object.member_variable = 5; // 设置 member_variable 为 5 int value = object.member_variable; // 获取 member_variable 的值</code>
Type of member variable
Member variable can be any C data types, including:
The above is the detailed content of What are class member variables in c++. For more information, please follow other related articles on the PHP Chinese website!