STATIC MEMBERS IN C++

Introduction

Before getting into the topic we must know what STATIC is,
Most c++ keywords allow you to do one thing. You use it to declare an int, or a function returns an int or expects an int as an argument .you use new to allocate memory, and delete to free memory. you use constantly to indicate that the value of a variable cannot be changed. Ironically, the keyword static, through the word means “unchanging” has multiple uses. the keyword static can be used in three major contexts: inside a function, inside a class definition, and in front of a global variable inside a file making up multiple programs.
The use of static inside a function is the simplest. It simply means that once the variable has been initialized, it remains in memory until the end of the program, You can think of it as saying that the variable sticks around, Maintaining its value, Until the program completely ends. For instance, you can use a static variable to record the number of times a function has been called simply including the lines static int count=0;
And count++; inside the function. Because the count is a static variable, The line “static int count=0:” will only be executed once. Whether the function is called, the count will have the last value assigned to it.
You can also use static in this fashion to prevent a variable from being reinitialized inside a loop.

See also  HISTORY OF C & C++

WHAT IS A STATIC KEYWORD IN C++?

· Static keyword meaning differs when used with different types of data

· SYNTAX: class _name :: function_name (parameter);

The static keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member

TYPES OF STATIC MEMBERS:

1. STATIC VARIABLES

As the variables declared as static are initialized only once as they are allocated space in separate static storage, the static variables in a class are shared by the objects. There can not be multiple copies of the same static variables for different objects. Also because for this reason, static variables can not be initialized using constructors.

2. STATIC MEMBERS OF CLASS

Just like variables, objects also when declared as static have a scope till the lifetime of the program.

 Inside a class definition, the keyword static declares members that are not bound to class instances. Outside a class definition, it has a different meaning 

//Or else we can call them “Data members” and “member function”

Now we will see what is data member and member function

STATIC DATA MEMBER:

· Whenever we declare a data member as static either inside or outside of a class it is called a static data member
· There is only one copy of static data member even if there are many class objects
· it is always initialized with zero because its default value is zero
· It is shared memory for all objects of the class
· It retains its values]
· If you are calling a static data member within a member function, the member function should be declared as static (i.e. a static member function can access the static data members)

See also     C++ CLASSES AND OBJECTS

DECLARATION:

Static data_type member_name;

DEFINING THE STATIC DATA MEMBER:

It should be defined outside of the class following this syntax:

Data_type class_name :: member_name = value;

STATIC MEMBER FUNCTION:

· If we create a member function of a class as a static it is called a static member function
· It can be accessed only as static data members
· It can be accessed when we don’t have any kind of an object of a class
LET’S HAVE A EXAMPLE CODE OF IMPLEMENTING STATIC DATA MEMBER AND FUNCTION

//PROGRAM FOR STATIC DATA & FUNCTION IN C++ EXP-1
#include <iostream>

using namespace std;

class Demo
{
 private: 
  //static data members
  static int X;
  static int Y;

 public:
 //static member function
 static void Print()
 {
  cout <<"Value of X: " << X << endl;
  cout <<"Value of Y: " << Y << endl;
 }
};

//static data members' initializations
int Demo :: X =10;
int Demo :: Y =20;


int main()
{
 Demo OB;
 //accessing class name with object name
 cout<<"Printing through object name:"<<endl;
 OB.Print();
 
 //accessing class name with class name
 cout<<"Printing through class name:"<<endl;
 Demo::Print();
 
 return 0;
}

OUTPUT:

Printing through object name:
Value of x: 10
Value of y: 20
Printing through class name:
Value of x: 10
Value of y: 20

In the above program X and Y are two static data members and print() is a static member function. According to the rule of static in C++, only the static member function can access static data members. Non-static data members can never be accessed through static member functions.

//PROGRAM FOR STATIC DATA & FUNCTION IN C++

#include <iostream>
using namespace std;

// define a Test class.
class Test{
  // define static function.
  public: 
  static void print_age(){
    cout << "Age : " << age;
  }
  
  // define static property.
  static int age;
};

// now initialize age value.
int Test::age = 25;

int main() {
  // you can access the static method using :: colon.
  Test::print_age();
  
  // you can also change the value.
  Test::age = 30;
  cout << endl;
  Test::print_age();
  return 0;
}

OUTPUT:

Age : 25
Age : 30

Only a single copy of static members is made, no matter how many class objects we create. The value of static members remains the same for all objects. However, you will understand this better later.
Keep in mind that you cannot initialize static properties while defining them. You only have to define static properties, not initialize, if you do this then an error will be generated.
static int age = 90;
error: ISO C++ forbids in-class initialization of non-const static member ‘Test::age’
Since you access static members without making them a class object, the constructor does not even run on accessing them. Nor can you call a non-static method inside a static method, because a class object is required to call a static method, and static methods are not in the context of the object, nor do you create a class object inside it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
100% Free SEO Tools - Tool Kits PRO