Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

(6)

116 documents

1 / 35

Toggle sidebar

Related documents


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) I -TomU ldo \ (0m Review ►Constant data members ►Constant objects ►Static data members docsity.com Class vs. Instance Variable ►Student s1, s2, s3; Class Space s1(rollNo,…) s2(rollNo,…) s3(rollNo,…) Instance Variable Class Variable docsity.com Static Data Member (Syntax) ►Keyword static is used to make a data member static class ClassName{ … static DataType VariableName; }; docsity.com Defining Static Data Member ►Static data member is declared inside the class ►But they are defined outside the class docsity.com Example class Student{ private: static int noOfStudents; public: … }; int Student::noOfStudents = 0; /*private static member cannot be accessed outside the class except for initialization*/ docsity.com Initializing Static Data Member ►If static data members are not explicitly initialized at the time of definition then they are initialized to 0 docsity.com Example int Student::noOfStudents; is equivalent to int Student::noOfStudents=0; docsity.com Life of Static Data Member ►They are created even when there is no object of a class ►They remain in memory even when all objects of a class are destroyed docsity.com Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ Student::noOfStudents = 1; } docsity.com Example class Student{ public: static int noOfStudents; }; int Student::noOfStudents; int main(){ { Student aStudent; aStudent.noOfStudents = 1; } Student::noOfStudents = 1; } docsity.com Example class Student{ … public: static int noOfStudents; Student(); ~Student(); … }; int Student::noOfStudents = 0; docsity.com Example Student::Student(){ noOfStudents++; } Student::~Student(){ noOfStudents--; } docsity.com Example int Student::noOfStudents = 0; int main(){ cout <<Student::noOfStudents <<endl; Student studentA; cout <<Student::noOfStudents <<endl; Student studentB; cout <<Student::noOfStudents <<endl; } Output: 0 1 2 docsity.com Static Member Function ►They are used to access static data members ►Access mechanism for static member functions is same as that of static data members ►They cannot access any non-static members docsity.com Example class Student{ static int noOfStudents; int rollNo; public: static int getTotalStudent(){ return noOfStudents; } }; int main(){ int i = Student::getTotalStudents(); } docsity.com Accessing non static data members int Student::getTotalStudents(){ return rollNo; } int main(){ int i = Student::getTotalStudents(); /*Error: There is no instance of Student, rollNo cannot be accessed*/ } docsity.com Array of Objects ►Array of objects can only be created if an object can be created without supplying an explicit initializer ►There must always be a default constructor if we want to create array of objects docsity.com Example class Test{ public: }; int main(){ Test array[2]; // OK } docsity.com Example class Test{ public: Test(); }; int main(){ Test array[2]; // OK } docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved