This topic is about D Programming – Static members of a class.
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. You cannot 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 −
import std.stdio; class Box { public: static int objectCount = 0; // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); 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 }; void main() { Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects. writeln("Total objects: ",Box.objectCount); }
When the above code is compiled and executed, it produces the 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 −
import std.stdio; class Box { public: static int objectCount = 0; // Constructor definition this(double l = 2.0, double b = 2.0, double h = 2.0) { writeln("Constructor called."); 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 }; void main() { // Print total number of objects before creating object. writeln("Inital Stage Count: ",Box.getCount()); Box Box1 = new Box(3.3, 1.2, 1.5); // Declare box1 Box Box2 = new Box(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects after creating object. writeln("Final Stage Count: ",Box.getCount()); }
When the above code is compiled and executed, it produces the following result −
Inital Stage Count: 0 Constructor called. Constructor called Final Stage Count: 2
In this topic we learned about D Programming – Static members of a class. To know more, Click Here.
Pingback: D Programming - Classes & Objects - Adglob Infosystem Pvt Ltd