Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Characteristics of Static data member
3.
Example
3.1.
Output
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Static data members in C++

Author Anant Dhakad
1 upvote

Introduction

In this blog, we will learn about static data members in C++. The "static" keyword is used to declare class members as static data members. The syntax to declare static members is as follows:

static dataType dataMemberName;
You can also try this code with Online C++ Compiler
Run Code


Also see, Literals in C.Fibonacci Series in C++

Know What is Object in OOPs here in detail.

Characteristics of Static data member

  • No matter how many objects are created, only one copy of static data member is made for the entire class and shared by all the class objects.
  • It is initiated before any class object is created, even before the main function starts.
  • The lifetime of a static data member is the entire program execution time.

Example

#include<bits/stdc++.h>
using namespace std;

class Student {

private:
    int rollNo, marks;
    string name;

public:
    static int objectCount;
    Student() {
        objectCount++;
    }

    void setData(int rollNo, int marks, string name) {
        this->rollNo = rollNo;
        this->marks = marks;
        this->name = name;
    }
    void displayData() {
        cout<<"Roll Number = "<< rollNo <<endl;
        cout<<"Name = "<< name <<endl;
        cout<<"Marks = "<< marks <<endl;
        cout<<endl;
    }
};

// initializing the static variable
int Student::objectCount = 0;

int main(){
   Student s1;
   s1.setData(1070, 100, "Anant");
   s1.displayData();

   Student s2;
   s2.setData(1084, 100, "Het");
   s2.displayData();

   cout << "Total objects created = " << Student::objectCount << endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Roll Number = 1070
Name = Anant
Marks = 100

Roll Number = 1084      
Name = Het
Marks = 100

Total objects created = 2

In the above program, the student class has three data members (namely rollNo, marks and student name). The student class also has an objectCount variable which is a static data member. It counts the number of objects created of that class. Since it is a static data member, it is shared among all class object instances. 

Try and compile with online c++ compiler.

Check out this article - Compile Time Polymorphism

FAQs

1. What is Object-Oriented Programming? 
Object-Oriented Programming (OOP) is a programming paradigm in which the entire software is composed of a collection of objects that communicate with one another. A collection of data and methods that operate on that data is referred to as an object.

2. What are the main features of OOPs? 
Following are the main features of OOPs:
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction

3. What is Inheritance? 
The concept of inheritance is simple: a class is built on another class and uses the other class's data and implementation. The goal of inheritance is to reuse code.

4. What is a Namespace? 
All C++ programmes must include Namespaces. In C++, namespaces define a scope for the identifiers used in the programme.

5. What are cin and cout in C++? 
The object of the ostream class is cout. By default, the cout stream is connected to the console output device. Its main purpose is to display characters on the console screen. It's analogous to the printf function in C. The object of the istream class is cin. By default, the cin stream is connected to the console input device. Its main purpose is to extract characters from the user. In C, it's similar to scanf.

Key Takeaways

Cheers if you reached here!! 

In this article, we learned about Static data members in C++. We also saw its example program.

We hope that this blog has helped you enhance your knowledge regarding Static data members in C++ and if you would like to learn more, check out our articles on the platform Coding Ninjas Studio. Also, do check out our course on C++. Do upvote our blog to help other ninjas grow. Happy Coding!

Recommended Readings:

Live masterclass