Lecture 12
• this Pointer
• Scope Resolution Operator
• Friend Function
1/21/2018 UTA009 1
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.
class ABC class ABC
{ int a; { int a;
public: public:
void set() void set()
{ a = 10; } { this->a = 10; }
1/21/2018 }; UTA009 }; 2
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. [Link]();
11. [Link]();
12. cout << endl;
13. return 0;
14. }
1/21/2018 UTA009 3
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;
Thapar University UTA007 - Computer Programming I 4
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([Link] >= age) return x;
13. else return *this; }
14. void display()
15. { cout << name << " with age " << age; }
16. };
1/21/2018 UTA009 5
Contd…
17. int main()
18. { person p1,p2,p3;
19. [Link]("Abha", 21);
20. [Link]("Akhil", 29);
21. [Link]("Mitali", 31);
22. person p = [Link](p2);
23. cout << "Elder person is: ";
24. [Link]();
25. p = [Link](p3);
26. cout << endl << "Elder person is: "; Output
27. [Link]();
28. return 0; Elder person is: Akhil with age 29
29. } Elder person is: Mitali with age 31
1/21/2018 UTA009 6
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: int I; //Global
void f()
{ int I; //Local
I = 10; //Local
::I = 20; //Global }
1/21/2018 UTA009 7
Example
1. #include<iostream>
2. using namespace std; Output
3. int m = 10;
4. int main() Inner block: k = 20, m = 30, ::m = 10
5. { int m = 20; Outer block: m = 20, ::m = 10
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
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.
Thapar University UTA007 - Computer Programming I 9
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.
Thapar University UTA007 - Computer Programming I 10
Example
1. #include <iostream> 13. int main()
2. using namespace std; 14. { myclass n;
3. class myclass 15. n.set_ab(3, 4);
4. { int a, b; 16. cout <<
5. public: sum(n);
6. friend int sum(myclass x); 17. return 0;
7. void set_ab(int i, int j); 18. }
8. };
9. void myclass::set_ab(int i, int j)
10. { a = i; b = j; } Output
11. int sum(myclass x) 7
12. { return x.a + x.b; }
1/21/2018 UTA009 11
Friend of more than one class
1. #include <iostream> 18. void C1::set_status(int state)
2. using namespace std; 19. { status = state; }
3. const int IDLE = 0; 20. void C2::set_status(int state)
4. const int INUSE = 1; 21. { status = state; }
5. class C2; // forward declaration 22. int idle(C1 a, C2 b)
6. class C1 { 23. { if([Link] || [Link])
7. int status; 24. return 0;
8. public: 25. else
9. void set_status(int state); 26. return 1; }
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);
Thapar University UTA007 - Computer Programming I 12
17. };
Contd…
27. int main()
28. { C1 x; Output
29. C2 y; Screen can be used.
30. x.set_status(IDLE);
31. y.set_status(IDLE); In use.
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. 1/21/2018
} UTA009 13
Class member as a friend function
1. #include <iostream> 18. void C1::set_status(int state)
2. using namespace std; 19. { status = state; }
3. const int IDLE = 0; 20. void C2::set_status(int state)
4. const int INUSE = 1; 21. { status = state; }
5. class C2; // forward declaration 22. int C1::idle(C2 b)
6. class C1 { 23. { if(status || [Link])
7. int status; 24. return 0;
8. public: 25. else
9. void set_status(int state); 26. return 1; }
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);
Thapar University UTA007 - Computer Programming I 14
17. };
Contd…
27. int main()
28. { C1 x; Output
29. C2 y;
Screen can be used.
30. x.set_status(IDLE);
31. y.set_status(IDLE); In use.
32. if([Link](y))
33. cout << "Screen can be used.\n";
34. else
35. cout << "In use.\n";
36. x.set_status(INUSE);
37. if([Link](y))
38. cout << "Screen can be used.\n";
39. else
40. cout << "In use.\n";
41. return 0;
42. 1/21/2018
} UTA009 15
Friend Class
• A class can also be made a friend of another class.
• Example:
class A class B
{ ...... { .....
}; friend class A;
..... };
• 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
Example
1. #include <iostream>
2. using namespace std;
3. class TwoValues { Output
4. int a, b; 10
5. public:
6. TwoValues(int i, int j) { a = i; b = j; }
7. friend class Min;
15. int main()
8. };
16. { TwoValues ob(10,
9. class Min {
20);
10. public:
17. Min m;
11. int min(TwoValues x);
18. cout << [Link](ob);
12. };
19. return 0;
13. int Min::min(TwoValues x)
20. }
14. { return x.a < x.b ? x.a : x.b; }
Thapar University UTA007 - Computer Programming I 17