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: 10Value using s2: 10Value using s3: 10
Explanation:
countis declared as a static data member using thestatickeyword.int Student::count = 0;defines and initializes the static data member outside the class.Three objects,
s1,s2ands3, are created.When
s1.countis changed to 10, the same value is visible through the other objects because the variable is shared.
व्याख्या:
countकोstatickeyword की सहायता से 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: 100Explanation:
employeeCountis 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 FunctionExplanation:
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 की कुछ विशेषताएँ होती हैं।
They belong to the class rather than to individual objects.
ये individual objects के बजाय पूरी class से संबंधित होते हैं।
They can be called using the class name and scope resolution operator.
इन्हें class name और Scope Resolution Operator की सहायता से call किया जा सकता है।
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 कर सकते हैं।
They do not have a
thispointer because they are not associated with a particular object.इनमें
thispointer नहीं होता क्योंकि ये किसी particular object से संबंधित नहीं होते।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: 50Explanation:
countis a private static data member.setCount()is a static member function that updatescount.displayCount()is a static member function that displayscount.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
| Basis | Static Data Member | Non-Static Data Member |
|---|---|---|
| Declaration | Uses static keyword | Does not use static keyword |
| Ownership | Belongs to the class | Belongs to each object |
| Memory | One shared entity per class | Separate member for each object |
| Value | Shared among all objects | Can differ between objects |
| Access | Can be accessed using class name | Normally accessed through an object |
7. Difference Between Static and Non-Static Member Functions
| Basis | Static Member Function | Non-Static Member Function |
|---|---|---|
| Declaration | Uses static keyword | Does not use static keyword |
| Association | Belongs to the class | Associated with objects |
| Calling | Can be called using class name | Normally called using an object |
this pointer | Not available | Available |
| Static data members | Can directly access them | Can access them |
| Non-static data members | Cannot access directly | Can 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 उपयोगी होते हैं।
Shared Data: Static data members maintain a common value for all objects.
Shared Data: Static data members सभी objects के लिए एक common value maintain करते हैं।
Memory Efficiency: A static data member is not duplicated for every object.
Memory Efficiency: Static data member की हर object के लिए अलग copy नहीं बनती।
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 कर सकते हैं।
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
Post a Comment