Exp 3
Exp 3
Algorithm –
1.) Declaration of class using ‘class keyword’. Give the class a name.
2.) Use the static keyword before declaring the data member. Declare the static data member.
3.) Access the data member using scope resolution (::) operator.
4.) Static data members can be accessed without creating an object as they belong to class itself.
Code –
Output –
Discussion –
Note that the type and scope of each static member variable must be defined outside the class definition. This is
necessary because the static data members are stored separately rather than as a part of an object. Since they are
associated with the class itself rather than with any class object, they are also known as class variables. The
objects of a class share the static data member.
Static variables are like non-inline member functions as they are declared in a class declaration and defined in
the source file. While defining a static variable, some initial value can also be assigned to the variable.
Finding/Learning –
What are static data members? How do they work? How to declare them and access them?