Static members of a C++ class
http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm
We
can define class members static using static keyword. When we declare a member of a
class as static it means no matter how many objects of the class are created,
there is only one copy of the static member.
A
static member is shared by all objects of the class. All static data is
initialized to zero when the first object is created, if no other
initialization is present. We can't put it in the class definition but it can
be initialized outside the class as done in the following example by
redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Let us try the following
example to understand the concept of static data members:
#include <iostream>
using namespace std;
class Box
{
public:
static int objectCount;
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}
When the above code is
compiled and executed, it produces following result:
Constructor called.
Constructor called.
Total objects: 2
Static
Function Members:
By
declaring a function member as static, you make it independent of any
particular object of the class. A static member function can be called even if
no objects of the class exist and the static functions are accessed using only the
class name and the scope resolution operator ::.
A static member function can
only access static data member, other static member functions and any other
functions from outside the class.
Static
member functions have a class scope and they do not have access to the this pointer of the class. You could use a
static member function to determine whether some objects of the class have been
created or not.
Let us try the following
example to understand the concept of static function members:
#include <iostream>
using namespace std;
class Box
{
public:
static int objectCount;
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void)
{
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}
When the above code is
compiled and executed, it produces following result:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
C++
Static Functions
http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-static-functions.html
Static
member functions have a class scope and they do not have access to the 'this'
pointer of the class. When a member is declared as static, a static member of
class, it has only one data for the entire class even though there are many
objects created for the class. The main usage of static function is when the
programmer wants to have a function which is accessible even when the class is
not instantiated.
Defining Static Function
Static
function is defined by using the keyword static before the member function that
is to be declared as static function.
Syntax
Sample
Code
1. static return_data_type function_name()
2. //Static function defined with
keyword static
3. {
4. statement1;
5. //Statements for execution
inside static function
6. statement2;
7. ...
8. ...
9. }
Copyright
exforsys.com
For
example if a function exforsys returning nothing is to be declared as staic
function it is done as follows:
Sample
Code
1. static void exforsys()
2. {
3. ... ;
4. ... ;
5. }
Copyright
exforsys.com
Accessing Static Function
A
normal member function is accessed using the object and an operator called the
dot member access operator. The functions declared static or static functions
are accessed using only the class name and the scope resolution operator,
unlike in normal member functions where these are not used.
Static Function Example
The
declaration of static member function and how to access static member function:
Sample
Code
1. #include <iostream>
2. using namespace std;
3. class example
4. {
5. private:
6. static int sum; //Static data
7. int x;
8. public:
9.
example() //Constructor of the class
10. {
11. sum=sum+1;
12.
x=sum;
13. }
14.
~example() //Destructor of the class
15. {
16.
sum=sum-1;
17. }
18. static void exforsys()
19. //Static function exforsys( ) defined with keyword
static
20. {
21.
cout << "nResult is: " << sum;
22. }
23. void number() //Normal member function number( )
24. {
25.
cout << "nNumber is: " << x;
26. }
27. };
28. int example::sum=0;
29. void main()
30. {
31.
example e1;
32.
example::exforsys();
33. //Static function exforsys() accessed using class
name example and the scope resolution operator ::
34.
example e2,e3,e4;
35.
example::exforsys();
36. e1.number();
37. //Normal member function accessed using object e1
and the dot member access operator.
38. e2.number();
39. e3.number();
40. e4.number();
41. }
Copyright
exforsys.com
The
output of the above program is:
In
the above example, the function exforsys() is defined as static function and
the integer data type sum is declared as static data type. Four objects e1, e2,
e3 and e4 are created for the class example. The constructor of the class
example increments the sum by 1 and the destructor of the class decrements sum
by 1.
The
static function is accessed using the class name example and the scope
resolution operator :: as
example::exforsys();
But
the normal member function number() is accessed using the object name and the
dot member access operator as
Sample
Code
1. e1.number()
2. e2.number()
3. e3.number()
4. e4.number()
Copyright
exforsys.com
The
first time the static function exforsys() is called, there was one object
created and thus, the sum is incremented by 1 in the constructor printing the
result of sum as 1.When the static function exforsys() is called the second
time, there were three more objects e2,e3 and e4 created which results in the
sum incremented thrice from 1 in the constructor of the corresponding class
example, resulting in the value of sum as 4, which is displayed in the second
result. Applying the above explanation, it is clear that the static function
operates on the class and not in object. To access static function the
programmer can use the class name, followed by the scope resolution operator,
as seen in example above.
Things to keep in mind while using static
member functions
1. A
static member function can only access static member data, static member
functions and data and functions outside the class.
2.
You must take note not to use static member function in the same manner as
non-static member function, as non-static member function can access all of the
above including the static data member.
3. A
non-static member function can be declared as virtual but care must be taken
not to declare a static member function as virtual. v
4.
The programmer must first understand the concept of static data while learning
the context of static functions. It is possible to declare a data member of a
class as static irrespective of it being a public or a private type in class
definition. If a data is declared as static, then the static data is created
and initialized only once. Non-static data members are created again and again.
For each separate object of the class, the static data is created and
initialized only once. As in the concept of static data, all objects of the
class in static functions share the variables. This applies to all objects of
the class.
5. A
non-static member function can be called only after instantiating the class as
an object. This is not the case with static member functions. A static member
function can be called, even when a class is not instantiated.
No comments:
Post a Comment