As we know that For Accessing anything means Data Members and Member Functions from a Class, we must have to Create an Object First, So that When we Create an Object of Class, then all the Data of Class will be Copied into an Object so that this will Consume Some Memory. So that double Memory will be used for Storing the Data of single Class. First, Memory used by the Class for storing his data and Second by Object. So that for Removing the Problem for storing the data and Member Functions twice. We use the Static and Static Member Functions.
A static data member(s) is the data member(s) that gets memory only once for the whole class, no matter how many object(s) of a class is created. All objects of its class share this common copy. The static data member is declared within the class by prefixing the data member declaration in the class body with a keyword static and Static Data Members always have Default values as \0 for int and Null for Strings and needs to be defined explicitly outside the class using the scope resolution operator, which identifies the class to which it belongs. They will Never Store any Garbage values. Always remember that Static Data Members are always used in the Static Member Functions. If a Member Functions wants to use Static Data, then we must have to declare that Member Function as Static. And the Static Data Members are always Assigned Some values from the outside from the Class. The syntax for declaring and defining static data member of a class is
class CLASSNAME { ................. static datatype DATAMEMBER; }; datatype CLASSNAME::DATAMEMBER = Intialvalue;
The above syntax specifies how the static data member is declared and defined. They are initialized during their definition outside the class explicitly. But unlike normal variables and data members, they are initialized to 0 by default. The static data members are declared and defined separately because all the data members (static or non-static) must be declared inside the class, as class serves as a blueprint for creating objects and doesn’t allocate any memory. So unlike normal data members that get memory (defined) corresponding to their objects, the static data members who are not associated with any object needs to be defined explicitly outside the class.
Since the static data members are associated with the class and not with any object, they are also known as class variables. Thus, by defining a static data member outside the class, we allocate storage for it and link it to the class to which it belongs. The static data members remain in memory even though no object of a class is created.
Static data members may look like a global variable, but this is not true as it can be a private member, whereas a global variable cannot. The static data member(s) obeys the private, public and protected access rules similar to normal data members of classes and can be accessed within the member function of its class without using the dot(.) operator.
To understand the static data members, let us consider a problem that maintains account information of multiple customers by updating the balance periodically with the same interest. To simulate it, we create an accounting class containing private data members like account number, balance, rate of interest etc. To represent individual customer, we need to create an object. Each object will store all its data members in a separate memory location. We have different account numbers and balances for every account object, but the interest rate for all the account objects is the same. So allocation of separate memory to data member rate of interest for all the objects will cause the following problems.
a) Wastage of memory space will occur because the same value of the interest rate is maintained by all the objects of the account class.
b) If the value of the rate of interest changes over time, then each copy would have to be updated that can lead to inefficiency, wastage of time and greater potential of errors.
To overcome this problem, we need to store the rate of interest only once in memory which can be shared by all the objects of the same class. This problem can be solved by declaring the rate of interest as a global variable. But this may lead to accidental modification as it always violates the date hiding principle and leads to undesirable effects on the efficiency and reliability of the program. In such cases, declaring the data member rate of interest as static in the account class provides a better solution.
#include<iostream.h> #include<conio.h> class account { private: int acc_no; double balance; static double rate; //static data member declaration public: void read() { cout<<"\nEnter account no. and balance = "; cin>>acc_no>>balance; } void show{) { cout<<"\nAccount no = "<<acc_no<< '\t'; cout<<''Interest = "<<rate<<'\t'; cout<<''Balance ="<<balance; } void qtr_rate_cal() { double interest=(balance*rate*0.25); balance=balance+interest; } }; double account::rate=0.05; //static data member definition int main() { clrscr(); account a1,a2; cout<<"Enter customer 1 information ..... \n"; a1.read(); cout<<"Enter customer 2 information ..... \n"; a2.read(); a1.qtr_rate_cal(); a2.qtr_rate_cal(); a1.show(); a2.show(); getch(); return 0; } Output : Enter customer 1 information ..... Enter account no. and balance = 201 1000 Enter customer 2 information ..... Enter account no. and balance = 202 2000 Account No. = 201 Interest= 0.05 Balance = 1012.5 Account No. = 202 Interest = 0.05 Balance = 2025
Explanation: The above program displays the balance of customers after calculating the Interest. It contains a account class having private data members ace_no, balance, rate and public member functions read(), show(), qtr_rate_cal(). The static data member rate is declared inside the class and defined outside. It is initialized with a value of 0.05 (5%). After inputting the account’s information for each customer, it calculates their quarterly rate of Interest and updates the balance, which is then displayed.
Properties of Static Data Members
A static data member of a class has the following characteristics :
• The static data member gets memory only once, which is shared by all the objects of its class.
• The static data members are associated with the class and not with any object, so they are also known as class variables.
• The static data members must be defined outside the class; otherwise, the linker will generate an error. Also, there is only one definition of a static data member.
• The static data member follow the public, private and protected access rules. The private static data members are accessed within the class in the same way as normal private data members. The public static data members can be accessed throughout the program using the name of the class, followed by the scope resolution operator and then the static data member itself.
account::rate=0.07;
• Unlike non-static data members, a static data member, can appear as a default argument to the member function of the class.
• A static data member exists before any object of a class is created.
• The static data member is similar to the normal static variable. The main difference is that a normal static variable is used to retain information between function calls. In contrast, static data members are used to sharing information among multiple objects of a class.
Static Member Functions
The Static Member Functions are those which are declared by using the Static in Front of the Member Function. It is possible to have static member functions in a class in the same way as static data members. The static member function(s) is similar to the normal member function(s) of a class, but the only difference is that it can access only static member(s) (data or functions) declared in the same class. In other words, we cannot access non-static members inside the definition of a static member function. The declaration of a static member function(s) is the same as that of non-static member functions(s) except that the function declaration in the class body must be preceded with the keyword static and the function definition that appears outside the class body must not be specified with the keyword static. If the member function is defined inside the class body, then the function header must be preceded by keyword static.
The public static member function can be accessed from outside the class in either of the following ways.
a) Using scope resolution operator as
CLASSNAME::static_mem_function_name();
b) or through the object of a class as
OBJECTNAME.static_mem_function_name();
Calling a static member function using the object of its class is not recommended because none of the object’s data member (non-static) is ever accessed or modified at all. It merely creates confusion, as a programmer thinks he is accessing and manipulating an object’s data since static member function(s) manipulates only static member(s) which are not a part of any object and are associated with the entire class, so it is better to use the classname with the scope resolution operator for accessing static members.
To understand the static member function concept, let us consider the same example as we discussed in static data members. If the bank decides to change the interest rate for all its customers, then we need to make a function in the class that update it. It can be illustrated in the following program.
#include<iostream.h> #include<conio.h> class account { private: int acc_no; double balance; static double rate; public: void read() { cout<<"Enter account number and balance->"; cin>>acc_no>>balance; } void show(){ cout<<"\nAccount no = "<<acc_no; cout<<" Interest = '"'<<rate; cout<<" Balance = "<<balance; } void qtr_rate_cal () { double interest=(balance*rate*0.25)/100; balance = balance + interest; } static void modify_rate(double incr) { rate = rate + incr;//modifying rate cout<<"\n Modified Rate of Interest = "<<rate; } };//end of class specification double account::rate=0.05; int main() { clrscr(); account a1,a2; a1.read(); a2.read(); account::modify_rate(0.01); //calling static mem.func. a1.qtr_rate_cal(); a2.qtr_rate_cal(); a1.show(); a2.show(); getch(); return 0; }
Explanation: The statement account:: modify_rate(0.01); calls a static members function modify_rate() using the class name account instead of object name. It modifies the rate of interest by the value specified as an argument (0.01). The function modifies rate() is made static in the class as we are not using non-static data members acc_no and balance in its definition.