SlideShare a Scribd company logo
Static Data Member
• A type of data member that is shared among all objects of class is
known as static data member.
• The static data member is defined in the class with static keyword.
• When a data member is defined as static, only one variable is created
in the memory even if there are many objects of that class.
• A static data item is useful when all objects of the same class must
share a common item of information.
• The characteristics of a static data member are same as normal static
variable.
Static Data Member
• It is visible only in the class, in which it is defined but its lifetime
• Starts when the program starts its execution.
• Ends when the entire program is terminated.
• It is normally used to share some data among all objects of a particular
class.
• The main difference between normal data member and static data
member is that
• each object has its own variable of normal data member.
• On the other hand, static data member is shared among all objects of the class.
• Only one memory location is created for static data member that is shared
among all objects.
Difference Between Normal & Static Data
Members
Object with three normal data
members
Object with two normal data members
and one static data member
200
n
1
a
10
b
Object b1
2
a
20
b
Object b2
1
a
10
b
100
n
Object b1
2
a
20
b
200
n
Object b2
Uses of Static Class Data
• Why would you want to use static member data?
• As an example, suppose an object needed to know how many other
objects of its class were in the program.
• for example :
• In a road-racing game, a race car might want to know how many other cars
are still in the race.
• In this case a static variable count could be included as a member of
the class. All the objects would have access to this variable.
• It would be the same variable for all of them; they would all see the
same count.
Separate Declaration and Definition
• Static member data requires an unusual format.
• Ordinary variables are usually declared and defined in the same
statement.
• Static member data, on the other hand, requires two separate
statements.
• The variable’s declaration appears in the class definition, but the
• Variable is defined outside the class, in much the same way as a global
variable.
• Why is this two-part approach used?
• If static member data were defined inside the class, it would violate the idea
that a class definition is only a blueprint and does not set aside any memory.
Separate Declaration and Definition
• Putting the definition of static member data outside the class also
serves to emphasize that
• the memory space for such data is allocated only once, before the program
starts to execute, and that
• one static member variable is accessed by an entire class; each object does
not have its own version of the variable, as it would with ordinary member
data.
• In this way a static member variable is more like a global variable.
Write a program that counts the number of
objects created of a particular class (1/2)
class yahoo
{
private:
static int n;
public:
yahoo()
{ n++; }
void show()
{
cout<<“you have created ”<<n<<“ objects so far ”<<endl;
}
};
Write a program that counts the number of
objects created of a particular class (2/2)
int yahoo::n=0;
void main()
{
yahoo x,y;
x.show();
yahoo z;
x.show();
}
OUTPUT:
• You have created 2 objects so far.
• You have created 3 objects so far.
How it Works
• The program declares a static data member n to count the number of objects
that have been created.
• The statement int yahoo::n=0;defines the variable and initializes it to 0.
• The variable is defined outside the class because it will be not part of any object.
• It is created only once in the memory and is shared among all objects of the class.
• The variable definition outside the class must be preceded with class name and
scope resolution operator ::.
• The compiler does not display any error if the static data member is not defined.
• The linker will generate an error when the program is executed.
• The above program creates three objects x, y and z. each time an object is
created, the constructor is executed that increases the value of n by 1.
Write a program that creates three objects of class
student. Each of them must assigned a unique roll
number. (Hint: use static data member for unique roll number) (1/2)
class Student
{
private:
static int r;
int rno,marks;
char name[30];
public:
Student()
{ r++;
Rno =r; }
void in()
{
cout<<“enter name:”;
gets(name);
cout<<“enter marks:”;
cin>>marks;
}
void show()
{
cout<<“Roll No:”<<rno<<endl;
cout<<“Name:”<<name<<endl;
cout<<“Marks:”<<marks<<endl;
}
};
Write a program that creates three objects of class
student. Each of them must assigned a unique roll
number. (Hint: use static data member for unique roll number) (2/2)
int Student::r=0;
void main()
{
Student s1,s2,s3;
s1.in();
s2.in();
s3.in();
s1.show();
s2.show();
s3.show();
}
How it Works
• The above program uses a static data member r to assign unique roll
numbers to each object of the class Student.
• The static data member is initialized to 0.
• The constructor increments its value by 1 when an object is created
and then assigns the updated value of r to the data member rno.
• It ensures that each object gets a unique roll number.
Static member function:
• A member function that is declared static has the following properties:
 A static function can have access to only other static members(function or variable)
declared in the same class.
 A static member function can be called using the class name.
