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

Member Initializer List-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Member, Initializer, Keyword, State, Object, Const, Orient, Change, Program, Declare, List

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(6)

116 documents

1 / 32

Toggle sidebar

Related documents


Partial preview of the text

Download Member Initializer List-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity! Object Oriented Programming (OOP) I -Toia0 dom \ On Review ►this Pointer ►Separation of interface and implementation ►Constant member functions docsity.com Modified Student Class class Student{ … const int rollNo; public: Student(int aNo); int getRollNo(); void setRollNo(int aNo); … }; docsity.com Example Student::Student(int aRollNo) { rollNo = aRollNo; /*error: cannot modify a constant data member*/ } docsity.com Example void Student::SetRollNo(int i) { rollNo = i; /*error: cannot modify a constant data member*/ } docsity.com Order of Initialization ►Data member are initialized in order they are declared ►Order in member initializer list is not significant at all docsity.com Example class ABC{ int x; int y; int z; public: ABC(); }; docsity.com Example ABC::ABC():y(10),x(y),z(y) { … } /* x = Junk value y = 10 z = 10 */ docsity.com Example class Student{ … int rollNo; public: … int getRollNo(){ return rollNo; } }; docsity.com Example int main(){ const Student aStudent; int a = aStudent.getRollNo(); //error } docsity.com const Objects ►const objects cannot access “non const” member function ►Chances of unintentional modification are eliminated docsity.com Constant data members ►Make all functions that don’t change the state of the object constant ►This will enable constant objects to access more member functions docsity.com Static Variables ►Lifetime of static variable is throughout the program life ►If static variables are not explicitly initialized then they are initialized to 0 of appropriate type docsity.com Example void func1(int i){ static int staticInt = i; cout << staticInt << endl; } int main(){ func1(1); func1(2); } Output: 1 1 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
Docsity logo



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