SlideShare a Scribd company logo
1 of 17
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;
• }

More Related Content

Similar to static MEMBER IN OOPS PROGRAMING LANGUAGE.pptx

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.pptxAshrithaRokkam
 
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.pptxsyedabbas594247
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxDivyaKS18
 
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 NotesDSMS Group of Institutes
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxrayanbabur
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructorsanitashinde33
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 

Similar to static MEMBER IN OOPS PROGRAMING LANGUAGE.pptx (20)

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
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
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
 
Java2
Java2Java2
Java2
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
 
Java
JavaJava
Java
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 

More from urvashipundir04

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxurvashipundir04
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptxurvashipundir04
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptxurvashipundir04
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxurvashipundir04
 
HOMEOPATHY A new approach to medicines.pptx
HOMEOPATHY A new approach to medicines.pptxHOMEOPATHY A new approach to medicines.pptx
HOMEOPATHY A new approach to medicines.pptxurvashipundir04
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxurvashipundir04
 
Analysis of variance(one way ANOVA).pptx
Analysis of variance(one way ANOVA).pptxAnalysis of variance(one way ANOVA).pptx
Analysis of variance(one way ANOVA).pptxurvashipundir04
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxurvashipundir04
 
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptx
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptxETHICAL ISSUES RELATED TO DATA COLLECTION.pptx
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptxurvashipundir04
 
Scientific Conduct (Part-1)on research.pptx
Scientific Conduct (Part-1)on research.pptxScientific Conduct (Part-1)on research.pptx
Scientific Conduct (Part-1)on research.pptxurvashipundir04
 
INTRODUCTION on hardware and software.pptx
INTRODUCTION on hardware and software.pptxINTRODUCTION on hardware and software.pptx
INTRODUCTION on hardware and software.pptxurvashipundir04
 
INTRODUCTIONTO BASICS OF PROGRAMMING.pptx
INTRODUCTIONTO BASICS OF PROGRAMMING.pptxINTRODUCTIONTO BASICS OF PROGRAMMING.pptx
INTRODUCTIONTO BASICS OF PROGRAMMING.pptxurvashipundir04
 
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptx
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptxINTRODUCTION ON ANOVA TECHNIQUE ONE .pptx
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptxurvashipundir04
 

More from urvashipundir04 (13)

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptx
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
HOMEOPATHY A new approach to medicines.pptx
HOMEOPATHY A new approach to medicines.pptxHOMEOPATHY A new approach to medicines.pptx
HOMEOPATHY A new approach to medicines.pptx
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
Analysis of variance(one way ANOVA).pptx
Analysis of variance(one way ANOVA).pptxAnalysis of variance(one way ANOVA).pptx
Analysis of variance(one way ANOVA).pptx
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptx
 
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptx
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptxETHICAL ISSUES RELATED TO DATA COLLECTION.pptx
ETHICAL ISSUES RELATED TO DATA COLLECTION.pptx
 
Scientific Conduct (Part-1)on research.pptx
Scientific Conduct (Part-1)on research.pptxScientific Conduct (Part-1)on research.pptx
Scientific Conduct (Part-1)on research.pptx
 
INTRODUCTION on hardware and software.pptx
INTRODUCTION on hardware and software.pptxINTRODUCTION on hardware and software.pptx
INTRODUCTION on hardware and software.pptx
 
INTRODUCTIONTO BASICS OF PROGRAMMING.pptx
INTRODUCTIONTO BASICS OF PROGRAMMING.pptxINTRODUCTIONTO BASICS OF PROGRAMMING.pptx
INTRODUCTIONTO BASICS OF PROGRAMMING.pptx
 
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptx
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptxINTRODUCTION ON ANOVA TECHNIQUE ONE .pptx
INTRODUCTION ON ANOVA TECHNIQUE ONE .pptx
 

Recently uploaded

Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Nitin Sonavane
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1T.D. Shashikala
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligencemahaffeycheryld
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...IJECEIAES
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxRashidFaridChishti
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfMadan Karki
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfJNTUA
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfAshrafRagab14
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological universityMohd Saifudeen
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdfKamal Acharya
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AISheetal Jain
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxCHAIRMAN M
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalSwarnaSLcse
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfJNTUA
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfJNTUA
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashidFaiyazSheikh
 

Recently uploaded (20)

Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
Module-III Varried Flow.pptx GVF Definition, Water Surface Profile Dynamic Eq...
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdfALCOHOL PRODUCTION- Beer Brewing Process.pdf
ALCOHOL PRODUCTION- Beer Brewing Process.pdf
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Piping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdfPiping and instrumentation diagram p.pdf
Piping and instrumentation diagram p.pdf
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Online crime reporting system project.pdf
Online crime reporting system project.pdfOnline crime reporting system project.pdf
Online crime reporting system project.pdf
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
Geometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdfGeometric constructions Engineering Drawing.pdf
Geometric constructions Engineering Drawing.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 

static MEMBER IN OOPS PROGRAMING LANGUAGE.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; • }