8/3/2019 Computer Notes - Destruct Or
1/28
DestructorDestructor
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
2/28
ReviewReview
Copy constructorsCopy constructorsDestructorDestructor
Accessor FunctionsAccessor Functionsthis Pointerthis Pointer
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
3/28
this Pointerthis PointerThere are situations whereThere are situations where
designer wants to returndesigner wants to returnreference to current object fromreference to current object from
a functiona functionIn such cases reference is takenIn such cases reference is taken
from this pointer like (*this)from this pointer like (*this)
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
4/28
ExampleExampleStudentStudent Student::setRollNo(intStudent::setRollNo(int aNoaNo))
{{
return *this;return *this;
}}StudentStudent Student::setName(charStudent::setName(char **aNameaName))
{{
return *this;return *this;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
5/28
ExampleExampleintintmain()main()
{{
StudentStudent aStudentaStudent;;
StudentStudentbStudentbStudent;;
bStudentbStudent == aStudent.setName(aStudent.setName(AhmadAhmad););
bStudentbStudent = aStudent.setName(= aStudent.setName(AliAli).setRollNo(2);).setRollNo(2);
return 0;return 0;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
6/28
Separation of interface andSeparation of interface and
implementationimplementation
Public member function exposed by aPublic member function exposed by aclass is called interfaceclass is called interface
Separation of implementation from theSeparation of implementation from the
interface is good software engineeringinterface is good software engineering
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
7/28
Complex NumberComplex Number
There are two representations ofThere are two representations ofcomplex numbercomplex number Euler formEuler form
z = x +z = x + ii yy PhasorPhasor formform
z = |z| (z = |z| (coscos
++ii
sinsin
))z is known as the complex modulus andz is known as the complex modulus and is known as the complex argument oris known as the complex argument or
phasephase
8/3/2019 Computer Notes - Destruct Or
8/28
ExampleExample
floatfloat getXgetX()()
floatfloat getYgetY()()
voidvoid setNumbersetNumber
(float i, float j)(float i, float j)
float xfloat x
float yfloat y
ComplexComplex
Old implementationOld implementation
floatfloat getXgetX()()
floatfloat getYgetY()()
voidvoid setNumbersetNumber
(float i, float j)(float i, float j)
float zfloat z
float thetafloat theta
ComplexComplex
New implementationNew implementation
8/3/2019 Computer Notes - Destruct Or
9/28
ExampleExampleclass Complex{ //oldclass Complex{ //old
float x;float x;
float y;float y;
public:public:
voidvoidsetNumber(floatsetNumber(float i, float j){i, float j){
x = i;x = i;
y = j;y = j;
}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
10/28
ExampleExampleclass Complex{ //newclass Complex{ //new
float z;float z;
float theta;float theta;
public:public:
voidvoidsetNumber(floatsetNumber(float i, float j){i, float j){
theta =theta = arctan(j/iarctan(j/i););
}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
11/28
AdvantagesAdvantages
User is only concerned about ways ofUser is only concerned about ways of
accessing data (interface)accessing data (interface)
User has no concern about theUser has no concern about the
internal representation andinternal representation and
implementation of the classimplementation of the class
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
12/28
Separation of interface andSeparation of interface and
implementationimplementation
Usually functions are defined inUsually functions are defined in
implementation files (.implementation files (.cppcpp) while the) while the
class definition is given in header fileclass definition is given in header file
(.h)(.h)
Some authors also consider this asSome authors also consider this as
separation of interface andseparation of interface andimplementationimplementation
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
13/28
Student.hStudent.hclass Student{class Student{
intint rollNorollNo;;public:public:
voidvoidsetRollNo(intsetRollNo(int aRollNoaRollNo););
intint getRollNogetRollNo();();
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
14/28
Student.cppStudent.cpp#include#include student.hstudent.h
voidvoidStudent::setRollNo(intStudent::setRollNo(int aNoaNo){){
}}intint Student::getRollNoStudent::getRollNo(){(){
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
15/28
Driver.cppDriver.cpp#include#include student.hstudent.h
intintmain(){main(){
StudentStudent aStudentaStudent;;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
16/28
There are functions that areThere are functions that are
meant to be read onlymeant to be read only
There must exist a mechanism toThere must exist a mechanism todetect error if such functionsdetect error if such functions
accidentally change the dataaccidentally change the datamembermember
constconst Member FunctionsMember Functions
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
17/28
KeywordKeyword constconst is placed at theis placed at the
end of the parameter listend of the parameter list
constconst Member FunctionsMember Functions
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
18/28
constconst Member FunctionsMember Functions
Declaration:Declaration:
classclass ClassNameClassName{{ReturnValReturnVal Function() const;Function() const;
};};
Definition:Definition:
ReturnValReturnVal ClassName::FunctionClassName::Function() const{() const{
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
19/28
ExampleExampleclass Student{class Student{
public:public:
intint getRollNogetRollNo()() constconst{{
returnreturn rollNorollNo;;}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
20/28
constconst FunctionsFunctions
Constant member functions cannotConstant member functions cannot
modify the state of any objectmodify the state of any object
They are justThey are just readread--onlyonly
Errors due to typing are also caught atErrors due to typing are also caught at
compile timecompile time
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
21/28
ExampleExampleboolbool Student::isRollNo(intStudent::isRollNo(int aNoaNo){){
if(rollNoif(rollNo == == aNoaNo){){
return true;return true;
}}return false;return false;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
22/28
ExampleExampleboolbool Student::isRollNo(intStudent::isRollNo(int aNoaNo){){
/*undetected typing mistake*//*undetected typing mistake*/
if(rollNoif(rollNo == aNoaNo){){
return true;return true;}}
return false;return false;
}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
23/28
ExampleExampleboolbool Student::isRollNoStudent::isRollNo
(int(int aNo)constaNo)const{{/*compiler error*//*compiler error*/
if(rollNoif(rollNo == aNoaNo){){
return true;return true;
}}
return false;return false;}}
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
24/28
constconst FunctionsFunctions
Constructors and Destructors cannotConstructors and Destructors cannotbebe constconst
Constructor and destructor are usedConstructor and destructor are used
to modify the object to a well definedto modify the object to a well defined
statestate
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
25/28
ExampleExampleclass Time{class Time{
public:public:
Time() const {}Time() const {} //error//error
~Time() const {}~Time() const {} //error//error
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
26/28
constconst FunctionFunction
Constant member function cannotConstant member function cannot
change data memberchange data member
Constant member function cannotConstant member function cannotaccess nonaccess non--constant memberconstant member
functionsfunctions
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
27/28
ExampleExampleclass Student{class Student{
char * name;char * name;public:public:
char *char *getNamegetName();();
voidvoidsetName(charsetName(char ** aNameaName););intint ConstFuncConstFunc() const{() const{
name =name = getNamegetName();();//error//error
setName(setName(
AhmadAhmad
);//error);//error
}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Destruct Or
28/28
this Pointer and const Member Functionthis Pointer and const Member Function
this pointer is passed as constantthis pointer is passed as constant
pointer to const data in case ofpointer to const data in case of
constant member functionsconstant member functions
const Student *const this;const Student *const this;
instead ofinstead of
Student * const this;Student * const this;
http://ecomputernotes.com
Recommended