Static Data Members & Static Member Functions in C++ Notes | Static Keyword, Static Variables, Static Functions & Examples | Basic Computer Engineering | RGPV BTech First Year

Static Data Members & Static Member Functions in C++ Notes | Static Keyword, Static Variables, Static Functions & Examples | Basic Computer Engineering | RGPV BTech First Year


Static Data Members & Static Member Functions in C++

Static Keyword C++ का एक महत्वपूर्ण Keyword है जिसका उपयोग किसी Variable अथवा Member को पूरी Class के सभी Objects के बीच साझा (Shared) बनाने के लिए किया जाता है। जब किसी Data Member को static घोषित किया जाता है, तब उसकी केवल एक ही Copy Memory में बनाई जाती है और वही Copy सभी Objects द्वारा उपयोग की जाती है।

इसी प्रकार Static Member Function ऐसा Member Function होता है जो केवल Static Data Members को Access कर सकता है तथा उसे Object के बजाय सीधे Class Name द्वारा Call किया जा सकता है।

RGPV B.Tech First Year के Basic Computer Engineering Subject में Static Data Members & Static Member Functions अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Static Keyword, Static Data Member, Static Member Function, Syntax तथा Programs से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।

Class



Static Member



Single Memory Copy



Shared by All Objects

Introduction to Static Keyword

सामान्यतः जब किसी Class के अनेक Objects बनाए जाते हैं, तब प्रत्येक Object के लिए अलग-अलग Data Members की Memory Allocate होती है। लेकिन यदि कोई Data सभी Objects के लिए समान (Common) हो, तो प्रत्येक Object के लिए अलग Memory Allocate करना उचित नहीं होता।

इस समस्या के समाधान के लिए C++ में Static Keyword का उपयोग किया जाता है। Static Member की केवल एक Copy Memory में रहती है और सभी Objects उसी Copy का उपयोग करते हैं।


What is Static Data Member?

Static Data Member वह Data Member होता है जिसे static Keyword द्वारा Declare किया जाता है। इसकी केवल एक ही Memory Location होती है, चाहे उस Class के कितने भी Objects बनाए जाएँ।

class Student { static int count; };

ऊपर दिए गए उदाहरण में count एक Static Data Member है।


Need of Static Data Member

जब किसी Class के सभी Objects को एक Common Data Share करना हो, तब Static Data Member का उपयोग किया जाता है। उदाहरण के लिए Total Number of Objects, Company Name, Interest Rate अथवा College Name जैसी जानकारी सभी Objects के लिए समान हो सकती है।

  • Common Data को सभी Objects के बीच Share करने के लिए।
  • Memory की बचत (Memory Saving) करने के लिए।
  • Object Counter बनाने के लिए।
  • Class Level Information Store करने के लिए।
  • Program की Efficiency बढ़ाने के लिए।
  • Large Scale Object Oriented Applications में Common Information Maintain करने के लिए।
  • Code Reusability एवं Easy Data Management के लिए।

Advantages of Static Data Member

Advantage Description
Memory Saving केवल एक Memory Copy बनाई जाती है।
Shared Data सभी Objects एक ही Data का उपयोग करते हैं।
Easy Management Common Information को आसानी से Manage किया जा सकता है।
Object Counter Objects की कुल संख्या Track की जा सकती है।
High Efficiency Memory एवं Performance दोनों में सुधार होता है।

Characteristics of Static Data Member

Static Data Member की कई महत्वपूर्ण विशेषताएँ (Characteristics) होती हैं जो इसे सामान्य Data Member से अलग बनाती हैं। Static Member पूरी Class से संबंधित होता है, किसी एक Object से नहीं।

  • इसे static Keyword द्वारा Declare किया जाता है।
  • इसकी केवल एक ही Memory Copy होती है।
  • सभी Objects इसी एक Copy को Share करते हैं।
  • Memory केवल एक बार Allocate होती है।
  • Class Load होने पर Static Member Initialize होता है।
  • Static Member का Lifetime पूरे Program तक रहता है।
  • इसे Class Name द्वारा भी Access किया जा सकता है।

Declaration of Static Data Member

Static Data Member को Class के अंदर static Keyword द्वारा Declare किया जाता है। केवल Declaration Class के अंदर लिखी जाती है।

class Employee { public: static int count; };

ऊपर दिए गए उदाहरण में count एक Static Data Member है।


Definition of Static Data Member

Static Data Member की Memory Allocate करने के लिए उसकी Definition Class के बाहर लिखी जाती है। यदि Definition नहीं लिखी जाती, तो Program Linking Error दे सकता है।

int Employee::count = 0;

यह Statement Static Data Member के लिए Memory Allocate करता है तथा उसकी प्रारंभिक Value 0 निर्धारित करता है।


