SlideShare a Scribd company logo
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
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();
}
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.

More Related Content

PPTX
static members in object oriented program.pptx
PPTX
static MEMBER IN OOPS PROGRAMING LANGUAGE.pptx
PPTX
Oop lect3.pptx
PPT
classes & objects.ppt
PPT
Unit vi(dsc++)
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Class and object
PDF
Static Keyword Static is a keyword in C++ used to give special chara.pdf
static members in object oriented program.pptx
static MEMBER IN OOPS PROGRAMING LANGUAGE.pptx
Oop lect3.pptx
classes & objects.ppt
Unit vi(dsc++)
[OOP - Lec 19] Static Member Functions
Class and object
Static Keyword Static is a keyword in C++ used to give special chara.pdf

Similar to lecture-18staticdatamember-160705095116.pdf (20)

PDF
Chapter22 static-class-member-example
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPTX
class c++
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
static data member and member function .pptx
PPTX
18.Static Data Members and Static member function.pptx
PPTX
OOPs & C++ UNIT 3
PPTX
classes & objects in cpp
PPTX
classes & objects in cpp overview
PPTX
Static Data Members and Member Functions
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PPT
4 Classes & Objects
PPTX
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
PPTX
classes and objects in C++
PPTX
Classes and objects
PPT
Classes and objects
PPT
C++ Returning Objects
PPTX
Operator overload rr
PPTX
B.sc CSIT 2nd semester C++ Unit3
PPTX
Oop objects_classes
Chapter22 static-class-member-example
classandobjectunit2-150824133722-lva1-app6891.ppt
class c++
Introduction to C++ Class & Objects. Book Notes
static data member and member function .pptx
18.Static Data Members and Static member function.pptx
OOPs & C++ UNIT 3
classes & objects in cpp
classes & objects in cpp overview
Static Data Members and Member Functions
Arre tari mano bhosko ka pikina tari ma no piko
4 Classes & Objects
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
classes and objects in C++
Classes and objects
Classes and objects
C++ Returning Objects
Operator overload rr
B.sc CSIT 2nd semester C++ Unit3
Oop objects_classes

More from AneesAbbasi14 (10)

PPTX
Goal Setting UPDATED.pptx
PDF
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
PDF
lecture-2021inheritance-160705095417.pdf
PPTX
lec09_ransac.pptx
PPTX
lec07_transformations.pptx
PPTX
02-07-20_Anees.pptx
PPTX
Yasir Presentation.pptx
PPTX
Lec5_OOP.pptx
PDF
Lec_1,2_OOP_(Introduction).pdf
PPTX
IntroComputerVision23.pptx
Goal Setting UPDATED.pptx
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
lecture-2021inheritance-160705095417.pdf
lec09_ransac.pptx
lec07_transformations.pptx
02-07-20_Anees.pptx
Yasir Presentation.pptx
Lec5_OOP.pptx
Lec_1,2_OOP_(Introduction).pdf
IntroComputerVision23.pptx

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Business Ethics Teaching Materials for college
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Business Ethics Teaching Materials for college
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharmacology of Heart Failure /Pharmacotherapy of CHF

lecture-18staticdatamember-160705095116.pdf

  • 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 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
  • 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.