Explain static member functions, C/C++ Programming

Assignment Help:

Static Member Functions

All the objects of the class share static data members of a class. The example above demonstrates how to keep track of all the objects of a class which area in existence. Though, this function uses existing objects to invoke a member function getcount(), which returns the value of the static data member. What if the programme does not require to use objects to invoke this function and still the programme would like to know how many objects have been created? If there is no object how the member function is invoked? Further, as can be seen from the last output, the number of objects (count) remains similar at a given instance no matter which object is used to invoke the member function.  In fact, the use of existing objects, like in the above instance, is not an effective way to access the value of the static data member. A specific object should not be used to refer to this member, as it does not belong to that object; it belongs to the whole class. C++ gives a facility to define static function members, for the similar. That is, to invoke such a function, an object is not needed. It can be invoked with the name of the class. The programme given below illustrates its use.

 

                class counter

                 {

                  public :

                                                counter ();

                                                static int getcount();

 

                  private:

                                static int count;

                };

counter::counter ()

                 {

                                count++;

                 }

 

                int counter::getcount()

                 {

                                 }

 

int counter :: count = 0;        // INITIALIZATION OF  

                                    STATIC MEMBER.

void main()

                 {

                  counter c1,c2;

                                cout << " Count = " << counter :: getcount() << endl;

                                cout << " Count = " << counter :: getcount() << endl;

                  counter c3;

                cout << " Count = " << counter :: getcount() << endl;

 

                  counter c4,c5;

 

                                cout << " Count = " << counter :: getcount() << endl;

                                cout << " Count = " << counter :: getcount() << endl;

                  }

Output:

                Count = 2

                Count = 2

                Count = 3

                Count = 5

                Count = 5

 

 


Related Discussions:- Explain static member functions

File Input and Output, Given a bool variable isReadable write some statem...

Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isR

Decodethecode, 6999066263304447777077766622337778 -----> message sent by th...

6999066263304447777077766622337778 -----> message sent by the first smuggler. my name is robert---------> message decoded by the second smuggler. Where ‘0’ denotes the "space".

C++ project, project on business management

project on business management

Vision based simultaneous mapping and localization, Project Description: ...

Project Description: Design and prepare software that can navigate a mobile robot using SLAM technique using vision sensor (camera). Skills required are C Programming, Engine

Stack push pop, 2 flowcharts, pseudocode for each (at least 4 algorithms), ...

2 flowcharts, pseudocode for each (at least 4 algorithms), and code for either a stack or a queuestion..

Merge sort, Write a program in C language to implement Two-Way Merge Sort. ...

Write a program in C language to implement Two-Way Merge Sort. Input the following data to the program. Show all intermediate steps: 84, 83, 78,90,23,123,98,159,8,200

Differentiate between functions getch () and getche (), Differentiate betwe...

Differentiate between functions getch () and getche (). - Both functions accept a character input value from user. - When getch () is used, key that was pressed won't appear

Explain the process of using the constructor, Using the Constructor The...

Using the Constructor There are basically three ways of creating and initializing the object. The first way to call the constructor is explicitly as :

Area under curve, progarm in c for area under curve   #include ...

progarm in c for area under curve   #include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; int numtraps;

Write a function that takes in radius and cover screen, Write a function th...

Write a function that takes in a radius and evenly covers the screen with circles of that radius. Don't attempt to draw any circles that are completely off the screen.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd