🟣 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.
Level
🟣 Object-Oriented Programming
Example File
static-members.cpp
Main Focus
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

TermMeaning / Use
static memberStatic Data Members and Static Functions programming में Static member use होता है।
class variableStatic Data Members and Static Functions programming में Class variable use होता है।
shared valueStatic Data Members and Static Functions programming में Shared value use होता है।
static functionStatic 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

  1. Count number of objects created.
  2. 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.
Level
🟣 Object-Oriented Programming
Example File
static-members.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
static memberStatic member Static Data Members and Static Functions programming में use होता है।
class variableClass variable Static Data Members and Static Functions programming में use होता है।
shared valueShared value Static Data Members and Static Functions programming में use होता है।
static functionStatic 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)

  1. Count number of objects created.
  2. Create static schoolName member.

सारांश

Static Data Members and Static Functions एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 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ें.