What is the difference between static and class methods in C++?

A class method is a function declared within a class as a member function in object-oriented programming. It plays a vital role in adhering to the “don’t repeat yourself” (DRY) principle by allowing reusable code snippets. Programmers can efficiently use specific functionality in a program when needed, but solely for the base and inherited classes. To illustrate this point, let’s delve into an example.

Class method

Let’s look at the following coding example that provides a proper understanding of the class method:

//C++ program
#include <iostream>
using namespace std;
//Initialize the class Student
class Student {
private:
//Declare data member pf class
string student_name;
int student_age;
public:
//declaring a constructor
Student(string sname,int sage)
{
student_name = sname;
student_age = sage;
}
//declaring a display function
void display(){
cout<<"Student name is "<<student_name<<" and age is "<< student_age<<endl;
}
};
int main()
{ //calling the Student class constructor
Student s1 ("hassan",21);
//calling the display function
s1.display();
return 0;
}

Explanation

  • Line 5: We declare a class named Student.

  • Lines 8–9: We declare variables to store data.

  • Lines 13–17: We define a constructor through which we pass values using parameters.

  • Lines 19–22: We declare the class method with named Display in which we display the student’s name and age.

  • Line 27: We initialize an object for the Student class with the name s1 during which we invoke the constructor of the Student class and pass values to its parameters.

  • Line 29: We call the display function with the s1 object to display the value initialized through the constructor.

Static method

A static method is similar to a class method but relies on a static function rather than an object of the class. It must be declared within the class. The distinctive feature of a static method is that it is accessed using the :: scope operator with the class, eliminating the need for an object of the class.

Note: In the static method, we can only use a static variable and function because we allot a fixed memory at the time of compiling

Example

Let’s look at the coding example that provides an understanding of the static method:

#include <iostream>
using namespace std;
// Initialize the class Student
class Student {
private:
// Declare variables
string student_name;
int student_age;
// Declare a static variable for static function
static int strengthcount;
public:
// Declared constructor of Student class
Student(string sname, int sage) {
student_name = sname;
student_age = sage;
Student::strengthcount++;
}
// Declared a display member function to display student name and age
void display() {
cout << "Student name is " << student_name << " and age is " << student_age << endl;
}
// Declare a static function to verify admission of the student
static void strength() {
// Added a limitation of 15 student admissions
if (Student::strengthcount < 15) {
cout << "Eligible for admission" << endl;
}
else {
cout << "Admissions are closed" << endl;
}
}
};
// Initialize the static variable
int Student::strengthcount = 0;
int main() {
// Create a Student class object and call the constructor
Student s1("hassan", 21);
s1.display();
// Call a static function to implement a static method
Student::strength();
return 0;
}

Note: The code uses the strength() static function to determine student eligibility. It determines if the number of admitted students strengthcount is fewer than 15, and displays Eligible for admission. If it exceeds 15, it displays Admissions are closed. The maximum number of eligible pupils is limited to 15.

Explanation

  • Line 5: We declare a class names Student .

  • Lines 8– 9: We declare variables to store data.

  • Line 12: We declare a static variable.

  • Lines 14–20: We declare a constructor in which we pass values through its parameters.

  • Lines 23–25: We declare the first-class method of the program with the name Display in which we display the students’ names and ages.

  • Lines 28–37: We declare a static function, strength(), in which we add an admission eligibility condition.

  • Line 40: We initialize the strengthcount variable in which we store students’ strength.

  • Line 42: We initialize an object for the Student class with the name s1 in which we call the constructor of the Student class and pass values to its parameters.

  • Line 45: We call the display function with the s1 object to display the value initialized through the constructor.

  • Line 48: We initialize a static method by calling Student::strength(); using the Student class, a :: scope resolution operator, and the strength static function.

Let's summarize the key differences of static and class methods:

Static method vs. Class method

Feature

Static method

Class Method


Access to class members

Can only access static members and other static methods of the class.

Can access both static and non-static (instance) members of the class.

Use of 'this' pointer

No 'this' pointer is available inside a static method.

Has access to the 'this' pointer, allowing access to the instance it was called on.

Invocation

Can be invoked using the class name or an object instance.

Typically invoked using an object instance, but can also be invoked using the class name.

Memory allocation

No implicit access to instance-specific memory, as it operates at the class level.

Can implicitly access instance-specific memory through the 'this' pointer.

Friend functions

Can be declared as friend functions inside a class.

Cannot be declared as friend functions inside a class.

Usage in constructors and destructors

Cannot be used directly in constructors and destructors.

Can be used in constructors and destructors.


Overloading consideration

Can be overloaded based on the parameters but not based on whether they are const or non-const.

Can be overloaded based on both the parameters and whether they are const or non-const.

Inheritance

Cannot be virtual.

Can be virtual, allowing for dynamic dispatch in the presence of polymorphism.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved