Object Oriented Programming (OOP) Lecture No. 11

Preview:

Citation preview

Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)

Lecture No. 11Lecture No. 11

ReviewReview

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

implementationimplementation►Constant member functionsConstant member functions

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

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);

……};};

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);

……};};

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*/

}}

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*/

}}

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

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){

……}}

……};};

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

ExampleExample

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

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

};};

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 */*/

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

ExampleExample

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

}}

ExampleExample

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

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

return rollNo;return rollNo;}}

};};

ExampleExample

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

}}

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

ExampleExample

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

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

return rollNo;return rollNo;}}

};};

ExampleExample

int main(){int main(){

const Student aStudent;const Student aStudent;

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

}}

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

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

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

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”

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

Class vs. Instance VariableClass vs. Instance Variable

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

Class Space

s1(rollNo,…)

s2(rollNo,…)

s3(rollNo,…)

Instance VariableClass Variable

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;

};};

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

Defining Static Data MemberDefining Static Data Member

class ClassName{class ClassName{

……

static DataType VariableName;static DataType VariableName;

};};

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

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

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*/

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

ExampleExample

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

is equivalent tois equivalent to

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

Recommended