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.
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 Studentclass Student {private://Declare data member pf classstring student_name;int student_age;public://declaring a constructorStudent(string sname,int sage){student_name = sname;student_age = sage;}//declaring a display functionvoid display(){cout<<"Student name is "<<student_name<<" and age is "<< student_age<<endl;}};int main(){ //calling the Student class constructorStudent s1 ("hassan",21);//calling the display functions1.display();return 0;}
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.
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
Let’s look at the coding example that provides an understanding of the static method:
#include <iostream>using namespace std;// Initialize the class Studentclass Student {private:// Declare variablesstring student_name;int student_age;// Declare a static variable for static functionstatic int strengthcount;public:// Declared constructor of Student classStudent(string sname, int sage) {student_name = sname;student_age = sage;Student::strengthcount++;}// Declared a display member function to display student name and agevoid display() {cout << "Student name is " << student_name << " and age is " << student_age << endl;}// Declare a static function to verify admission of the studentstatic void strength() {// Added a limitation of 15 student admissionsif (Student::strengthcount < 15) {cout << "Eligible for admission" << endl;}else {cout << "Admissions are closed" << endl;}}};// Initialize the static variableint Student::strengthcount = 0;int main() {// Create a Student class object and call the constructorStudent s1("hassan", 21);s1.display();// Call a static function to implement a static methodStudent::strength();return 0;}
Note: The code uses the
strength()static function to determine student eligibility. It determines if the number of admitted studentsstrengthcountis fewer than 15, and displaysEligible for admission. If it exceeds 15, it displaysAdmissions are closed. The maximum number of eligible pupils is limited to 15.
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:
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