Static Data Members and Static Member Functions in C++ with Examples

1. Introduction to Static Data Members in C++

A static data member is a class member declared using the static keyword. Unlike ordinary data members, a static data member is shared by all objects of the class. Only one copy of a static data member exists for the class, regardless of how many objects are created.

Static Data Member वह class member होता है जिसे static keyword की सहायता से declare किया जाता है। सामान्य data members के विपरीत, static data member class के सभी objects के बीच shared होता है। Class के लिए static data member की केवल एक copy होती है, चाहे कितने भी objects create किए जाएँ।

Syntax

class ClassName
{
public:
static data_type variableName;
};

A static data member declared inside a class generally needs a separate definition outside the class.

Class के अंदर declare किए गए static data member को सामान्यतः class के बाहर अलग से define करना पड़ता है।

Example 1: Static Data Member in C++

#include <iostream>
using namespace std;
class Student
{
public:
static int count;
};
int Student::count = 0;
int main()
{
Student s1, s2, s3;
s1.count = 10;
cout << "Value using s1: " << s1.count << endl;
cout << "Value using s2: " << s2.count << endl;
cout << "Value using s3: " << s3.count;
return 0;
}

Output:

Value using s1: 10
Value using s2: 10
Value using s3: 10

Explanation:

  • count is declared as a static data member using the static keyword.

  • int Student::count = 0; defines and initializes the static data member outside the class.

  • Three objects, s1, s2 and s3, are created.

  • When s1.count is changed to 10, the same value is visible through the other objects because the variable is shared.

व्याख्या:

  • count को static keyword की सहायता से static data member declare किया गया है।

  • int Student::count = 0; class के बाहर static data member को define और initialize करता है।

  • s1, s2 और s3 नाम के तीन objects create किए गए हैं।

  • जब s1.count की value 10 की जाती है, तो यही value अन्य objects के माध्यम से भी दिखाई देती है क्योंकि यह variable shared होता है।

2. Accessing Static Data Members

Static data members can be accessed using the class name and scope resolution operator (::). They can also be accessed through objects, although using the class name is generally preferred.

Static Data Members को class name और Scope Resolution Operator (::) की सहायता से access किया जा सकता है। इन्हें objects के माध्यम से भी access किया जा सकता है, लेकिन सामान्यतः class name का उपयोग करना बेहतर होता है।

Example 2: Accessing a Static Data Member Using Class Name

#include <iostream>
using namespace std;
class Employee
{
public:
static int employeeCount;
};
int Employee::employeeCount = 100;
int main()
{
cout << "Employee Count: "
<< Employee::employeeCount;
return 0;
}

Output:

Employee Count: 100

Explanation:

  • employeeCount is a static data member.

  • It is accessed using Employee::employeeCount.

  • An object of the class is not required to access this static member.

व्याख्या:

  • employeeCount एक static data member है।

  • इसे Employee::employeeCount की सहायता से access किया गया है।

  • Static member को access करने के लिए class के object की आवश्यकता नहीं होती।

3. Introduction to Static Member Functions in C++

A static member function is a member function declared using the static keyword inside a class. It belongs to the class rather than to any particular object. Therefore, it can be called using the class name without creating an object.

Static Member Function वह member function होता है जिसे class के अंदर static keyword की सहायता से declare किया जाता है। यह किसी particular object के बजाय पूरी class से संबंधित होता है। इसलिए इसे object create किए बिना class name की सहायता से call किया जा सकता है।

Syntax

class ClassName
{
public:
static return_type functionName(parameters);
};

Example 3: Static Member Function in C++

#include <iostream>
using namespace std;
class Test
{
public:
static void display()
{
cout << "Static Member Function";
}
};
int main()
{
Test::display();
return 0;
}

Output:

Static Member Function

Explanation:

  • display() is declared as a static member function.

  • It is called using the class name Test.

  • No object of the class is required.

व्याख्या:

  • display() को static member function declare किया गया है।

  • इसे class name Test की सहायता से call किया गया है।

  • इसके लिए class का object create करना आवश्यक नहीं है।

4. Characteristics of Static Member Functions

