object oriented programming (oop) lecture no. 10

28
Object Oriented Programming Object Oriented Programming (OOP) (OOP) Lecture No. 10 Lecture No. 10

Upload: gabriella-mccormick

Post on 05-Jan-2016

234 views

Category:

Documents


4 download

TRANSCRIPT

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

Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)

Lecture No. 10Lecture No. 10

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

ReviewReview

►Copy constructorsCopy constructors►DestructorDestructor►Accessor FunctionsAccessor Functions►this Pointerthis Pointer

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

this Pointerthis Pointer►There are situations where There are situations where

designer wants to return designer wants to return reference to current object reference to current object from a functionfrom a function

►In such cases reference is In such cases reference is taken from this pointer like taken from this pointer like (*this)(*this)

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

ExampleExample

Student Student::setRollNo(int aNo)Student Student::setRollNo(int aNo){{……return *this;return *this;

}}Student Student::setName(char *aName)Student Student::setName(char *aName){{……return *this;return *this;

}}

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

ExampleExample

int main()int main(){{

Student aStudent;Student aStudent;Student bStudent;Student bStudent;

bStudent = aStudent.setName(“Ahmad”);bStudent = aStudent.setName(“Ahmad”);……bStudent = aStudent.setName(“Ali”).setRollNo(2);bStudent = aStudent.setName(“Ali”).setRollNo(2);

return 0;return 0;}}

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

Separation of interface and Separation of interface and implementationimplementation

►Public member function exposed Public member function exposed by a class is called interfaceby a class is called interface

►Separation of implementation Separation of implementation from the interface is good from the interface is good software engineeringsoftware engineering

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

Complex NumberComplex Number

►There are two representations of There are two representations of complex numbercomplex number Euler form Euler form ►z = x + z = x + ii y y

Phasor form Phasor form ►z = |z| (cos z = |z| (cos + + ii sin sin ))►z is known as the complex modulus z is known as the complex modulus and and is known as the complex is known as the complex argument or phaseargument or phase

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

ExampleExample

float getX()float getX()

float getY()float getY()

void setNumbervoid setNumber

(float i, float j)(float i, float j)

……

float xfloat x

float yfloat y

ComplexComplex

Old Old implementationimplementation

float getX()float getX()

float getY()float getY()

void setNumbervoid setNumber

(float i, float j)(float i, float j)

……

float zfloat z

float thetafloat theta

ComplexComplex

New New implementationimplementation

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

ExampleExampleclass Complex{ //oldclass Complex{ //oldfloat x;float x;float y;float y;

public:public:void setNumber(float i, float j){void setNumber(float i, float j){

x = i;x = i;y = j;y = j;

}}……

};};

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

ExampleExampleclass Complex{ //newclass Complex{ //newfloat z;float z;float theta;float theta;

public:public:void setNumber(float i, float j){void setNumber(float i, float j){

theta = arctan(j/i);theta = arctan(j/i);……

}}……

};};

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

AdvantagesAdvantages

►User is only concerned about User is only concerned about ways of accessing data ways of accessing data (interface)(interface)

►User has no concern about the User has no concern about the internal representation and internal representation and implementation of the classimplementation of the class

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

Separation of interface and Separation of interface and implementationimplementation

►Usually functions are defined Usually functions are defined in implementation files (.cpp) in implementation files (.cpp) while the class definition is while the class definition is given in header file (.h)given in header file (.h)

►Some authors also consider Some authors also consider this as separation of interface this as separation of interface and implementationand implementation

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

Student.hStudent.h

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

public:public:void setRollNo(int aRollNo);void setRollNo(int aRollNo);int getRollNo();int getRollNo();……

};};

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

Student.cppStudent.cpp

#include “student.h”#include “student.h”

void Student::setRollNo(int aNo){void Student::setRollNo(int aNo){……

}}int Student::getRollNo(){int Student::getRollNo(){……}}

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

Driver.cppDriver.cpp

#include “student.h”#include “student.h”

int main(){int main(){Student aStudent;Student aStudent;

}}

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

►There are functions that are There are functions that are meant to be read onlymeant to be read only

►There must exist a There must exist a mechanism to detect error if mechanism to detect error if such functions accidentally such functions accidentally change the data memberchange the data member

constconst Member Functions Member Functions

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

►Keyword Keyword constconst is placed at is placed at the end of the parameter the end of the parameter listlist

constconst Member Functions Member Functions

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

constconst Member Functions Member Functions

Declaration:Declaration:class ClassName{class ClassName{ReturnVal Function() const;ReturnVal Function() const;

};};

Definition:Definition:ReturnVal ClassName::Function() const{ReturnVal ClassName::Function() const{……

}}

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

ExampleExample

class Student{class Student{

public:public:

int getRollNo() int getRollNo() constconst {{

return rollNo;return rollNo;

}}

};};

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

constconst Functions Functions

►Constant member functions Constant member functions cannot modify the state of any cannot modify the state of any objectobject

►They are just They are just “read-only”“read-only”►Errors due to typing are also Errors due to typing are also

caught at compile timecaught at compile time

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

ExampleExample

bool Student::isRollNo(int aNo){bool Student::isRollNo(int aNo){

if(rollNo =if(rollNo = = aNo){= aNo){

return true;return true;

}}

return false;return false;

}}

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

ExampleExample

bool Student::isRollNo(int aNo){bool Student::isRollNo(int aNo){

/*undetected typing mistake*//*undetected typing mistake*/

if(rollNo = aNo){if(rollNo = aNo){

return true;return true;

}}

return false;return false;

}}

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

ExampleExample

bool Student::isRollNobool Student::isRollNo(int aNo)const{(int aNo)const{

/*compiler error*//*compiler error*/if(rollNo = aNo){if(rollNo = aNo){

return true;return true;}}return false;return false;

}}

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

constconst Functions Functions

►Constructors and Destructors Constructors and Destructors cannot be cannot be constconst

►Constructor and destructor are Constructor and destructor are used to modify the object to a used to modify the object to a well defined statewell defined state

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

ExampleExample

class Time{class Time{

public:public:

Time() const {} Time() const {} //error…//error…

~Time() const {} ~Time() const {} //error…//error…

};};

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

constconst FunctionFunction

►Constant member function Constant member function cannot change data cannot change data membermember

►Constant member function Constant member function cannot access non-constant cannot access non-constant member functionsmember functions

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

ExampleExample

class Student{class Student{char * name;char * name;

public:public:char *getName();char *getName();void setName(char * aName);void setName(char * aName);int ConstFunc() const{int ConstFunc() const{

name = getName();name = getName();//error//errorsetName(“Ahmad”);//errorsetName(“Ahmad”);//error

}}};};

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

this Pointer and const Member this Pointer and const Member FunctionFunction

►this pointer is passed as this pointer is passed as constant pointer to const data constant pointer to const data in case of constant member in case of constant member functionsfunctions

const Student *const this;const Student *const this;

instead ofinstead of

Student * const this;Student * const this;