32
Object Oriented Programming Object Oriented Programming (OOP) (OOP) Lecture No. 11 Lecture No. 11

Object Oriented Programming (OOP) Lecture No. 11

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming (OOP) Lecture No. 11

Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)

Lecture No. 11Lecture No. 11

Page 2: Object Oriented Programming (OOP) Lecture No. 11

ReviewReview

►this Pointerthis Pointer►Separation of interface and Separation of interface and

implementationimplementation►Constant member functionsConstant member functions

Page 3: Object Oriented Programming (OOP) Lecture No. 11

ProblemProblem

►Change the class Student Change the class Student such that a student is given such that a student is given a roll number when the a roll number when the object is created and cannot object is created and cannot be changed afterwardsbe changed afterwards

Page 4: Object Oriented Programming (OOP) Lecture No. 11

Student ClassStudent Class

class Student{class Student{……int rollNo;int rollNo;

public:public:Student(int aNo);Student(int aNo);int getRollNo();int getRollNo();void setRollNo(int aNo);void setRollNo(int aNo);

……};};

Page 5: Object Oriented Programming (OOP) Lecture No. 11

Modified Student ClassModified Student Class

class Student{class Student{……const int rollNo;const int rollNo;

public:public:Student(int aNo);Student(int aNo);int getRollNo();int getRollNo();void setRollNo(int aNo);void setRollNo(int aNo);

……};};

Page 6: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

Student::Student(int aRollNo)Student::Student(int aRollNo)

{{

rollNo = aRollNo;rollNo = aRollNo;

/*error: cannot modify a /*error: cannot modify a constant data member*/constant data member*/

}}

Page 7: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

void Student::SetRollNo(int i)void Student::SetRollNo(int i)

{{

rollNo = i;rollNo = i;

/*error: cannot modify a /*error: cannot modify a constant data member*/constant data member*/

}}

Page 8: Object Oriented Programming (OOP) Lecture No. 11

Member Initializer ListMember Initializer List

►A member initializer list is a A member initializer list is a mechanism to initialize data mechanism to initialize data membersmembers

►It is given after closing parenthesis It is given after closing parenthesis of parameter list of constructorof parameter list of constructor

►In case of more then one member In case of more then one member use comma separated listuse comma separated list

Page 9: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

class Student{class Student{const int rollNo;const int rollNo;char *name;char *name;float GPA;float GPA;

public:public:Student(int aRollNo)Student(int aRollNo): rollNo(aRollNo), name(Null), GPA(0.0){: rollNo(aRollNo), name(Null), GPA(0.0){

……}}

……};};

Page 10: Object Oriented Programming (OOP) Lecture No. 11

Order of InitializationOrder of Initialization

►Data member are initialized in order Data member are initialized in order they are declaredthey are declared

►Order in member initializer list is not Order in member initializer list is not significant at allsignificant at all

Page 11: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

class ABC{class ABC{int x;int x;int y;int y;int z;int z;

public:public:ABC();ABC();

};};

Page 12: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

ABC::ABC():y(10),x(y),z(y)ABC::ABC():y(10),x(y),z(y){{……

}}/*/* x = Junk valuex = Junk value

y = 10y = 10z = 10z = 10 */*/

Page 13: Object Oriented Programming (OOP) Lecture No. 11

constconst ObjectsObjects

►Objects can be declared Objects can be declared constant with the use of constant with the use of const keywordconst keyword

►Constant objects cannot Constant objects cannot change their statechange their state

Page 14: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

int main()int main(){{const Student aStudent;const Student aStudent;return 0;return 0;

}}

Page 15: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

class Student{class Student{……int rollNo;int rollNo;

public:public:……int getRollNo(){int getRollNo(){

return rollNo;return rollNo;}}

};};

Page 16: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

int main(){int main(){const Student aStudent;const Student aStudent;int a = aStudent.getRollNo();int a = aStudent.getRollNo();//error//error

}}

Page 17: Object Oriented Programming (OOP) Lecture No. 11

constconst Objects Objects

►constconst objects cannot objects cannot access “access “non const”non const” member member functionfunction

