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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
EC-241 Object-Oriented Programming
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 C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
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.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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?
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
CITA 342 Section 1 Object Oriented Programming (OOP)
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Operator Overloading Ritika Sharma.
Friend functions.
Access Functions and Friend Functions
Friend Function.
Class and Objects UNIT II.
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.
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?
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 15: Overloading and Templates
Anatomy of a class Part I
this Pointer Scope Resolution Operator Friend Function
Static Data Member and Functions
Lecture 4-7 Classes and Objects
Contents Introduction to Constructor Characteristics of Constructor
Code Organization Classes
Friend Functions.
Classes and Objects.
Static in Classes CSCE 121 J. Michael Moore.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Submitted By : Veenu Saini Lecturer (IT)
Types of Computer Languages
Code Organization Classes
Constructors & Destructors
Anatomy of a class Part I
Object Oriented Programming (OOP) Lecture No. 12
Scope Rules.
Object Oriented Programming
Presentation transcript:

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 to the private data of a class. C++ support a special function to access private data of class called friend function or friendly fun.

3 Friend Function A friend function is used to access private data in a class from outside the class. A friend function is not a member of a class but has access to the class's private members. To make a fun friend, the fun declaration should be preceded by the keyword friend

4 Friend Function Class s4 { …. public: friend void show();// declaration }; Void show() // definition { ….. }

5 Friend Function Friend fun def does not need the keyword and scope resolution opr (::) Friend fun are not in the scope of any class, so they cannot invoked using class object. They invoked like an ordinary function in c language A friend can be declared in any number of classes.

6 Friend Function 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); }

7 Friend Function Characteristics The keyword friend is placed only in the function declaration of the friend function and not in the function definition. It is possible to declare a function as friend in any number of classes. When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend. A friend function, even though it is not a member function, would have the rights to access the private members of the class. It is possible to declare the friend function as either private or public. The function cannot be invoked with class object. The friend function has its argument as objects.