0% found this document useful (0 votes)
218 views15 pages

2.2.2 Static Member, Function

This document contains a chapter summary for an Object Oriented Programming Using C++ course. It discusses classes and objects, including static data members, static member functions, and friend functions. It provides examples of using static data to count the number of objects created and static member functions that can access only static variables. The summary also includes example code for declaring and initializing a static data member and using a static member function without object initialization. Finally, it lists the page numbers where these topics are further discussed in the textbook.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views15 pages

2.2.2 Static Member, Function

This document contains a chapter summary for an Object Oriented Programming Using C++ course. It discusses classes and objects, including static data members, static member functions, and friend functions. It provides examples of using static data to count the number of objects created and static member functions that can access only static variables. The summary also includes example code for declaring and initializing a static data member and using a static member function without object initialization. Finally, it lists the page numbers where these topics are further discussed in the textbook.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SANJIVANI K. B. P.

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

Department:- COMPUTER TECHNOLOGY Class:- SYCM-B

Name of Subject:- Object Oriented Programming Using C++ MSBTE Subject Code:- 22316
Chapter 2: CLASSES & OBJECTS

2.1 Class & Object: Introduction, specifying a class, access


specifiers, defining member functions, creating Objects,
memory allocations for objects.

2.2 Static data members, static member function, friend function

2.3 Array of Objects, Object as function arguments.

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 2


Chapter 1: Principles of OOP

2.4 Concepts of constructors, Types of


Constructors
2.5 Multiple Constructors in a Class,
Constructors with default arguments.

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

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 12


#include <iostream.h> • int test::count;

class test • int main()


• { test t1,t2,t3;
{ static int count;
• t1.setcode();
int code; • t1.showcode();
public: • test::showcount();
void setcode() •
• t2.setcode();
{ code = ++count; }
• t2.showcode();
void showcode()
• test::showcount();
{ cout<<"\nobject number = •
"<<code; } • t3.setcode();
static void showcount() • t3.showcode();
{ cout<<"\ncount : "<<count; • test::showcount();
} • return 0;
}; • }
Sanjivani K.B.P. Polytechnic, Kopargaon Department
13
of Compute Technology P. M. Dhanrao
#include <iostream.h> int test::count;
class test int main()
{ static int count; { test t1,t2,t3;
t1.setcode();
int code;
test::showcount();
public:
void setcode() t2.setcode();
test::showcount();
{ code = ++count; }
static void showcount() t3.setcode();
{ cout<<"\ncount : "<<count; } test::showcount();
return 0;
};
}
Sanjivani K.B.P. Polytechnic, Kopargaon Department
14
of Compute Technology P. M. Dhanrao
•2.2 Static data members-115
•static member function- 117
•friend function-124

Sanjivani K.B.P. Polytechnic, Kopargaon Department of Compute Technology P. M. Dhanrao 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy