In C++, when we define a class, we initialize some variables inside that class. These variables are the “member variable” of the class. We can initialize the “member variable” by utilizing the constructor method in C++ programming. A variable that is linked to a particular class object and is accessible to all its methods or functions is referred to as a “member variable” in C++. In this article, we will declare these “member variables” in the C++ code and explain the “member variables” in detail here.
Example 1:
Let’s begin some code here by including the “header file” that we require during this code. The “iostream” is the header file that we insert in this code. This header file includes as many functions as possible that we need in this code and are declared in it. Then, we must include the namespace, which is the “std”, so we add it here by placing the “using” keyword with the “namespace std”.
We don’t need to add this “std” with the functions separately. Then, we define a class named “SampleClass” and utilize the “public” constructor. In this “public” constructor, we declare some member variables that we will access in our member functions later. The member variables that we declare here are “mVar1” and “mVar2” of the “int” data type.
Then, we declare a function underneath this which is named “setValues()”. In this “setValues()” function, we place the “cout” to print the message which instructs the user to input two values. The two values that the user will enter are saved in the “mVar1” and “mVar2” variables. We get these values with the help of the “cin” method. After getting both values and saving them in “mVar1” and “mVar2”, we define another function below this. The name of that function is “getValues” where we utilize the “cout” statements.
Here, we place the “mVar1” and “mVar2” again in the “cout” to display both values that we get from the previous user. Underneath this, we call the “main()” method in which the class object is generated. The name of the class object is “sc1”. Then, we call the first function which is “setValues” which we define here with the “sc1” object.
After this, we place the object name again and call the second function which is “getValues()”with the “sc1” object name. Now, it gets the values first and then displays them on the console as we call both methods with the class object in which the member variable is accessed in this code.
Code 1:
using namespace std;
class SampleClass{
public:
int mVar1, mVar2;
void setValues(){
cout <> mVar1 >> mVar2;
}
void getValues(){
cout << "The value of first memeber variable = " << mVar1<< endl ;
cout << "The value of second memeber variable = " << mVar2 << endl;
}
};
int main()
{
SampleClass sc1;
sc1.setValues();
sc1.getValues();
return 0;
}
Output:
It prints the message first. Then, we enter “12” and “456” as the input values which are stored in the member variables that we declared in the code. Then, it displays both variables’ values as we access them using the member function.
Example 2:
Now, we define a class named “ProductClass” after adding the “iostream” header file and the “std” namespace. In this “ProductClass”, we utilize the “public” constructor in which we declare two variables of “int” data type. These are “value” and “multiplier” which are member variables.
Then, we define a function here with the name, “calProductOfTwo()” function, to access both variables. This is the member function here and we access the member variables in this function. We utilize the “return” keyword underneath this and place the “value * multiplier” which returns the product of these two variables.
Then, we invoke the “main()” function in which we create the object of the “ProductClass” with the name “p_obj” and then assign the values to the member variables that we previously declared. We initialize these variables with the help of the “class” object. We first place the object name and then the member variable name “p_obj.value” and assign “33” to the “value” variable.
Then, we initialize the “multiplier” variable and assign “98” to this variable. Now, we call the “calProductOfTwo()” function in the “cout” with the object of the “p_obj” class which displays the product of both member variables.
Code 2:
using namespace std;
class ProductClass {
public:
int value;
int multiplier;
int calProductOfTwo() {
return value * multiplier;
}
};
int main() {
ProductClass p_obj;
p_obj.value = 33;
p_obj.multiplier = 98;
cout << "The product of two values = " << p_obj.calProductOfTwo() << endl;
return 0;
}
Output:
The product of the values of the “member” variables is displayed here. We can see that we access the “member” variables inside the “member” function and initialize them after creating the “class” object and calling the “member” function in our code. The product of the values of both member variables is given in the following:
Example 3:
The “SumClass” is defined here in this code. Then, in “public”, we declare three member variables with the names “s_val_1”, “s_val_2”, and “s_val_3” and the data type of all variables is “int”. Then, we define a “calSumOfTwo()” function which is the “member” function and we access the variables in this function. In the “calSumOfTwo()”, we utilize the “return” keyword. Here, we place “s_val_1 + s_val_2”.
Now, it gives the sum of these two variables. Underneath this, we define one more function which is “calSumOfThree()”. In this function, we access all three variables to find their sum and return their result here. Then, we call the “main()” method. The “class” object that is generated here is “sObj1”.
After this, we initialize all three member variables here with the aid of the “class” object and assign “33”, “98”, and “195” to the “s_val_1”, “s_val_2”, and “s_val_3”, respectively. Then, we call both “member” functions in the “cout” in which “calSumOfTwo()” displays the sum of the first two variables and the “calSumOfThree()” displays the sum of all three member variables.
Code 3:
using namespace std;
class SumClass {
public:
int s_val_1;
int s_val_2;
int s_val_3;
int calSumOfTwo() {
return s_val_1 + s_val_2;
}
int calSumOfThree() {
return s_val_1 + s_val_2 + s_val_3;
}
};
int main() {
SumClass sObj1;
sObj1.s_val_1 = 33;
sObj1.s_val_2 = 98;
sObj1.s_val_3 = 195;
cout << "The sum of two values = " << sObj1.calSumOfTwo() << endl;
cout << "The sum of three values = " << sObj1.calSumOfThree() << endl;
return 0;
}
Output:
The sum of the first two member variables is “131” which we get here by calling the first “member” function. The sum of all three member variables is “326″ which we get by calling the second “member” function in our code.
Conclusion
The “member variables” in C++ programming are explored deeply in this article. We explained that the “member variables” are declared after defining the class. These variables are accessible from everywhere in the code after creating the “class” object. We can also access these member variables in the “member” functions. We initialized these variables after creating the “class” object. Here, we declared, initialized, and accessed the “member variables” in our C++ code.