1 / 28

Object Oriented Programming (OOP) Lecture No. 10

Object Oriented Programming (OOP) Lecture No. 10. Copy constructors Destructor Accessor Functions this Pointer. Review. 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).

ecunha
Download Presentation

Object Oriented Programming (OOP) Lecture No. 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Copy constructors Destructor Accessor Functions this Pointer Review

  3. 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) this Pointer

  4. Student Student::setRollNo(int aNo) { … return *this; } Student Student::setName(char *aName) { … return *this; } Example

  5. int main() { Student aStudent; Student bStudent; bStudent = aStudent.setName(“Ahmad”); … bStudent = aStudent.setName(“Ali”).setRollNo(2); return 0; } Example

  6. Public member function exposed by a class is called interface Separation of implementation from the interface is good software engineering Separation of interface and implementation

  7. 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 Complex Number

  8. Old implementation New implementation Complex Complex float x float y float z float theta float getX() float getY() void setNumber (float i, float j) … float getX() float getY() void setNumber (float i, float j) … Example

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

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

  11. User is only concerned about ways of accessing data (interface) User has no concern about the internal representation and implementation of the class Advantages

  12. 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 Separation of interface and implementation

  13. class Student{ int rollNo; public: void setRollNo(int aRollNo); int getRollNo(); … }; Student.h

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

  15. #include “student.h” int main(){ Student aStudent; } Driver.cpp

  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

  17. Keyword constis placed at the end of the parameter list const Member Functions

  18. Declaration: class ClassName{ ReturnVal Function() const; }; Definition: ReturnVal ClassName::Function() const{ … } const Member Functions

  19. class Student{ public: int getRollNo() const { return rollNo; } }; Example

  20. 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 const Functions

  21. bool Student::isRollNo(int aNo){ if(rollNo == aNo){ return true; } return false; } Example

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

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

  24. Constructors and Destructors cannot be const Constructor and destructor are used to modify the object to a well defined state const Functions

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

  26. Constant member function cannot change data member Constant member function cannot access non-constant member functions constFunction

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

  28. 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; this Pointer and const Member Function

More Related