Static member functions have some special characteristics in C++.

C++ में Static Member Functions की कुछ विशेषताएँ होती हैं।

  1. They belong to the class rather than to individual objects.

    ये individual objects के बजाय पूरी class से संबंधित होते हैं।

  2. They can be called using the class name and scope resolution operator.

    इन्हें class name और Scope Resolution Operator की सहायता से call किया जा सकता है।

  3. They can directly access static data members and other static member functions of the class, subject to access control.

    ये access control के नियमों के अधीन class के static data members और अन्य static member functions को directly access कर सकते हैं।

  4. They do not have a this pointer because they are not associated with a particular object.

    इनमें this pointer नहीं होता क्योंकि ये किसी particular object से संबंधित नहीं होते।

  5. They cannot directly access non-static data members or call non-static member functions because those members require an object.

    ये non-static data members को directly access नहीं कर सकते और न ही non-static member functions को directly call कर सकते हैं क्योंकि इनके लिए object आवश्यक होता है।

5. Static Member Functions Accessing Static Data Members

A static member function can directly access static data members of the same class.

Static Member Function उसी class के static data members को directly access कर सकता है।

Example 4: Static Data Member and Static Member Function

#include <iostream>
using namespace std;
class Student
{
private:
static int count;
public:
static void setCount(int c)
{
count = c;
}
static void displayCount()
{
cout << "Student Count: " << count;
}
};
int Student::count = 0;
int main()
{
Student::setCount(50);
Student::displayCount();
return 0;
}

Output:

Student Count: 50

Explanation:

  • count is a private static data member.

  • setCount() is a static member function that updates count.

  • displayCount() is a static member function that displays count.

  • Both functions are called using the class name.

व्याख्या:

  • count एक private static data member है।

  • setCount() एक static member function है जो count को update करता है।

  • displayCount() एक static member function है जो count को display करता है।

  • दोनों functions को class name की सहायता से call किया गया है।

6. Difference Between Static and Non-Static Data Members

BasisStatic Data MemberNon-Static Data Member
DeclarationUses static keywordDoes not use static keyword
OwnershipBelongs to the classBelongs to each object
MemoryOne shared entity per classSeparate member for each object
ValueShared among all objectsCan differ between objects
AccessCan be accessed using class nameNormally accessed through an object

7. Difference Between Static and Non-Static Member Functions

BasisStatic Member FunctionNon-Static Member Function
DeclarationUses static keywordDoes not use static keyword
AssociationBelongs to the classAssociated with objects
CallingCan be called using class nameNormally called using an object
this pointerNot availableAvailable
Static data membersCan directly access themCan access them
Non-static data membersCannot access directlyCan access them

8. Advantages of Static Data Members and Static Member Functions

Static members are useful when data or functionality needs to be shared across all objects of a class.

जब किसी data या functionality को class के सभी objects के बीच share करना हो, तब static members उपयोगी होते हैं।

  1. Shared Data: Static data members maintain a common value for all objects.

    Shared Data: Static data members सभी objects के लिए एक common value maintain करते हैं।

  2. Memory Efficiency: A static data member is not duplicated for every object.

    Memory Efficiency: Static data member की हर object के लिए अलग copy नहीं बनती।

  3. Object-independent Operations: Static member functions can perform class-related operations without requiring an object.

    Object-independent Operations: Static member functions बिना object create किए class-related operations perform कर सकते हैं।

  4. Centralized Information: Static members can maintain common information such as an object counter.

    Centralized Information: Static members object counter जैसी common information maintain कर सकते हैं।

Static Data Members and Static Member Functions are important features of C++ classes. Static Data Members are shared among all objects of a class, whereas Static Member Functions belong to the class and can be called without creating an object. These features are useful for maintaining shared data and performing class-level operations.

C++ में Static Data Members और Static Member Functions classes के महत्वपूर्ण features हैं। Static Data Members class के सभी objects के बीच shared होते हैं, जबकि Static Member Functions पूरी class से संबंधित होते हैं और इन्हें object create किए बिना call किया जा सकता है। ये features shared data maintain करने और class-level operations perform करने के लिए उपयोगी हैं।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)