Presentation is loading. Please wait.

Presentation is loading. Please wait.

this Pointer Scope Resolution Operator Friend Function

Similar presentations


Presentation on theme: "this Pointer Scope Resolution Operator Friend Function"— Presentation transcript:

1 this Pointer Scope Resolution Operator Friend Function
Lecture 12 this Pointer Scope Resolution Operator Friend Function 9/21/2018 UTA009

2 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 { int a; public: void set() { a = 10; } }; { this->a = 10; } 9/21/2018 UTA009

3 Example #include <iostream> using namespace std; class ABC
{ private: char charray[10]; public: void reveal() { cout << "\nMy object's address is " << this; } }; int main() { ABC w1, w2; w1.reveal(); w2.reveal(); cout << endl; return 0; } 9/21/2018 UTA009

4 UTA007 - Computer Programming I
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

5 Example #include<iostream> #include<cstring>
using namespace std; class person { char name[20]; int age; public: void setData(const char *s, int a) { strcpy(name, s); age = a; } person& greater(person &x) { if(x.age >= age) return x; else return *this; } void display() { cout << name << " with age " << age; } }; 9/21/2018 UTA009

6 Contd… Output Elder person is: Akhil with age 29
int main() { person p1,p2,p3; p1.setData("Abha", 21); p2.setData("Akhil", 29); p3.setData("Mitali", 31); person p = p1.greater(p2); cout << "Elder person is: "; p.display(); p = p2.greater(p3); cout << endl << "Elder person is: "; return 0; } Output Elder person is: Akhil with age 29 Elder person is: Mitali with age 31 9/21/2018 UTA009

7 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 } 9/21/2018 UTA009

8 UTA007 - Computer Programming I
Example #include<iostream> using namespace std; int m = 10; int main() { int m = 20; { int k = m; int m = 30; cout << "Inner block: k = " << k << ", m = "; cout << m << ", ::m = " << ::m; } cout << endl << "Outer block: m = " << m << ", ::m = " << ::m; return 0; } Output Inner block: k = 20, m = 30, ::m = 10 Outer block: m = 20, ::m = 10 Thapar University UTA007 - Computer Programming I

9 UTA007 - Computer Programming I
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

10 UTA007 - Computer Programming I
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

11 Example Output 7 #include <iostream> using namespace std;
class myclass { int a, b; public: friend int sum(myclass x); void set_ab(int i, int j); }; void myclass::set_ab(int i, int j) { a = i; b = j; } int sum(myclass x) { return x.a + x.b; } int main() { myclass n; n.set_ab(3, 4); cout << sum(n); return 0; } Output 7 9/21/2018 UTA009

12 Friend of more than one class
#include <iostream> using namespace std; const int IDLE = 0; const int INUSE = 1; class C2; // forward declaration class C1 { int status; public: void set_status(int state); friend int idle(C1 a, C2 b); }; class C2 { void C1::set_status(int state) { status = state; } void C2::set_status(int state) int idle(C1 a, C2 b) { if(a.status || b.status) return 0; else return 1; } Thapar University UTA007 - Computer Programming I

13 Contd… Output Screen can be used. In use. int main() { C1 x; C2 y;
x.set_status(IDLE); y.set_status(IDLE); if(idle(x, y)) cout << "Screen can be used.\n"; else cout << "In use.\n"; x.set_status(INUSE); return 0; } Output Screen can be used. In use. 9/21/2018 UTA009

14 Class member as a friend function
#include <iostream> using namespace std; const int IDLE = 0; const int INUSE = 1; class C2; // forward declaration class C1 { int status; public: void set_status(int state); int idle(C2 b); }; class C2 { friend int C1::idle(C2 b); void C1::set_status(int state) { status = state; } void C2::set_status(int state) int C1::idle(C2 b) { if(status || b.status) return 0; else return 1; } Thapar University UTA007 - Computer Programming I

15 Contd… Output Screen can be used. In use. int main() { C1 x; C2 y;
x.set_status(IDLE); y.set_status(IDLE); if(x.idle(y)) cout << "Screen can be used.\n"; else cout << "In use.\n"; x.set_status(INUSE); return 0; } Output Screen can be used. In use. 9/21/2018 UTA009

16 UTA007 - Computer Programming I
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. class A { }; class B { ..... friend class A; }; Thapar University UTA007 - Computer Programming I

17 UTA007 - Computer Programming I
Example #include <iostream> using namespace std; class TwoValues { int a, b; public: TwoValues(int i, int j) { a = i; b = j; } friend class Min; }; class Min { int min(TwoValues x); int Min::min(TwoValues x) { return x.a < x.b ? x.a : x.b; } Output 10 int main() { TwoValues ob(10, 20); Min m; cout << m.min(ob); return 0; } Thapar University UTA007 - Computer Programming I


Download ppt "this Pointer Scope Resolution Operator Friend Function"

Similar presentations


Ads by Google