2.2.2 Static Member, Function
2.2.2 Static Member, Function
POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Name of Subject:- Object Oriented Programming Using C++ MSBTE Subject Code:- 22316
Chapter 2: CLASSES & OBJECTS
2.6 Destructors.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 3
• 2a. Develop relevant friend functions to solve the
given problem.
• 2b. Write C++ program to use array of given objects.
• 2c. Write C++ program to create the given object
using constructor.
• 2d. Write program to delete the given object using
destructor in C++ program.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 4
static int count; // declaration of static data member.
int item :: count; //Initialization of static data
• Data member of class can be qualified as static.
• They are used to maintain values that are common to
entire program.
• For example
static data member keeping record of how many
objects have been created till now
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 5
• Its initialized to zero=>0 when object of its class
created. Item a;
• Its Only one copy is created in memory doesn’t
matter how many objects are created.
• As only one copy created so shared by all objects.
• Its visibility is only within the class but its lifetime is
entire program.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 6
int item::count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 7
int item::count;
int main()
{
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
void getdata(int a)
a.getdata(10); { number=a;
a.showcount(); count++;
}
b.showcount();
c.showcount();
Sanjivani K.B.P. Polytechnic, Kopargaon Department
8
of Compute Technology P. M. Dhanrao
int item::count; void getdata(int a)
int main() { number=a;
count++;
{ }
item a,b,c;
a.showcount();
b.showcount();
c.showcount();
a.getdata(10);
a.showcount();
b.showcount(); a.getdata(10); b.getdata(20);
c.showcount(); a.showcount(); a.showcount();
b.showcount(); b.showcount();
c.showcount(); c.showcount();
Sanjivani K.B.P. Polytechnic, Kopargaon Department
9
of Compute Technology P. M. Dhanrao
int item::count; void getdata(int a)
{ number=a;
int main() count++;
}
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
c.getdata(30);
a.getcount();
b.getcount();
c.getcount();
Sanjivani K.B.P. Polytechnic, Kopargaon Department
10
of Compute Technology P. M. Dhanrao
STATIC MEMBER FUNCTION
• we can declare static member function
• but it have access to only static variables and
other static functions
•It can be called by
classname::function-name
Class-name::function-name;
•It can not be called by object.fuction-name
•It cannot access normal data members.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 11
• if still tried to access normal data member say
“int code” inside static function then it gives
error-
• [Error] invalid use of member 'test::code' in
static member function and another error like-
• recipe for target 'static-function' failed