like, class_name :: Function_name();
test :: getdata();
• Static Member Function in C++
• Static Member Function in a class is the function that is declared as static because
of which function attains certain properties as defined below:
• A static member function is independent of any object of the class.
• A static member function can be called even if no objects of the class exist.
• A static member function can also be accessed using the class name through the
scope resolution operator.
• A static member function can access static data members and static member
functions inside or outside of the class.
• Static member functions have a scope inside the class and cannot access the
current object pointer.
• You can also use a static member function to determine how many objects of the
class have been created.
• Static members are frequently used to store information that is
shared by all objects in a class.
• For instance, you may keep track of the quantity of newly generated
objects of a specific class type using a static data member as a
counter. This static data member can be increased each time an
object is generated to keep track of the overall number of objects.
• // C++ Program to show the working of
• // static member functions
• #include <iostream>
• using namespace std;
• class Box
• {
• private:
• static int length;
• static int breadth;
• static int height;
•
• public:
•
• static void print()
• {
• cout << "The value of the length is: " << length << endl;
• cout << "The value of the breadth is: " << breadth << endl;
• cout << "The value of the height is: " << height << endl;
• }
• };
• // initialize the static data members
• int Box :: length = 10;
• int Box :: breadth = 20;
• int Box :: height = 30;
• // Driver Code
• int main()
• {
•
• Box b;
•
• cout << "Static member function is called through Object name: n" << endl;
• b.print();
•
• cout << "nStatic member function is called through Class name: n" << endl;
• Box::print();
•
• return 0;
• }
Ad

More Related Content

What's hot (20)

Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners
Vibhawa Nirmal
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
Basant Medhat
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 
Oops in vb
Oops in vbOops in vb
Oops in vb
Dalwin INDIA
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
elliando dias
 
Java servlets
Java servletsJava servlets
Java servlets
yuvarani p
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
Aniruddha Chakrabarti
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Java annotations
Java annotationsJava annotations
Java annotations
FAROOK Samath
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
M Vishnuvardhan Reddy
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 
Css class-02
Css class-02Css class-02
Css class-02
Md Ali Hossain
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
pcnmtutorials
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
malathip12
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
Reggie Niccolo Santos
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
PRINCE KUMAR
 

Similar to static members in object oriented program.pptx (20)

lecture-18staticdatamember-160705095116.pdf
lecture-18staticdatamember-160705095116.pdflecture-18staticdatamember-160705095116.pdf
lecture-18staticdatamember-160705095116.pdf
AneesAbbasi14
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
samiairshad090
 
C++ training
C++ training C++ training
C++ training
PL Sharma
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
AshrithaRokkam
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
OOPs theory about its concepts and properties.
OOPs theory about its concepts and properties.OOPs theory about its concepts and properties.
OOPs theory about its concepts and properties.
ssuser1af273
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
jayeshsoni49
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Shailendra Veeru
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
Java2
Java2Java2
Java2
Ranjitham N
 
lecture-18staticdatamember-160705095116.pdf
lecture-18staticdatamember-160705095116.pdflecture-18staticdatamember-160705095116.pdf
lecture-18staticdatamember-160705095116.pdf
AneesAbbasi14
 
18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
samiairshad090
 
C++ training
C++ training C++ training
C++ training
PL Sharma
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
AshrithaRokkam
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
OOPs theory about its concepts and properties.
OOPs theory about its concepts and properties.OOPs theory about its concepts and properties.
OOPs theory about its concepts and properties.
ssuser1af273
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
Ad

More from urvashipundir04 (20)

stack in python using different datatypes.pptx
stack in python using different datatypes.pptxstack in python using different datatypes.pptx
stack in python using different datatypes.pptx
urvashipundir04
 
Game Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptxGame Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
extended modelling in dbms using different.pptx
extended modelling in dbms using different.pptxextended modelling in dbms using different.pptx
extended modelling in dbms using different.pptx
urvashipundir04
 
PRODUCTION SYSTEM in data science .pptx
PRODUCTION SYSTEM in data science  .pptxPRODUCTION SYSTEM in data science  .pptx
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
Presentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptxPresentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptx
urvashipundir04
 
Dependency modelling in data mining.pptx
Dependency modelling in data mining.pptxDependency modelling in data mining.pptx
Dependency modelling in data mining.pptx
urvashipundir04
 
INTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptxINTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
SOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptxSOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
datamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptxdatamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptx
urvashipundir04
 
datamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptxdatamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptx
urvashipundir04
 
Underfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptxUnderfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
introduction values and best practices in
introduction values and best practices inintroduction values and best practices in
introduction values and best practices in
urvashipundir04
 
ppt on different topics of circular.pptx
ppt on different topics of circular.pptxppt on different topics of circular.pptx
ppt on different topics of circular.pptx
urvashipundir04
 
list in python and traversal of list.pptx
list in python and traversal of list.pptxlist in python and traversal of list.pptx
list in python and traversal of list.pptx
urvashipundir04
 