Example of Static Data Member

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

Output

3

ऊपर दिए गए Program में तीन Objects Create किए गए हैं। प्रत्येक Constructor Call पर count की Value बढ़ती है। क्योंकि count Static है, इसलिए सभी Objects एक ही Variable को Share करते हैं।


Accessing Static Data Member

Static Data Member को Object द्वारा भी Access किया जा सकता है तथा सीधे Class Name द्वारा भी Access किया जा सकता है। सामान्यतः Class Name द्वारा Access करना अधिक उपयुक्त माना जाता है।

Student::count

ऊपर दिए गए Statement में count को सीधे Student Class के माध्यम से Access किया गया है।


Static Data Member vs Normal Data Member

Static Data Member Normal Data Member
पूरी Class से संबंधित होता है। प्रत्येक Object से संबंधित होता है।
केवल एक Memory Copy होती है। प्रत्येक Object की अलग Memory Copy होती है।
सभी Objects इसे Share करते हैं। प्रत्येक Object का अलग Data होता है।
Class Name द्वारा Access किया जा सकता है। Object द्वारा Access किया जाता है।
Memory की बचत होती है। Memory अधिक उपयोग होती है।

Static Member Function

Static Member Function वह Member Function होता है जिसे static Keyword द्वारा Declare किया जाता है। यह Function किसी विशेष Object से संबंधित नहीं होता बल्कि पूरी Class से संबंधित होता है। इसलिए इसे Object के बजाय सीधे Class Name द्वारा Call किया जा सकता है।

Static Member Function केवल Static Data Members तथा अन्य Static Member Functions को सीधे Access कर सकता है। यह Non-Static Data Members को सीधे Access नहीं कर सकता।


Syntax of Static Member Function

class Student { public: static void display(); };

ऊपर दिए गए उदाहरण में display() एक Static Member Function है।


Example of Static Member Function

#include<iostream> using namespace std; class Student { public: static int count; static void show() { cout << count; } }; int Student::count = 100; int main() { Student::show(); return 0; }

Output

100

ऊपर दिए गए Program में show() एक Static Member Function है जिसे सीधे Student::show() द्वारा Call किया गया है। Function ने Static Data Member count को Display किया है।


Rules of Static Member Function

  • इसे static Keyword द्वारा Declare किया जाता है।
  • इसे Object के बजाय Class Name द्वारा Call किया जा सकता है।
  • यह केवल Static Data Members को सीधे Access कर सकता है।
  • यह Non-Static Data Members को सीधे Access नहीं कर सकता।
  • इसमें this Pointer उपलब्ध नहीं होता।
  • यह केवल अन्य Static Member Functions को सीधे Call कर सकता है।
  • पूरी Class के लिए केवल एक ही Copy उपलब्ध रहती है।

Working of Static Member Function

Class



Static Function



Access Static Members



Return Result

जब Static Member Function Call किया जाता है, तब वह किसी Object पर निर्भर नहीं होता। यह सीधे Class के Static Data Members पर कार्य करता है तथा आवश्यक Result Return करता है।


Static Member Function vs Normal Member Function

Static Member Function Normal Member Function
पूरी Class से संबंधित होता है। किसी विशेष Object से संबंधित होता है।
Class Name द्वारा Call किया जा सकता है। Object द्वारा Call किया जाता है।
केवल Static Members को सीधे Access करता है। Static तथा Non-Static दोनों Members को Access कर सकता है।
this Pointer उपलब्ध नहीं होता। this Pointer उपलब्ध होता है।
Object की आवश्यकता नहीं होती। Object आवश्यक होता है।

Applications of Static Data Members

Static Data Members का उपयोग उन परिस्थितियों में किया जाता है जहाँ किसी Class के सभी Objects के लिए एक Common Information बनाए रखना आवश्यक हो। इससे Memory की बचत होती है तथा Data को Centralized रूप से Manage किया जा सकता है।

Application Purpose
Object Counter कुल बनाए गए Objects की संख्या Track करना।
Company Information सभी Employees के लिए समान Company Name Store करना।
College Details सभी Students के लिए समान College Name Maintain करना।
Bank Interest Rate सभी Accounts के लिए समान Interest Rate रखना।
Configuration Values पूरे Program के लिए Common Settings Store करना।

Applications of Static Member Function

Static Member Functions का उपयोग तब किया जाता है जब किसी Operation को किसी विशेष Object के बजाय पूरी Class के स्तर (Class Level) पर Execute करना हो।

Application Purpose
Object Counter Display Static Counter की Value Display करना।
Configuration Access Global Configuration Values प्राप्त करना।
Utility Functions Class Level Helper Functions उपलब्ध कराना।
Factory Methods Objects Create करने के लिए Common Methods उपलब्ध कराना।
System Information पूरे Program की Common Information Return करना।

