🟣 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 member used in Static Data Members and Static Functions programming.
class variableClass variable used in Static Data Members and Static Functions programming.
shared valueShared value used in Static Data Members and Static Functions programming.
static functionStatic function used in Static Data Members and Static Functions programming.

Syntax / Pattern

static data_type member;

Example Program

#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 is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

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 member used in Static Data Members and Static Functions programming.
class variableClass variable used in Static Data Members and Static Functions programming.
shared valueShared value used in Static Data Members and Static Functions programming.
static functionStatic function used in Static Data Members and Static Functions programming.

Syntax / Pattern

static data_type member;

Example Program

#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 is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial