Skip to main content

Static Data Members and Functions

STATIC DATA MEMBERS

Objects of a class keep their own copy of data members of class. In certain situations a part of data may be same for all the objects of a class. In such a cases instead of every object keeping an individual copy of this data, it would be advantageous if one copy of the data is shared by all the objects of class, because, this would save memory space, particularly, when the common data is large. For such applications the common data may be declared as static. The static data may be declared under one of the three access specifiers, i.e. public, protected or private. A public static data member may be accessed directly through any object of the class because it is same for all the objects. Static data member declared private or protected may be accessed through the corresponding public functions of the class.

Characteristics

·         Each change affects to all the objects.
·         It is automatically initialized with zero when the first object of its class is created. No other initialization is permitted.
·         It is visible only within the class, but its lifetime is the entire program.
·         Static variables are normally used to maintain values common to the entire class.

Program 7:

#include<iostream.h>
#include<conio.h>

class data
{
         int x;
         static int y;
         public:


                     void getdata(int a, int b)
                     {
                          x=a;
                          y=b;
                     }

                     void show()
                     {
                          cout << "\n x= " << x << "\n y = " << y;
                     }
};

int data :: y;

void main()
{
data d1,d2;
d1.getdata(5,10);
d2.getdata(9,20);
d1.show();
d2.show();
getch();
}

Output:

X= 5 y=20
X=9 y=20

Note: int data :: y;           // definition of static data member

Note that the type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.

Static variables are like non-linear member functions as they are declared in a class declaration and defined in the source file. While defining a static variable, some initial values can also be assigned to the variable.

int data :: y=10;


STATIC MEMBER FUNCTIONS

A function member of a class may also be declared static provided its arguments are also declared static or if it does not access any non-static member of the class. The pointer this associated with every object of class is applicable only to non-static function members of the class. The static function members of the class do not have this pointer .The main purpose of a static member function is to access and modify static data member of the class.

Properties

·         A static function have access to only other static members (functions or variables) declared in the same class.
·         A static function can be called using the class name (instead of its objects).

Program 8:

#include<iostream.h>
#include<conio.h>

class interest
{
         int p,n;
         static float r;
                     public:
                     static void set_rate(float q)
                     {
                                 r=q;
                     }

                     void inter()
                     {
                                 cout << "\n enter the p and n ";
                                 cin >> p >> n;
                                 cout << "the rate of interest = " << (p*r*n);
                     }

}x;

float interest :: r=4.5;

void main()
{
x.inter();
interest ::set_rate(5.5);
x.inter();
getch();
}

Note: Here we can’t declare rate=4.5 in private because we can’t assign values in private section, so we need to declare “float interest :: r=4.5” outside the class.


Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Email/Password

In our  previous  article we saw how to create authentication module using Google identity provider. Now we will see how to implement Email and Password authentication using Firebase. We will use Bootstrap form to create intuitive user interface for sign-up and login using Email and Password. Step 1: Create signUp component. ng generate component signUp Step 2: Create custom form in sign-up.component.html file. The result of above code: Step 3: Add two functions for creating the user and login using Email and Password in src/app/providers/AFAuth.ts file. Step 4: Call createUserWithEmailAndPassword function in service from sign-up.component.ts. Step 5: Update routing configuration in app.module.ts  to include signUp component. Step 6: Update Login form to have Bootstrap form. The result of the above code. Step 7: Enable Email/Password component in Firebas...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...