ermodelN in database management system.ppt
ermodelN in database management system.pptermodelN in database management system.ppt
ermodelN in database management system.ppt
urvashipundir04
 
libraries in python using different .pptx
libraries in python using different .pptxlibraries in python using different .pptx
libraries in python using different .pptx
urvashipundir04
 
tuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptxtuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptx
urvashipundir04
 
ANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptxANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
dispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptxdispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
loopin gstatement in python using .pptx
loopin gstatement in python using  .pptxloopin gstatement in python using  .pptx
loopin gstatement in python using .pptx
urvashipundir04
 
stack in python using different datatypes.pptx
stack in python using different datatypes.pptxstack in python using different datatypes.pptx
stack in python using different datatypes.pptx
urvashipundir04
 
Game Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptxGame Playing in Artificial intelligence.pptx
Game Playing in Artificial intelligence.pptx
urvashipundir04
 
extended modelling in dbms using different.pptx
extended modelling in dbms using different.pptxextended modelling in dbms using different.pptx
extended modelling in dbms using different.pptx
urvashipundir04
 
PRODUCTION SYSTEM in data science .pptx
PRODUCTION SYSTEM in data science  .pptxPRODUCTION SYSTEM in data science  .pptx
PRODUCTION SYSTEM in data science .pptx
urvashipundir04
 
Presentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptxPresentation1 in datamining using techn.pptx
Presentation1 in datamining using techn.pptx
urvashipundir04
 
Dependency modelling in data mining.pptx
Dependency modelling in data mining.pptxDependency modelling in data mining.pptx
Dependency modelling in data mining.pptx
urvashipundir04
 
INTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptxINTRODUCTION to datawarehouse IN DATA.pptx
INTRODUCTION to datawarehouse IN DATA.pptx
urvashipundir04
 
SOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptxSOCIAL NETWORK ANALYISI in engeenireg.pptx
SOCIAL NETWORK ANALYISI in engeenireg.pptx
urvashipundir04
 
datamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptxdatamining in engerring using different techniques.pptx
datamining in engerring using different techniques.pptx
urvashipundir04
 
datamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptxdatamining IN Artificial intelligence.pptx
datamining IN Artificial intelligence.pptx
urvashipundir04
 
Underfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptxUnderfitting and Overfitting in Machine Learning.pptx
Underfitting and Overfitting in Machine Learning.pptx
urvashipundir04
 
introduction values and best practices in
introduction values and best practices inintroduction values and best practices in
introduction values and best practices in
urvashipundir04
 
ppt on different topics of circular.pptx
ppt on different topics of circular.pptxppt on different topics of circular.pptx
ppt on different topics of circular.pptx
urvashipundir04
 
list in python and traversal of list.pptx
list in python and traversal of list.pptxlist in python and traversal of list.pptx
list in python and traversal of list.pptx
urvashipundir04
 
ermodelN in database management system.ppt
ermodelN in database management system.pptermodelN in database management system.ppt
ermodelN in database management system.ppt
urvashipundir04
 
libraries in python using different .pptx
libraries in python using different .pptxlibraries in python using different .pptx
libraries in python using different .pptx
urvashipundir04
 
tuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptxtuple in python is an impotant topic.pptx
tuple in python is an impotant topic.pptx
urvashipundir04
 
ANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptxANIMATION in computer graphics using 3 D.pptx
ANIMATION in computer graphics using 3 D.pptx
urvashipundir04
 
dispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptxdispaly subroutines in computer graphics .pptx
dispaly subroutines in computer graphics .pptx
urvashipundir04
 
loopin gstatement in python using .pptx
loopin gstatement in python using  .pptxloopin gstatement in python using  .pptx
loopin gstatement in python using .pptx
urvashipundir04
 
Ad

Recently uploaded (20)

Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 

