No: 2
1.
Experiment
vision:
To
write a program on the concept of use of Static keyword.
2.
OBJECTIVE:
Program
to implement Static keyword.
3.1
THEORY:
-
Static Data Member:-
-
A data member of class can be qualified as a static. The property of static member variable is similar to that of C static variable.
Properties
of static variable:-
1)Storage:-
member
2)Default
value:-0
3)Scope:-
local
4)life-
value
persist between different function call.
-
A static member variable has certain special characteristics. These are
-
It is initialized to zero when the first object of is 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.
3.2
PROGRAM:
1)
Following
is an example which makes use of static data member that work as
counter to count the number of objects:
#include
<iostream>
using
namespace std;
class
item
{
static
int count;
int
no;
public:
void
getdata(int a)
{
no=a;
count++;
}
void
getcount()
{
cout<<"\tcount="<<count;
}
};
int
item::count;
int
main()
{
item
a,b,c;
//clrscr();
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"\nAfter
reading data\n";
a.getcount();
b.getcount();
c.getcount();
//getch();
return
0;
}
3.3
OUTPUT:
4.1
THEORY:
-
Static Member Function:-
-
A member function that is declared as static is static member function. It has following properties.
-
A static function can have access to only other static members declared in the same class.
-
A static member function can be called using the class name as follows
class_name::function-name;
4.2
PROGRAM:
2)
Following
is an example which makes use of static member function:
#include
<iostream>
using
namespace std;
class
test
{
int
code;
static
int count;
public:
void
setcode()
{
code=++count;
}
void
showcode()
{
cout<<"Object
number:"<<code<<"\n";
}
static
void showcount()
{
cout<<"count:"<<count<<"\n";
}
};
int
test::count;
int
main()
{
test
t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test
t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return
0;
}
4.3
OUTPUT:
5.
conclusion:
Thus
a
C++ program by Static data member and static member function has
been executed successfully.
6.
PRECAUTIONS:
1.
Avoid wrong keywords.
2.
Check whether all brackets are closed properly or not.
3.
Take desired output.
4.
In case of abnormal results, Contact
the teacher/instructor, repeat the
experiment,
and check the algorithm and program.
7.
REMARK:
Only
one copy of static member is created for entire class and shared by
all the object of that class.
8.
Discussion
Questions:
1)
How to declare static data member.
____________________________________________________________________________________________________________________________________________________________
2)
Write any two characteristic of static data member.
___________________________________________________________________________________________________________________________________________________________
3)
What do you mean by function?
____________________________________________________________________________________________________________________________________________________________
4)
What is use of function?
____________________________________________________________________________________________________________________________________________________________
5)
How is a static member function invoked?
____________________________________________________________________________________________________________________________________________________________
No comments:
Post a Comment