🟣 OOP · Lesson 43
Static Data Members and Static Functions
Static Data Members and Static Functions
What is Static Data Members and Static Functions?
Static Data Members and Static Functions
Static members belong to the class rather than individual objects. They are shared by all objects of the class.
Static members belong to the class rather than individual objects. They are shared by all objects of the class.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
static-members.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| static member | Static Data Members and Static Functions programming में Static member use होता है। |
| class variable | Static Data Members and Static Functions programming में Class variable use होता है। |
| shared value | Static Data Members and Static Functions programming में Shared value use होता है। |
| static function | Static Data Members and Static Functions programming में Static function use होता है। |
Syntax / Pattern
C++
static data_type member;
Example Program
main.cpp
#include <iostream>
using namespace std;
class Student{
public:
static int count;
Student(){ count++; }
};
int Student::count = 0;
int main(){ Student a,b,c; cout << Student::count; }Expected Output
3
Program Explanation
- count is shared by all objects.
- Constructor increases count whenever an object is created.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- object counting
- shared configuration
- factory methods
Common Mistakes
- Forgetting to define static data member outside class.
- Accessing non-static data from static function directly.
Practice Tasks
- Count number of objects created.
- Create static schoolName member.
Summary
Static Data Members and Static Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
Static Data Members and Static Functions क्या है?
Static Data Members and Static Functions
Static members belong to the class rather than individual objects. They are shared by all objects of the class.
Static members belong to the class rather than individual objects. They are shared by all objects of the class.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
static-members.cppमुख्य फोकस
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
इसे क्यों सीखें?
- यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
- यह school practicals, projects और competitive programming में बार-बार use होता है।
- यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।
ज़रूरी Terms
| Term | अर्थ / उपयोग |
|---|---|
| static member | Static member Static Data Members and Static Functions programming में use होता है। |
| class variable | Class variable Static Data Members and Static Functions programming में use होता है। |
| shared value | Shared value Static Data Members and Static Functions programming में use होता है। |
| static function | Static function Static Data Members and Static Functions programming में use होता है। |
Syntax / Pattern
C++
static data_type member;
Example Program
main.cpp
#include <iostream>
using namespace std;
class Student{
public:
static int count;
Student(){ count++; }
};
int Student::count = 0;
int main(){ Student a,b,c; cout << Student::count; }Expected Output
3
Program Explanation
- count is shared by all objects.
- Constructor increases count whenever an object is created.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- object counting
- shared configuration
- factory methods
आम गलतियाँ (Common Mistakes)
- Forgetting to define static data member outside class.
- Accessing non-static data from static function directly.
अभ्यास (Practice Tasks)
- Count number of objects created.
- Create static schoolName member.
सारांश
Static Data Members and Static Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.