1 / 13

Static Data Members And Static Member Functions

Static Data Members And Static Member Functions. Topics to be discussed. Static Data Members Example of Static Data Member Static Member functions Example of Static Member Function. Static Data Members.

Télécharger la présentation

Static Data Members And Static Member Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Static Data Members And Static Member Functions

  2. Topics to be discussed • Static Data Members • Example of Static Data Member • Static Member functions • Example of Static Member Function

  3. Static Data Members are data objects that are common to all objects of a class.they exist only once in all objects of this class and are used when the information is to be shared BACK 3 It is initialized to zero when the first object of its class is created. There is exactly one copy of a static member for the entire class and is shared by all the objects of that class. It is visible only within the class,but its lifetime is the entire program. When a static data member is declared private,the non-member functions can not access this member. Static variables are normally used to maintain values commom to the entire class.For example,a static data member can be used as a counter that records the occurrences of all the objects.

  4. Here above in the program int item::count; is the definition of the Static Data Member • The Static Variable count is initialized to when the objects are created.The count is incremented whenever the data is read into an object.Since the data is read into objects three times,the variable count is incremented three times because there is only one copy of count shared by all the three objects and all the three output statements cause the value 3 to be displayed. BACK

  5. Static Member functions BACK 8 • It is the special member function that is declared static hasthe following features: • It cannot access non-static data members or non-static member functions of the class (because the object may not exist when the function is called) • A static function can have access to only other static members(functions or variables) declared in the same class. • static data members and static member functions exist independently of any objects of a class, i.e., when a static member function is called, there might not be any objects of its class in memory • A static member function can be called using the class name(instead of its objects) as follows: classname::functionname;

  6. The Static function showcount() displays the number of objects created till that moment.A count of number of objects created is maintained by the static variable count. • The statement code=++count; is executed whenever setcode() function is invoked and the current value of count is assigned to code.Since each object has its own copy of code,the value contained in code represents a unique number of its objects BACK

  7. thanks

More Related