►Chances of unintentional Chances of unintentional modification are eliminatedmodification are eliminated

Page 18: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

class Student{class Student{……int rollNo;int rollNo;

public:public:……int getRollNo()const{int getRollNo()const{

return rollNo;return rollNo;}}

};};

Page 19: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

int main(){int main(){

const Student aStudent;const Student aStudent;

int a = aStudent.getRollNo();int a = aStudent.getRollNo();

}}

Page 20: Object Oriented Programming (OOP) Lecture No. 11

Constant data membersConstant data members

►Make all functions that don’t change Make all functions that don’t change the state of the object constantthe state of the object constant

►This will enable constant objects to This will enable constant objects to access more member functionsaccess more member functions

Page 21: Object Oriented Programming (OOP) Lecture No. 11

Static VariablesStatic Variables

►Lifetime of static variable is Lifetime of static variable is throughout the program lifethroughout the program life

►If static variables are not If static variables are not explicitly initialized then they explicitly initialized then they are initialized to 0 of are initialized to 0 of appropriate typeappropriate type

Page 22: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

void func1(int i){void func1(int i){static int staticInt = i;static int staticInt = i;cout << staticInt << endl;cout << staticInt << endl;

}}int main(){int main(){func1(1);func1(1);func1(2);func1(2);

}}

Output:11

Page 23: Object Oriented Programming (OOP) Lecture No. 11

Static Data MemberStatic Data Member

DefinitionDefinition

““A variable that is part of a A variable that is part of a class, yet is not part of an class, yet is not part of an object of that class, is called object of that class, is called static data member”static data member”

Page 24: Object Oriented Programming (OOP) Lecture No. 11

Static Data MemberStatic Data Member

►They are shared by all They are shared by all instances of the classinstances of the class

►They do not belong to any They do not belong to any particular instance of a particular instance of a classclass

Page 25: Object Oriented Programming (OOP) Lecture No. 11

Class vs. Instance VariableClass vs. Instance Variable

►Student sStudent s11, , s2s2, , s3s3;;

Class Space

s1(rollNo,…)

s2(rollNo,…)

s3(rollNo,…)

Instance VariableClass Variable

Page 26: Object Oriented Programming (OOP) Lecture No. 11

Static Data Member (Syntax)Static Data Member (Syntax)

►Keyword static is used to Keyword static is used to make a data member staticmake a data member static

class ClassName{class ClassName{

……

static DataType VariableName;static DataType VariableName;

};};

Page 27: Object Oriented Programming (OOP) Lecture No. 11

Defining Static Data MemberDefining Static Data Member

►Static data member is Static data member is declared inside the classdeclared inside the class

►But they are defined outside But they are defined outside the classthe class

Page 28: Object Oriented Programming (OOP) Lecture No. 11

Defining Static Data MemberDefining Static Data Member

class ClassName{class ClassName{

……

static DataType VariableName;static DataType VariableName;

};};

DataType ClassName::VariableName;DataType ClassName::VariableName;

Page 29: Object Oriented Programming (OOP) Lecture No. 11

Initializing Static Data Initializing Static Data MemberMember

►Static data members should Static data members should be initialized once at file be initialized once at file scopescope

►They are initialized at the They are initialized at the time of definitiontime of definition

Page 30: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

class Student{class Student{private:private:

static int noOfStudents;static int noOfStudents;public:public: … …};};

int Student::noOfStudentsint Student::noOfStudents = 0; = 0;/*private static member cannot be /*private static member cannot be accessed outside the class except for accessed outside the class except for initialization*/initialization*/

Page 31: Object Oriented Programming (OOP) Lecture No. 11

Initializing Static Data Initializing Static Data MemberMember

►If static data members are If static data members are not explicitly initialized at not explicitly initialized at the time of definition then the time of definition then they are initialized to 0they are initialized to 0

Page 32: Object Oriented Programming (OOP) Lecture No. 11

ExampleExample

int Student::noOfStudents;int Student::noOfStudents;

is equivalent tois equivalent to

int Student::noOfStudents=0;int Student::noOfStudents=0;