SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Static Data Members
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
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
200n
1a
10b
Object b1
2a
20b
Object b2
1a
10b
100n
Object b1
2a
20b
200n
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();
}
OUTPUT
• Enter name: Farhan Khan
• Enter marks: 600
• Enter name: Zeeshan
• Enter marks: 786
• Enter name: Ali
• Enter marks: 567
• Roll no: 1
• Name: Farhan Khan
• Marks: 600
• Roll no: 2
• Name: Farhan Khan
• Marks: 786
• Roll no: 3
• Name: Ali
• Marks: 567
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.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Java program structure
Java program structureJava program structure
Java program structure
 
pointer-to-object-.pptx
pointer-to-object-.pptxpointer-to-object-.pptx
pointer-to-object-.pptx
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
class and objects
class and objectsclass and objects
class and objects
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 

Andere mochten auch

[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their AccessingMuhammad Hammad Waseem
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnTypeMuhammad Hammad Waseem
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)Muhammad Hammad Waseem
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three noskrismishra
 
OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)Kai-Feng Chou
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
Programming in c++
Programming in c++Programming in c++
Programming in c++Baljit Saini
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 

Andere mochten auch (20)

[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Computer Software & its Types
Computer Software & its Types Computer Software & its Types
Computer Software & its Types
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
[OOP - Lec 03] Programming Paradigms
[OOP - Lec 03] Programming Paradigms[OOP - Lec 03] Programming Paradigms
[OOP - Lec 03] Programming Paradigms
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
Roots of polynomials
Roots of polynomialsRoots of polynomials
Roots of polynomials
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Es272 ch3b
Es272 ch3bEs272 ch3b
Es272 ch3b
 
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 

Ähnlich wie [OOP - Lec 18] Static Data Member

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
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptxsyedabbas594247
 
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
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptxumarAnjum6
 
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 NotesDigitalDsms
 
Ifi7184 lesson7
Ifi7184 lesson7Ifi7184 lesson7
Ifi7184 lesson7Sónia
 
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
 
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
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for seleniumapoorvams
 

Ähnlich wie [OOP - Lec 18] Static Data Member (20)

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
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
C++ training
C++ training C++ training
C++ training
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
 
1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx
 
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
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Ifi7184 lesson7
Ifi7184 lesson7Ifi7184 lesson7
Ifi7184 lesson7
 
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
 
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
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 

Mehr von Muhammad Hammad Waseem

[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to PointersMuhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)Muhammad Hammad Waseem
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of PrecedenceMuhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of ProgrammingMuhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming LanguagesMuhammad Hammad Waseem
 

Mehr von Muhammad Hammad Waseem (18)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 

Kürzlich hochgeladen

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Kürzlich hochgeladen (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

[OOP - Lec 18] Static Data Member

  • 1. Static Data Members Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  • 2. 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.
  • 3. 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.
  • 4. Difference Between Normal & Static Data Members Object with three normal data members Object with two normal data members and one static data member 200n 1a 10b Object b1 2a 20b Object b2 1a 10b 100n Object b1 2a 20b 200n Object b2
  • 5. 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.
  • 6. 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.
  • 7. 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.
  • 8. 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; } };
  • 9. 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.
  • 10. 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.
  • 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) (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; } };
  • 12. 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(); } OUTPUT • Enter name: Farhan Khan • Enter marks: 600 • Enter name: Zeeshan • Enter marks: 786 • Enter name: Ali • Enter marks: 567 • Roll no: 1 • Name: Farhan Khan • Marks: 600 • Roll no: 2 • Name: Farhan Khan • Marks: 786 • Roll no: 3 • Name: Ali • Marks: 567
  • 13. 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.