Computer Notes - Destructor

0

No comments posted yet

Comments

Slide 1

Destructor http://ecomputernotes.com

Slide 2

Review Copy constructors Destructor Accessor Functions this Pointer http://ecomputernotes.com

Slide 3

this Pointer There are situations where designer wants to return reference to current object from a function In such cases reference is taken from this pointer like (*this) http://ecomputernotes.com

Slide 4

Example Student Student::setRollNo(int aNo) { … return *this; } Student Student::setName(char *aName) { … return *this; } http://ecomputernotes.com

Slide 5

Example int main() { Student aStudent; Student bStudent; bStudent = aStudent.setName(“Ahmad”); … bStudent = aStudent.setName(“Ali”).setRollNo(2); return 0; } http://ecomputernotes.com

Slide 6

Separation of interface and implementation Public member function exposed by a class is called interface Separation of implementation from the interface is good software engineering http://ecomputernotes.com

Slide 7

Complex Number There are two representations of complex number Euler form z = x + i y Phasor form z = |z| (cos  + i sin ) z is known as the complex modulus and  is known as the complex argument or phase

Slide 8

Example

Slide 9

Example class Complex{ //old float x; float y; public: void setNumber(float i, float j){ x = i; y = j; } … }; http://ecomputernotes.com

Slide 10

Example class Complex{ //new float z; float theta; public: void setNumber(float i, float j){ theta = arctan(j/i); … } … }; http://ecomputernotes.com

Slide 11

Advantages User is only concerned about ways of accessing data (interface) User has no concern about the internal representation and implementation of the class http://ecomputernotes.com

Slide 12

Separation of interface and implementation Usually functions are defined in implementation files (.cpp) while the class definition is given in header file (.h) Some authors also consider this as separation of interface and implementation http://ecomputernotes.com

Slide 13

Student.h class Student{ int rollNo; public: void setRollNo(int aRollNo); int getRollNo(); … }; http://ecomputernotes.com

Slide 14

Student.cpp #include “student.h” void Student::setRollNo(int aNo){ … } int Student::getRollNo(){ … } http://ecomputernotes.com

Slide 15

Driver.cpp #include “student.h” int main(){ Student aStudent; } http://ecomputernotes.com

Slide 16

There are functions that are meant to be read only There must exist a mechanism to detect error if such functions accidentally change the data member const Member Functions http://ecomputernotes.com

Slide 17

Keyword const is placed at the end of the parameter list const Member Functions http://ecomputernotes.com

Slide 18

const Member Functions Declaration: class ClassName{ ReturnVal Function() const; }; Definition: ReturnVal ClassName::Function() const{ … } http://ecomputernotes.com

Slide 19

Example class Student{ public: int getRollNo() const { return rollNo; } }; http://ecomputernotes.com

Slide 20

const Functions Constant member functions cannot modify the state of any object They are just “read-only” Errors due to typing are also caught at compile time http://ecomputernotes.com

Slide 21

Example bool Student::isRollNo(int aNo){ if(rollNo = = aNo){ return true; } return false; } http://ecomputernotes.com

Slide 22

Example bool Student::isRollNo(int aNo){ /*undetected typing mistake*/ if(rollNo = aNo){ return true; } return false; } http://ecomputernotes.com

Slide 23

Example bool Student::isRollNo (int aNo)const{ /*compiler error*/ if(rollNo = aNo){ return true; } return false; } http://ecomputernotes.com

Slide 24

const Functions Constructors and Destructors cannot be const Constructor and destructor are used to modify the object to a well defined state http://ecomputernotes.com

Slide 25

Example class Time{ public: Time() const {} //error… ~Time() const {} //error… }; http://ecomputernotes.com

Slide 26

const Function Constant member function cannot change data member Constant member function cannot access non-constant member functions http://ecomputernotes.com

Slide 27

Example class Student{ char * name; public: char *getName(); void setName(char * aName); int ConstFunc() const{ name = getName(); //error setName(“Ahmad”);//error } }; http://ecomputernotes.com

Slide 28

this Pointer and const Member Function this pointer is passed as constant pointer to const data in case of constant member functions const Student *const this; instead of Student * const this; http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Destructor in Object oriented Programming what is Destructor Explain about it in detail .explain it with example

Tags: computer notes destructor

URL:
More by this User
Most Viewed