Advantages of Static Member Function

  • Object बनाए बिना Function को Call किया जा सकता है।
  • Class Level Operations को सरल बनाता है।
  • Memory की बचत होती है क्योंकि Function की केवल एक Copy होती है।
  • Static Data Members को Efficient तरीके से Manage करता है।
  • Program की Performance बेहतर होती है।
  • Utility तथा Helper Functions बनाने में उपयोगी है।
  • Code Organization बेहतर बनती है।

Limitations of Static Members

  • Static Member Function सीधे Non-Static Data Members को Access नहीं कर सकता।
  • Static Functions में this Pointer उपलब्ध नहीं होता।
  • Static Data सभी Objects द्वारा Share किया जाता है, इसलिए गलत Modification सभी Objects को प्रभावित कर सकती है।
  • अत्यधिक Static Members Program की Flexibility कम कर सकते हैं।
  • Improper Design से Program में Tight Coupling बढ़ सकती है।
  • Object Specific Information को Static Member में Store नहीं करना चाहिए।

Best Practices for Using Static Members

  • केवल Common Data को ही Static Data Member बनाएं।
  • Object Specific Information को Static Member में Store न करें।
  • Static Member Function का उपयोग केवल Class Level Operations के लिए करें।
  • Static Data Members को Meaningful Initial Value दें।
  • Static Variables को Class Name द्वारा Access करना बेहतर Practice है।
  • जहाँ आवश्यक हो वहीं Static Keyword का उपयोग करें।
  • Program की Readability एवं Maintainability बनाए रखें।

Real Life Example

मान लीजिए किसी Employee Management System में सभी Employees के लिए Company Name समान है। ऐसी स्थिति में Company Name को Static Data Member बनाया जा सकता है। इसी प्रकार कुल Employees की संख्या दिखाने के लिए Static Member Function का उपयोग किया जा सकता है। इससे Memory की बचत होती है तथा Common Information को Centralized तरीके से Manage किया जा सकता है।


Applications of Static Data Members & Static Member Functions

Static Data Members तथा Static Member Functions का उपयोग आधुनिक Object Oriented Programming में Common Information को Manage करने तथा Class Level Operations करने के लिए किया जाता है। ये Features Memory Saving, Better Organization तथा Efficient Programming में महत्वपूर्ण भूमिका निभाते हैं।

Application Purpose
Student Management System Total Students की संख्या Maintain करना।
Banking Software Common Interest Rate Store करना।
Employee Management Company Name तथा Total Employees Store करना।
Inventory Management Total Products एवं Common Tax Rate Maintain करना।
Configuration Management Application की Global Settings Store करना।
Game Development Global Score, Total Players तथा Game Settings Maintain करना।

Advantages of Static Data Members

  • केवल एक Memory Copy होने के कारण Memory की बचत होती है।
  • Common Information सभी Objects के बीच Share होती है।
  • Object Counter Implement करना आसान हो जाता है।
  • Class Level Data को Efficient तरीके से Manage किया जा सकता है।
  • Program की Performance बेहतर होती है।
  • Code Duplication कम होती है।
  • Large Applications में Common Data Maintain करना सरल हो जाता है।

Advantages of Static Member Functions

  • Object Create किए बिना Function Call किया जा सकता है।
  • Class Level Operations आसानी से किए जा सकते हैं।
  • Static Data Members को सुरक्षित तरीके से Access किया जा सकता है।
  • Utility Functions बनाने में अत्यंत उपयोगी है।
  • Code Organization बेहतर होती है।
  • Performance एवं Maintainability दोनों में सुधार होता है।
  • Factory Methods तथा Configuration Methods विकसित करने में उपयोगी है।

Limitations of Static Members

  • Static Member Function सीधे Non-Static Members को Access नहीं कर सकता।
  • this Pointer उपलब्ध नहीं होता।
  • Shared Data में परिवर्तन सभी Objects को प्रभावित करता है।
  • Improper Design से Tight Coupling उत्पन्न हो सकती है।
  • Object Specific Information के लिए Static Members उपयुक्त नहीं हैं।
  • गलत उपयोग Program की Flexibility को कम कर सकता है।

Best Practices

  • केवल Common Data को ही Static घोषित करें।
  • Object Specific Variables को कभी Static न बनाएं।
  • Static Data Member को उचित Initial Value दें।
  • Static Members को Class Name द्वारा Access करना बेहतर Practice है।
  • Static Member Functions का उपयोग केवल Class Level Operations के लिए करें।
  • Shared Data को Modify करते समय सावधानी रखें।
  • Program की Readability तथा Maintainability बनाए रखें।

Real Life Example

