Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.

Slides:



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

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
Structures Spring 2013Programming and Data Structure1.
Sort the given string, without using string handling functions.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Classes and Objects Presented by: Gunjan Chhabra.
Introduction to C language
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Welcome to Classes and Objects Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )TGT(CS) KV JHAGRAKHAND.
Protectedly Inherited Base Class  When the access specifier of the base class in the derived class definition is protected, the base class is protectedly.
Inheritance Examples. Example Of Multiple Inheritance class personnel { protected: char name[30]; char addr[30]; char [30]; char birth_date[30];
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.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
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?
Function Overloading and References
Chapter -6 Polymorphism
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Classes, Interfaces and Packages
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.
Methods.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Operator Overloading.
Operator Overloading Ritika Sharma.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Friend functions.
2 Chapter Classes & Objects.
Programming with ANSI C ++
Friend Function.
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 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.
CONSTRUCTORS & DESTRUCTORS
Classes & Objects.
Concepts and Basics of C++ Programming
Concepts and Basics of C++ Programming
Group Status Project Status.
Learning Objectives Classes Constructors Principles of OOP
Passing objects As arguments
Friend Functions.
Dr. Bhargavi Dept of CS CHRIST
Introduction to Programming
Classes and Objects.
Operator Overloading.
CIS 199 Final Review.
Submitted By : Veenu Saini Lecturer (IT)
C++ Programming CLASS This pointer Static Class Friend Class
ITE “A” GROUP 2 ENCAPSULATION.
Object Oriented Programming (OOP) Lecture No. 12
Chapter 5 Classes.
Presentation transcript:

Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class

FRIEND FUNCTIONS Why it is used? - The concept behind the encapsulation and data hiding restrict the non-member function from accessing the private members of the class. The best way to access a private data member by a non- member function is to change a private data member to a public. but it violates the whole concept of data hiding and encapsulation. To solve this problem friend functions are used when we can access the private or protected members.

Friend informs the compiler that the function is not a member func.Of the class. Syntax : friend func_name(arguments); Member FunctionFriend Function It is a part of class definition and is invoked by a particular object It is no a part of a class It can access the data depending upon whether the function is private or protected It can access any data irrespective of the private or public or protected access

#include class book { private : int bno; char bname[20]; public: void getdata(); friend void show(book); }; void book::getdata() { cin>>bno>>bname; } void show(book bk) { cout<<bk.bno<<bk.bname; } void main() { book b; clrscr(); b.getdata(); show(b); getch(); }

FRIEND FUNCTIONS To access the private member of the class inside the friend function the object of the class should be passed as an argument to the function #include class second; class first { private : int no; public: first(int n); friend int add(first,second); }; class second {

private : int n1; public: second(int); friend int add(first,second); }; first ::first(int n) { no=n; } second ::second(int n) { n1=n; } int add(first f,second s) { cout<<"First class="<<f.no<<endl; cout<<"Second class=“ <<s.n1; return f.no+s.n1; } void main() { first f(10); second s(20); clrscr(); cout<<"\n The Result is "<<add(f,s); }

FRIEND CLASS The private members of one class can be accessed from the member functions Of another class by making them as friends #include class second; class first { private : int no; public: friend class second; first(int n) {

no=n; }}; class second { public: void show(first f) { cout<<f.no; } }; void main() { first f(10); second s; s.show(f); getch(); }

 The friend function acts as a bridge between two objects of different classes which can access even the private members of the class  A friend class is a class whose members functions are all friend functions of another class

EXERCISES  Define “friend “ keyword & its use?  Difference between friend function and member functions?  Explain the use of friend function?  Discuss about friend class?