this Pointer Scope Resolution Operator Friend Function

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Computer Science 1620 Function Scope & Global Variables.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Learners Support Publications Classes and Objects.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CSC241: Object Oriented Programming Lecture No 08.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
C++ : Operator overloading example
Eine By: Avinash Reddy 09/29/2016.
Friend functions.
Submission Example May 14, 2018
Two or more functions can have the same name but different parameters
Chapter 1.2 Introduction to C++ Programming
Class and Objects UNIT II.
OBJECT ORIENTED PROGRAMMING
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Object Oriented Programming With C++
Polymorphism in C++ Operator Overloading
School of EECS, Peking University
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Pointers and Pointer-Based Strings
group work #hifiTeam
Polymorphism Lec
Static Data Member and Functions
Lecture 4-7 Classes and Objects
Lecture 8 – 9 Arrays with in a class
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Dynamic Memory Allocation Reference Variables
Contents Introduction to Constructor Characteristics of Constructor
Pointers & Functions.
Learning Objectives Classes Constructors Principles of OOP
Classes and Objects.
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
Operator Overloading.
Local, Global Variables, and Scope
Method of Classes Chapter 7, page 155 Lecture /4/6.
Pointers and Pointer-Based Strings
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Using string type variables
Pointers & Functions.
CS 144 Advanced C++ Programming February 21 Class Meeting
C++ Class Members Class Definition class Name { public: constructor(s)
Class: Special Topics Overloading (methods) Copy Constructors
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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