Lecture 12
• this Pointer
• Scope Resolution Operator
• Friend Function
this pointer
• Member functions of every object have access to a
special constant pointer named this.
• It points to the object itself.
• It is passed automatically as an implicit argument to
the invoking member function.
09/22/2020 UTA009 2
class ABC { int a; public:
void set()
{ a = 10; } };
class ABC { int a; public:
void set()
Example
1. #include <iostream> 2. using namespace std; 3. class ABC
4. { private:char charray[10]; 5. public: void reveal()
6. { cout << "\nMy object's address is " << this; } 7. };
8. int main()
9. { ABC w1, w2;
10. w1.reveal();
11. w2.reveal();
12. cout << endl; 13. return 0;
14. }
Contd…
• Static member functions do not have 'this' pointer
as they can be called without any object using the class name.
• Not passed to friend functions as they are not
member functions of a class.
• Used when a binary operator is overloaded using a
member function.
• Used to return the object it points to.
return 0;
Example
1. #include<iostream> 2. #include<cstring>
3. using namespace std; 4. class person
5. { char name[20]; 6. int age;
7. public:
8. void setData(const char *s, int a) 9. { strcpy(name, s);
10. age = a; }
11. person& greater(person &x) 12. { if(x.age >= age) return x;
13. else return *this; }
14. void display()
15. { cout << name << " with age " << age; }
Contd…
17. int main()
18. { person p1,p2,p3;
19. p1.setData("Abha", 21); 20. p2.setData("Akhil", 29); 21. p3.setData("Mitali", 31);
22. person p = p1.greater(p2);
23. cout << "Elder person is: ";
24. p.display();
25. p = p2.greater(p3);
26. cout << endl << "Elder person is: ";
27. p.display();
28. return 0;
29. }
09/22/2020 UTA009 6
Elder person is: Akhil with age 29 Elder person is: Mitali with age 31
Scope resolution operator (::)
• It links class name with member name and tells
compiler what class the member belongs to.
• It allows access to a name in an enclosing scope
that is "hidden" by a local declaration of the same name.
• Example:
09/22/2020 UTA009 7
int I; //Global
void f()
{ int I; //Local
I = 10; //Local
Example
1. #include<iostream> 2. using namespace std; 3. int m = 10;
4. int main()
5. { int m = 20;
6. { int k = m;
7. int m = 30;
8. cout << "Inner block: k = " << k << ", m = "; 9. cout << m << ", ::m = " << ::m;
10. }
11. cout << endl << "Outer block: m = " << m << ", ::m = " << ::m;
12. return 0; }
Thapar University UTA007 - Computer Programming I 8
Inner block: k = 20, m = 30, ::m = 10 Outer block: m = 20, ::m = 10
Friend Function
• A function that can access the private and protected
members of a class to which it is a friend.
• It is not in the scope of a class to which it is a friend. • It cannot be called using the object of a class to
which it is a friend.
– Invoked like a normal function.
• Has to be declared either in the public or the
private section within a class (to which it is a friend)
preceded with a keyword friend.
Contd…
• Defined elsewhere in the program like a normal C++
function.
• Can be a simple function or a member function of
some other class.
• Unlike member functions, it cannot access the
member names directly and has to use an object name and dot membership operator with each member name.
• Usually, it has the objects as arguments.
• Best suited in operator overloading.
Example
1. #include <iostream> 2. using namespace std; 3. class myclass
4. { int a, b; 5. public:
6. friend int sum(myclass x); 7. void set_ab(int i, int j); 8. };
9. void myclass::set_ab(int i, int j) 10.{ a = i; b = j; }
11.int sum(myclass x) 12.{ return x.a + x.b; }
09/22/2020 UTA009 11
13. int main()
14. { myclass n;
15. n.set_ab(3, 4); 16. cout <<
sum(n);
17. return 0; 18. }
7
Friend of more than one class
1. #include <iostream> 2. using namespace std; 3. const int IDLE = 0; 4. const int INUSE = 1;
5. class C2; // forward declaration 6. class C1 {
7. int status; 8. public:
9. void set_status(int state); 10. friend int idle(C1 a, C2 b); 11. };
12. class C2 {
13. int status; 14. public:
15. void set_status(int state); 16. friend int idle(C1 a, C2 b);
17. };Thapar University UTA007 - Computer Programming I 12
18. void C1::set_status(int state) 19. { status = state; }
20. void C2::set_status(int state) 21. { status = state; }
22. int idle(C1 a, C2 b)
23. { if(a.status || b.status) 24. return 0;
25. else
Contd…
27. int main() 28. { C1 x; 29. C2 y;
30. x.set_status(IDLE); 31. y.set_status(IDLE); 32. if(idle(x, y))
33. cout << "Screen can be used.\n"; 34. else
35. cout << "In use.\n"; 36. x.set_status(INUSE); 37. if(idle(x, y))
38. cout << "Screen can be used.\n"; 39. else
40. cout << "In use.\n"; 41. return 0;
42. }09/22/2020 UTA009 13
Screen can be used. In use.
Class member as a friend function
1. #include <iostream> 2. using namespace std; 3. const int IDLE = 0; 4. const int INUSE = 1;
5. class C2; // forward declaration 6. class C1 {
7. int status; 8. public:
9. void set_status(int state); 10. int idle(C2 b);
11. };
12. class C2 {
13. int status; 14. public:
15. void set_status(int state); 16. friend int C1::idle(C2 b);
17. };Thapar University UTA007 - Computer Programming I 14
18. void C1::set_status(int state) 19. { status = state; }
20. void C2::set_status(int state) 21. { status = state; }
22. int C1::idle(C2 b)
23. { if(status || b.status) 24. return 0;
25. else
Contd…
27. int main() 28. { C1 x; 29. C2 y;
30. x.set_status(IDLE); 31. y.set_status(IDLE); 32. if(x.idle(y))
33. cout << "Screen can be used.\n"; 34. else
35. cout << "In use.\n"; 36. x.set_status(INUSE); 37. if(x.idle(y))
38. cout << "Screen can be used.\n"; 39. else
40. cout << "In use.\n"; 41. return 0;
42. }09/22/2020 UTA009 15
Screen can be used. In use.
Friend Class
• A class can also be made a friend of another class. • Example:
• All the member functions of a friend class A become friend of class B.
• Any member function of class A can access the private data of class
B.
• But, member functions of class B cannot access the private data of
class A.
Thapar University UTA007 - Computer Programming I 16
class B { ...
friend class A; ... };
Example
1. #include <iostream> 2. using namespace std; 3. class TwoValues {
4. int a, b;
5. public:
6. TwoValues(int i, int j) { a = i; b = j; }
7. friend class Min;
8. };
9. class Min { 10. public:
11. int min(TwoValues x);
12. };
13. int Min::min(TwoValues x)
14. { return x.a < x.b ? x.a : x.b; }
Thapar University UTA007 - Computer Programming I 17
10
Output
15. int main()
16. { TwoValues ob(10,
20);
17. Min m;
18. cout << m.min(ob);
19. return 0;