
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Static Data Members in C++
Static data members are class members that are declared using the static keyword. There is only one copy of the static data member in the class, even if there are many class objects. This is because all the objects share the static data member. The static data member is always initialized to zero when the first class object is created.
The syntax of the static data members is given as follows −
static data_type data_member_name;
In the above syntax, static keyword is used. The data_type is the C++ data type such as int, float etc. The data_member_name is the name provided to the data member.
A program that demonstrates the static data members in C++ is given as follows −
Example
#include <iostream> #include<string.h> using namespace std; class Student { private: int rollNo; char name[10]; int marks; public: static int objectCount; Student() { objectCount++; } void getdata() { cout << "Enter roll number: "<<endl; cin >> rollNo; cout << "Enter name: "<<endl; cin >> name; cout << "Enter marks: "<<endl; cin >> marks; } void putdata() { cout<<"Roll Number = "<< rollNo <<endl; cout<<"Name = "<< name <<endl; cout<<"Marks = "<< marks <<endl; cout<<endl; } }; int Student::objectCount = 0; int main(void) { Student s1; s1.getdata(); s1.putdata(); Student s2; s2.getdata(); s2.putdata(); Student s3; s3.getdata(); s3.putdata(); cout << "Total objects created = " << Student::objectCount << endl; return 0; }
Output
The output of the above program is as follows −
Enter roll number: 1 Enter name: Mark Enter marks: 78 Roll Number = 1 Name = Mark Marks = 78 Enter roll number: 2 Enter name: Nancy Enter marks: 55 Roll Number = 2 Name = Nancy Marks = 55 Enter roll number: 3 Enter name: Susan Enter marks: 90 Roll Number = 3 Name = Susan Marks = 90 Total objects created = 3
In the above program, the class student has three data members denoting the student roll number, name and marks. The objectCount data member is a static data member that contains the number of objects created of class Student. Student() is a constructor that increments objectCount each time a new class object is created.
There are 2 member functions in class. The function getdata() obtains the data from the user and putdata() displays the data. The code snippet for this is as follows −
class Student { private: int rollNo; char name[10]; int marks; public: static int objectCount; Student() { objectCount++; } void getdata() { cout << "Enter roll number: "<<endl; cin >> rollNo; cout << "Enter name: "<<endl; cin >> name; cout << "Enter marks: "<<endl; cin >> marks; } void putdata() { cout<<"Roll Number = "<< rollNo <<endl; cout<<"Name = "<< name <<endl; cout<<"Marks = "<< marks <<endl; cout<<endl; } };
In the function main(), there are three objects of class Student i.e. s1, s2 and s3. For each of these objects getdata() and putdata() are called. At the end, the value of objectCount is displayed. This is given below −
int main(void) { Student s1; s1.getdata(); s1.putdata(); Student s2; s2.getdata(); s2.putdata(); Student s3; s3.getdata(); s3.putdata(); cout << "Total objects created = " << Student::objectCount << endl; return 0; }
- Related Articles
- Defining static members in C++
- How to initialize private static members in C++?
- When are static C++ class members initialized?
- Static Members in Ruby Programming
- What are static members of a C# Class?
- Static Data Member Initialization in C++
- What are static members of a Java class?
- What are the steps to read static members in a Java class?\n
- Can a "this" keyword be used to refer to static members in Java?\n
- Comparing enum members in C#
- Static Finger Theorem in Data Structure
- Private and Protected Members in C++
- Static vs. Non-Static method in C#
- How do I create static class data and static class methods in Python?
- Are array members deeply copied in C++?
