Understanding Static Data Members in Object-Oriented Programming, Slides of Object Oriented Programming

An in-depth explanation of static data members in object- oriented programming (oop). Static data members are variables that belong to a class rather than an object, and they are shared among all instances of the class. The definition, syntax, initialization, accessing, and uses of static data members, as well as the differences between static data members and instance variables.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented Programming
(OOP)
Lecture No. 12
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Understanding Static Data Members in Object-Oriented Programming and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented Programming

(OOP)

Lecture No. 12

Review

►Constant data members ►Constant objects ►Static data members

Static Data Member

►They are shared by allinstances of the class ►They do not belong to anyparticular instance of a class

Class vs. Instance Variable

►Student

s

s

,^

s

Class Space

s1 (rollNo,…)

s2 (rollNo,…)

Instance Variable s3 (rollNo,…)

ClassVariable

Defining Static Data Member

►Static data member is declared inside theclass ►But they are defined outside the class

Defining Static Data Member

class ClassName{… static DataType VariableName;}; DataType ClassName::VariableName;

Example

class

Student{ private: static

int

noOfStudents;

public:… }; int Student::noOfStudents

/*private

static

member

cannot

be accessed

outside

the

class

except

for

initialization*/

  • Initializing Static Data Member ►If static data members are notexplicitly initialized at the timeof definition then they areinitialized to

Accessing Static Data Member ►To access a static data member there aretwo ways^ ^ Access like a normal data member^ ^ Access using a scope resolution operator ‘::’

Example

class Student{public: static int noOfStudents; };int Student::noOfStudents;int main(){ Student aStudent;aStudent.noOfStudents = 1;Student::noOfStudents = 1; }

Example

class Student{public:

static int noOfStudents;

}; int Student::noOfStudents;int main(){

Student::noOfStudents = 1;

Example

class Student{public: static int noOfStudents; };int Student::noOfStudents;int main(){ { Student aStudent;aStudent.noOfStudents = 1;} Student::noOfStudents = 1; }

Example

►Modify the class Student suchthat one can know the numberof student created in a system

Example

class

Student{ … public:static

int

noOfStudents;

Student();~Student();… };int^ Student::noOfStudents

=^