partment of Computer Science Engineer Air University Object

partment of Computer Science & Engineer Air University Object Oriented Programming Week # 7 Lecture # 12 By: Saqib Rasheed

this Pointer q. Every object in C++ has access to its own address through an important pointer called this pointer qwhich points to the object itself. q. Thus any member function can find out the address of the object of which it is a member
![Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout](http://slidetodoc.com/presentation_image_h2/06c7af8d365fa29c39a8816bd3318c46/image-3.jpg)
Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout << “n. My object’s address is “ << &charray; } }; int main() { //make three objects where w 1, w 2, w 3; //see where they are w 1. reveal(); w 2. reveal(); w 3. reveal(); return 0; }
![Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout](http://slidetodoc.com/presentation_image_h2/06c7af8d365fa29c39a8816bd3318c46/image-4.jpg)
Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout << “n. My object’s address is “ << this; } }; int main() { //make three objects where w 1, w 2, w 3; //see where they are w 1. reveal(); w 2. reveal(); w 3. reveal(); return 0; }
![Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout](http://slidetodoc.com/presentation_image_h2/06c7af8d365fa29c39a8816bd3318c46/image-5.jpg)
Example class where { private: //occupies 10 bytes charray[10]; public: void reveal() { cout << “n. My object’s address is “ << this; } }; this will do the same work int main() { //make three objects where w 1, w 2, w 3; //see where they are w 1. reveal(); w 2. reveal(); w 3. reveal(); return 0; }

Explanation…. q. The main() program in this example creates three objects of type where. q. It then asks each object to print its address, using the reveal() member function. q. This function prints out the value of the this pointer.

Example 2 class what { private: int alpha; public: void tester() { alpha = 11; cout << alpha; } }; int main() { what w; w. tester(); cout << endl; return 0; }

Example 2 class what { private: int alpha; public: void tester() { this->alpha = 11; cout << this->alpha; } }; int main() { what w; w. tester(); cout << endl; return 0; }

Explanation… q. When you call a member function, it comes into existence with the value of this set to the address of the object for which it was called. q. The this pointer can be treated like any other pointer to an object q. Thus be used to access the data in the object it points to

Use of this Pointer § We only need to use this pointer explicitly in limited circumstances. a) We might use this explicitly to resolve ambiguity; If a function parameter had the same name as a data member. b) We will also use this if we want to return the address of the current object from a member function. 10

Example class Something { private: int n. Data; public: void set. Value(int n. Data); int get. Value(); }; void Something: : set. Value(int n. Data) { this->n. Data = n. Data; } int Something: : get. Value() { return n. Data; } int main() { Something obj; obj. set. Value(5); cout << obj. get. Value(); return 0; }

Splitting code Header file myclass. h class myclass { private: int x; public: void set. Value(int); void getvalues(); int get. Value(); }; Definition file something. cpp #include <iostream> #include "myclass. h" using namespace std; void myclass: : set. Value(int y){ x = y; } void myclass: : getvalues(){ cout<<"Enetr value of x ="; cin>>x; } int myclass: : get. Value() { return x; } Driver file main() Something. cpp #include <iostream> #include "myclass. h" using namespace std; int main() { myclass obj 1, obj 2; obj 1. set. Value(5); obj 2. getvalues(); cout <<"Obj 1 ="<< obj 1. get. Value() cout <<"Obj 2 ="<< obj 2. get. Value() << endl; } return 0;

Example /* Header File (something. h) */ #ifndef SOMETHING_H #define SOMETHING_H class Something { private: int n. Data; public: void set. Value(int n. Data); int get. Value(); }; Note: The Data Member and Function Parameter have same name. #endif 13

Example (cont’d) // Implementation for Something class (something. cpp) #include <iostream> #include "something. h" using namespace std; void Something: : set. Value(int n. Data) { this->n. Data = n. Data; } int Something: : get. Value() { return n. Data; } Note: By using this we are telling the compiler that n. Data belongs to the object for which this member function is called. 14

Example (cont’d) /* Test Driver Program for Point class (Test. Point. cpp) */ #include <iostream> #include "something. h" using namespace std; int main() { Something obj; obj. set. Value(5); cout << obj. get. Value() << endl; return 0; } 15

Example (cont’d) § Output: 16
- Slides: 16