static members in object oriented program.pptx

  • 1. Static Data Member • A type of data member that is shared among all objects of class is known as static data member. • The static data member is defined in the class with static keyword. • When a data member is defined as static, only one variable is created in the memory even if there are many objects of that class. • A static data item is useful when all objects of the same class must share a common item of information. • The characteristics of a static data member are same as normal static variable.
  • 2. Static Data Member • It is visible only in the class, in which it is defined but its lifetime • Starts when the program starts its execution. • Ends when the entire program is terminated. • It is normally used to share some data among all objects of a particular class. • The main difference between normal data member and static data member is that • each object has its own variable of normal data member. • On the other hand, static data member is shared among all objects of the class. • Only one memory location is created for static data member that is shared among all objects.
  • 3. Difference Between Normal & Static Data Members Object with three normal data members Object with two normal data members and one static data member 200 n 1 a 10 b Object b1 2 a 20 b Object b2 1 a 10 b 100 n Object b1 2 a 20 b 200 n Object b2
  • 4. Uses of Static Class Data • Why would you want to use static member data? • As an example, suppose an object needed to know how many other objects of its class were in the program. • for example : • In a road-racing game, a race car might want to know how many other cars are still in the race. • In this case a static variable count could be included as a member of the class. All the objects would have access to this variable. • It would be the same variable for all of them; they would all see the same count.
  • 5. Separate Declaration and Definition • Static member data requires an unusual format. • Ordinary variables are usually declared and defined in the same statement. • Static member data, on the other hand, requires two separate statements. • The variable’s declaration appears in the class definition, but the • Variable is defined outside the class, in much the same way as a global variable. • Why is this two-part approach used? • If static member data were defined inside the class, it would violate the idea that a class definition is only a blueprint and does not set aside any memory.
  • 6. Separate Declaration and Definition • Putting the definition of static member data outside the class also serves to emphasize that • the memory space for such data is allocated only once, before the program starts to execute, and that • one static member variable is accessed by an entire class; each object does not have its own version of the variable, as it would with ordinary member data. • In this way a static member variable is more like a global variable.
  • 7. Write a program that counts the number of objects created of a particular class (1/2) class yahoo { private: static int n; public: yahoo() { n++; } void show() { cout<<“you have created ”<<n<<“ objects so far ”<<endl; } };
  • 8. Write a program that counts the number of objects created of a particular class (2/2) int yahoo::n=0; void main() { yahoo x,y; x.show(); yahoo z; x.show(); } OUTPUT: • You have created 2 objects so far. • You have created 3 objects so far.
  • 9. How it Works • The program declares a static data member n to count the number of objects that have been created. • The statement int yahoo::n=0;defines the variable and initializes it to 0. • The variable is defined outside the class because it will be not part of any object. • It is created only once in the memory and is shared among all objects of the class. • The variable definition outside the class must be preceded with class name and scope resolution operator ::. • The compiler does not display any error if the static data member is not defined. • The linker will generate an error when the program is executed. • The above program creates three objects x, y and z. each time an object is created, the constructor is executed that increases the value of n by 1.
  • 10. Write a program that creates three objects of class student. Each of them must assigned a unique roll number. (Hint: use static data member for unique roll number) (1/2) class Student { private: static int r; int rno,marks; char name[30]; public: Student() { r++; Rno =r; } void in() { cout<<“enter name:”; gets(name); cout<<“enter marks:”; cin>>marks; } void show() { cout<<“Roll No:”<<rno<<endl; cout<<“Name:”<<name<<endl; cout<<“Marks:”<<marks<<endl; } };
  • 11. Write a program that creates three objects of class student. Each of them must assigned a unique roll number. (Hint: use static data member for unique roll number) (2/2) int Student::r=0; void main() { Student s1,s2,s3; s1.in(); s2.in(); s3.in(); s1.show(); s2.show(); s3.show(); }
  • 12. How it Works • The above program uses a static data member r to assign unique roll numbers to each object of the class Student. • The static data member is initialized to 0. • The constructor increments its value by 1 when an object is created and then assigns the updated value of r to the data member rno. • It ensures that each object gets a unique roll number.
  • 13. Static member function: • A member function that is declared static has the following properties:  A static function can have access to only other static members(function or variable) declared in the same class.  A static member function can be called using the class name. like, class_name :: Function_name(); test :: getdata();
  • 14. • Static Member Function in C++ • Static Member Function in a class is the function that is declared as static because of which function attains certain properties as defined below: • A static member function is independent of any object of the class. • A static member function can be called even if no objects of the class exist. • A static member function can also be accessed using the class name through the scope resolution operator. • A static member function can access static data members and static member functions inside or outside of the class. • Static member functions have a scope inside the class and cannot access the current object pointer. • You can also use a static member function to determine how many objects of the class have been created.
  • 15. • Static members are frequently used to store information that is shared by all objects in a class. • For instance, you may keep track of the quantity of newly generated objects of a specific class type using a static data member as a counter. This static data member can be increased each time an object is generated to keep track of the overall number of objects.
  • 16. • // C++ Program to show the working of • // static member functions • #include <iostream> • using namespace std; • class Box • { • private: • static int length; • static int breadth; • static int height; • • public: • • static void print() • { • cout << "The value of the length is: " << length << endl; • cout << "The value of the breadth is: " << breadth << endl; • cout << "The value of the height is: " << height << endl; • } • };
  • 17. • // initialize the static data members • int Box :: length = 10; • int Box :: breadth = 20; • int Box :: height = 30; • // Driver Code • int main() • { • • Box b; • • cout << "Static member function is called through Object name: n" << endl; • b.print(); • • cout << "nStatic member function is called through Class name: n" << endl; • Box::print(); • • return 0; • }
  翻译: