Upload
gabriella-mccormick
View
228
Download
4
Tags:
Embed Size (px)
Citation preview
Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)
Lecture No. 10Lecture No. 10
ReviewReview
►Copy constructorsCopy constructors►DestructorDestructor►Accessor FunctionsAccessor Functions►this Pointerthis Pointer
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)
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;
}}
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;}}
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
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
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
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;
}}……
};};
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);……
}}……
};};
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
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
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();……
};};
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(){……}}
Driver.cppDriver.cpp
#include “student.h”#include “student.h”
int main(){int main(){Student aStudent;Student aStudent;
}}
►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
►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
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{……
}}
ExampleExample
class Student{class Student{
public:public:
int getRollNo() int getRollNo() constconst {{
return rollNo;return rollNo;
}}
};};
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
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;
}}
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;
}}
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;
}}
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
ExampleExample
class Time{class Time{
public:public:
Time() const {} Time() const {} //error…//error…
~Time() const {} ~Time() const {} //error…//error…
};};
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
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
}}};};
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;