Those of you who are familiar with C, have used static keyword to mean opposite of automatic. A static variable has a lifetime of complete program execution. That is to say it does not get destroyed when you leave the block where it is defined.
What is static in C++?
Static storage
Automatic variables are destroyed when a function or block in which it is defined exits where as static variables have static duration. That is they remain alive till the end of program.
And static global variables and functions have internal linkage. That is they are visible only in the file they are defined in. A non-static global variable or function has external linkage. It is visible in the entire program.
Let us discuss about static members of a class.
Static member functions:
But there is a catch. A static function can access only static data members. It can not access non-static data because it does not belong to an object. It has no "this" pointer.
In the code shown above, we
are not creating any object of A . We can still call the print()
method of class A using class name.
If in static function print(), we try to access n (a non-static member), we get a compilation error.
What is static in C++?
Static storage
Automatic variables are destroyed when a function or block in which it is defined exits where as static variables have static duration. That is they remain alive till the end of program.
And static global variables and functions have internal linkage. That is they are visible only in the file they are defined in. A non-static global variable or function has external linkage. It is visible in the entire program.
Let us discuss about static members of a class.
Static
member of a
class does
not belong to an object, but belongs to the class.
Static data member
Ordinary data members are present in each object and they are different for each object. But a static data
member
is shared by all objects of the class. There is only one copy
of static data member.
To make a data member static,
it should be defined with keyword static inside the class body.
A static data MUST also be defined outside
the body of the class because inside class, the declaration of
static
data is not definition.
class SimpleInterest { static double rate;/*static data declaration*/ int amount; float period; public: /*funnctions*/ }; double SimpleInterest::rate=0.07;/*definition*/
In the program above rate is a static data member. And it is defined
outside SimpleInterest class using scope resolution operator .
Static data members can be
accessed using dot or arrow operators like any other members but they can also be accessed without an object,
using class name followed by scope resolution operator. In the above example, we can access rate as SimpleInterest::rate.
int main() { SimpleInterest::rate=0.08; SimpleInterest obj(1000,1); float intrst = obj.amount*obj.period*obj.rate; obj.rate = 0.085; cout<<obj.rate<<" "; SimpleInterest obj2(10000,5); cout<<obj2.rate; }
Output:
0.085 0.085
In the example above, we are accessing rate using :: operator and dot operator. In first line of main() we accessed rate without creating any object.
we are assigning a value to rate using ::. Then we are setting the value of rate using obj. When obj2.rate is printed, we see the same value 0.085.
This shows that, unlike other data members, static members are shared by all objects of a class. In this case, rate is one integer which is shared by all objects of SimpleInterest class.
we are assigning a value to rate using ::. Then we are setting the value of rate using obj. When obj2.rate is printed, we see the same value 0.085.
This shows that, unlike other data members, static members are shared by all objects of a class. In this case, rate is one integer which is shared by all objects of SimpleInterest class.
Try it out : Find out the size of a class with a static data member in it using sizeof() operator Does the size include this static member?
Static data member can not be
mutable.
Static member functions:
A
static member function is a method which can be invoked
without using any object. Static function can
be called directly using class name and
scope resolution operator.
But there is a catch. A static function can access only static data members. It can not access non-static data because it does not belong to an object. It has no "this" pointer.
A function is made static by
using static keyword along with function declaration within the class
body.
Rules for static functions.
- A static function can not use "this" pointer. Hence it can not access non-static data members.
- A static function can not be const
- Static function can not be virtual
class A { int n; static int m; public: A(int m); static void print() ;/*static function*/ }; int A::m=4; void A::print() { cout<<"Hello world"; //cout<<n;/*error. Can not access n*/ cout<<m;/*no error. m is static*/ } int main() { A::print(); }
If in static function print(), we try to access n (a non-static member), we get a compilation error.
Let us look at an example where a static function will help us to create objects even if we have only private constructor to a class
#include <iostream> using namespace std; class A { int m; A(int n);/*private ctor*/ static A * instance; public: static A* getInstance(int m); int getm() { return m; } }; A* A::instance=0; A::A(int n):m(n) { } A* A::getInstance(int m) { if(instance == 0) { instance = new A(m); } return instance; } int main() { A *aptr = A::getInstance(10); cout<<aptr->getm(); A *anotherptr = A::getInstance(20); cout<<anotherptr->getm(); }
In this example class A has only a private constructor. So statement of the type A obj; outside of class will not compile because the call can not access the constructor. Instead we are having a static function getInstance() whcih creates a new object dynamically and returns its address.
The code also returns existing pointer if there is already an object of the class. So we can only create one object of this class - it is a singleton class.
You can get these notes and also programs, quiz on C++ by downloading the app Simplified C++ by Hegdeapps
You can get these notes and also programs, quiz on C++ by downloading the app Simplified C++ by Hegdeapps
Comments
Post a Comment