Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Static Members of C++ Classes

Static Members of C++ Classes

C++ Classes & Objects C++ Classes & Objects

We can use the static keyword to define a class member as static. When we declare a class member as static, it means that no matter how many objects of the class are created, the static member has only one copy.

Static members are shared among all objects of the class. If there is no other initialization statement, all static data will be initialized to zero when the first object is created. We cannot place the initialization of static members inside the class definition, but we can reinitialize the static variable outside the class using the scope resolution operator :: to reinitialize the static variable, as shown in the example below.

The example below helps to better understand the concept of static member data:

Example

#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

;

// Increment by 1 each time an object is created

objectCount

++;

}

double

Volume

(

)

{

return

length

breadth

height

;

}

private

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

}

;

// Initialize the 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

// Output the total number of objects

cout

<<

"

Total objects:

"

<<

Box

::

objectCount

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Constructor called.

Constructor called.

Total objects: 2

Static Member Functions

If a function member is declared as static, the function can be made independent of any particular object of the class. A static member function can be called even when no class object exists;static functionas long as you use the class name plus the scope resolution operator :: to access it.

A static member function can only access static member data, other static member functions, and other functions outside the class.

A static member function has a class scope and cannot access the class's this pointer. You can use a static member function to determine whether certain objects of the class have been created.

Differences between static member functions and ordinary member functions:

  • A static member function has no this pointer and can only access static members (including static member variables and static member functions).
  • An ordinary member function has a this pointer and can access any member of the class; whereas a static member function has no this pointer.
  • The example below helps to better understand the concept of static member functions:

    Example

    #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

    ;

    // Increment by 1 each time an object is created

    objectCount

    ++;

    }

    double

    Volume

    (

    )

    {

    return

    length

    breadth

    height

    ;

    }

    static

    int

    getCount

    (

    )

    {

    return

    objectCount

    ;

    }

    private

    :

    double

    length

    ;

    // length

    double

    breadth

    ;

    // width

    double

    height

    ;

    // height

    }

    ;

    // Initialize the static member of class Box

    int

    Box

    ::

    objectCount

    =

    0

    ;

    int

    main

    (

    void

    )

    {

    // Output the total number of objects before creating objects

    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

    // Output the total number of objects after creating objects

    cout

    <<

    "

    Final Stage Count:

    "

    <<

    Box

    ::

    getCount

    (

    )

    <<

    endl

    ;

    return

    0

    ;

    }

    When the code above is compiled and executed, it produces the following result:

    
    
    Inital Stage Count: 0
    
    Constructor called.
    
    Constructor called.
    
    Final Stage Count: 2
    
    

    C++ Classes & Objects C++ Classes & Objects