Member Variables and Functions in C++
In the C++ programming language, members, user-defined class variable parameters (also known as class data type variable members) and class member functions (also known as function methods declared in a class) are important elements of a custom declared class. Declaring class member elements in a C++ program provides C++ users with the ability to declare the object model of a real-world class and define the current behavior of an existing class.

So, let’s get to know class members and functions better in the C++ programming language.
Class member variable (data member) concept in C++.
Member variables in a user-defined class are custom variables that are connected to a user-defined class in some way. These class member variables hold or represent the state or attributes of a class object. Where each object in a user-declared class has its own custom collection of member variables. They can store and process custom values that define or declare the state of the existing class.
Types of member variables in a user-defined class.
- Instance variables – These variables are declared like regular member variables in a user-defined custom class. They are somehow connected to a particular instance (object) of the existing class.
- Static variables – These variables are shared between all instances of a user-defined class, if needed. C++ users can declare them in a class by using the static keyword.
Example of an instance member variable in a C++ program.
#include <iostream>
#include <string>
class Student {
public:
// here we define a class instance member variable
std::string stu_name;
int roll_no;
int classes;
// here we define a class student name Constructor with parameter
Student(std::string s, int r, int c)
: stu_name(s), roll_no(r), classes(c) {}
// here we use Member function to display student class data info
void student1Info() {
std::cout << “\n Name of student – ” << stu_name
<< ” \n Roll_no – ” << roll_no
<< ” \n Running classes – ” << classes
<< std::endl;
}
};
int main() {
// here we Create a class object
Student student1(“Siddhi deora”, 101, 10);
student1.student1Info();
return 0;
}
Explaining instance member variables in C++.
- In this example, the Student class instance variables stu_name, roll_no, and classes are user-defined class instance variables. Each Student class object will have its own stu_name, roll_no, and classes class information.
Class static member variable concept in C++.
A static member in a user-defined class is shared across all instances of the class. These static class member variables can be accessed directly without creating an object of the class.
Example of a class static member variable.
#include <iostream>
class Student {
public:
// Here we create a static class member variable parameter
static int stud_data;
// Here we create a constructor function
Student() {
stud_data++;
}
// Here we define a member function to display student information
void studentInfo() {
std::cout << “Numbers of display student information – ” << stud_data << std::endl;
}
};
// Here we initialize the static class member variable
int Student::stud_data = 0;
int main() {
// Here we create multiple student class objects
Student student1;
student1.studentInfo(); // Result – Numbers of display student information – 1
Student student2;
student2.studentInfo(); // Result – Numbers of display student information – 1
return 0;
}
Explanation of class static member variable.
- In this example, stud_data is a static class variable. It is shared and used by all Student class objects. It is created every time a new Student class object is created. Then the value of the stud_data static variable is automatically incremented.
Member function (method) concept in C++.
Member functions in a user-defined class are custom class functions that are created within a custom user-declared class and operate on the declared data type parameter members of this custom class. In these classes, the user can either make necessary modifications to instance variables or apply certain operations related to the existing class.
Types of member functions in a C++ class.
- Regular member functions – These are functions in an existing class that operate on user-defined class instance variables.
- Regular member functions – These are functions in an existing class that are directly connected to the class itself instead of a user-defined special class object.
Example of a regular member function.
#include <iostream>
class Triangle {
Public:
// here we define a triangle class Member variables
int side1, side2, side3;
// here we define a Member function to set the sides
void setSides(int s1, int s2, int s3) {
side1 = s1;
side2 = s2;
side3 = s3;
}
//here we use Member function to compute the parameter
int getPerimeter() {
return side1 + side2 + side3;
}
// here we use Member function to display the all triangle sides
void display() {
std::cout << “Side 1 – ” << side1 << std::endl;
std::cout << “Side 2 – ” << side2 << std::endl;
std::cout << “Side 3 – ” << side3 << std::endl;
}
};
int main() {
// Here we create an object of the Triangle class
Triangle tri;
// Here we set the triangle sides
tri.setSides(3, 4, 5);
// Here we display the triangle sides
tri.display();
// Here we display the triangle perimeter
std::cout << “The total perimeter of the triangle – ” << tri.getPerimeter() << std::endl;
return 0;
}
Explanation of a regular member function.
- In this example, two member functions are defined in the Triangle class. SetSides and getPerimeter are custom functions that calculate the three sides of the existing Triangle class and display their values.
Static member function concept in C++.
A static member function declared in a C++ class program can only access and manage a static member variable, and cannot operate on class instance variables in any way. Static member functions in user-defined classes are mostly used as utility functions or to manage global data for the entire class.
Example of a static member function.
#include <iostream>
class Course {
public:
// Here we declare a static member variable in the course class
static int course_data;
// Here we define a custom constructor
Course() {
course_data++;
}
// Here we use a static member function to get the number of course elements
static int getcourse_data() {
return course_data;
}
};
// Here we initialize the course class static member variable
int Course::course_data = 0;
int main() {
// Here we create objects of the Course class
Course course1;
Course course2;
// Here we access the static function without creating a course class object
std::cout << “List of available Courses – ” << Course::getcourse_data() << std::endl; // Result – List of available Courses – 2
return 0;
}
Explanation of a static member function.
- In this example, course_data() is a static class member function that assigns a value to a user-defined static variable course_data, which keeps track of the number of Course objects created in the current Course class.