मान लीजिए किसी University Management System में सभी Students के लिए University Name समान है। इसे Static Data Member के रूप में Store किया जा सकता है। इसी प्रकार कुल Registered Students की संख्या Display करने के लिए Static Member Function का उपयोग किया जा सकता है। इससे प्रत्येक Object में अलग-अलग University Name Store करने की आवश्यकता समाप्त हो जाती है तथा Memory की बचत होती है।


Working of Static Members

Class



Static Data Member



Single Shared Memory



Static Member Function



Access by Class Name

Summary

Static Data Members तथा Static Member Functions C++ के महत्वपूर्ण Features हैं जिनका उपयोग Class Level Information को Manage करने के लिए किया जाता है। Static Data Member की केवल एक ही Memory Copy होती है जिसे Class के सभी Objects Share करते हैं। Static Member Function किसी Object से संबंधित नहीं होता तथा इसे सीधे Class Name द्वारा Call किया जा सकता है। यह केवल Static Data Members एवं अन्य Static Member Functions को सीधे Access कर सकता है। Memory Saving, Object Counting तथा Common Configuration जैसी आवश्यकताओं के लिए Static Members अत्यंत उपयोगी होते हैं।

Concept Description
Static Keyword Class Level Members बनाने के लिए उपयोग किया जाता है।
Static Data Member पूरी Class के लिए केवल एक Memory Copy वाला Data Member।
Static Member Function केवल Static Members को सीधे Access करने वाला Function।
Memory Allocation केवल एक बार Memory Allocate होती है।
Access Method Class Name (::) द्वारा Access किया जा सकता है।
this Pointer Static Member Function में उपलब्ध नहीं होता।
Applications Object Counter, Configuration, Banking, Student एवं Employee Management।
Advantages Memory Saving, Shared Data एवं Better Performance।

Important Viva Questions

  1. Static Keyword क्या है?
  2. Static Data Member क्या होता है?
  3. Static Data Member की Definition Class के बाहर क्यों लिखी जाती है?
  4. Static Member Function क्या है?
  5. क्या Static Member Function Non-Static Data Members को सीधे Access कर सकता है?
  6. Static Member Function में this Pointer क्यों उपलब्ध नहीं होता?
  7. Static Data Member तथा Normal Data Member में क्या अंतर है?
  8. Static Member Function तथा Normal Member Function में क्या अंतर है?
  9. Object Counter बनाने में Static Data Member का क्या उपयोग है?
  10. Static Members के मुख्य Advantages क्या हैं?

University Examination Important Questions

  1. Static Data Member क्या है? Syntax एवं Program सहित समझाइए।
  2. Static Member Function क्या है? उदाहरण सहित समझाइए।
  3. Static Data Member तथा Normal Data Member में अंतर लिखिए।
  4. Static Member Function तथा Normal Member Function में अंतर लिखिए।
  5. Static Keyword की आवश्यकता स्पष्ट कीजिए।
  6. Object Counter Program Static Data Member का उपयोग करके लिखिए।
  7. Static Members के Applications, Advantages तथा Limitations लिखिए।
  8. Static Member Function के Rules लिखिए।
  9. Class Level Data Management में Static Members की भूमिका स्पष्ट कीजिए।
  10. Memory Saving में Static Data Members का योगदान समझाइए।

Exam Tips

  • हमेशा याद रखें कि Static Data Member की केवल एक ही Memory Copy होती है।
  • Static Data Member की Definition Class के बाहर लिखी जाती है।
  • Static Member Function को ClassName::FunctionName() द्वारा Call किया जा सकता है।
  • Static Member Function में this Pointer उपलब्ध नहीं होता।
  • Static Member Function सीधे केवल Static Data Members को Access कर सकता है।
  • Object Counter Program RGPV Practical एवं Viva में अक्सर पूछा जाता है।
  • Difference आधारित प्रश्न जैसे Static vs Normal Data Member तथा Static Member Function vs Normal Member Function Exam में अत्यंत महत्वपूर्ण हैं।

Conclusion

Static Data Members तथा Static Member Functions C++ Object Oriented Programming के अत्यंत उपयोगी Features हैं जो Common Data को Efficient तरीके से Manage करने तथा Memory की बचत करने में सहायता करते हैं। Static Members के माध्यम से सभी Objects एक ही Shared Information का उपयोग कर सकते हैं, जबकि Static Member Functions Object बनाए बिना Class Level Operations Execute कर सकते हैं। Banking Systems, Student Management Systems, Employee Management Systems, Inventory Software तथा Enterprise Applications में इनका व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical तथा Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।

Related Articles

Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year

Arrays ...

Read More →

Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year

Object ...

Read More →

Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year

Pointer...

Read More →

Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year

Dynamic...

Read More →

this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More →