0% found this document useful (0 votes)
6 views2 pages

Exp 3

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Uploaded by

8177.sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Exp 3

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Uploaded by

8177.sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program – 3

Aim – WAP to implement static members.


Description of aim and Related theory –
A data member of a class can be qualified as static. A static member variable has certain special characteristics.
It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one
copy of that member is created for the entire class and is shared by all the objects of that class, no matter how
many objects are created. It is visible only within the class, but its lifetime is the entire program. Static variables
are normally used to maintain values common to the entire class.

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?

You might also like