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.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
EC-241 Object-Oriented Programming
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Operator Overloading Fundamentals
Class and Objects.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
7/2/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter 11.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 CSC241: Object Oriented Programming Lecture No 13.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Chapter 8 Operator Overloading, Friends, and References.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Chapter -6 Polymorphism
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Topics Instance variables, set and get methods Encapsulation
1 CSC241: Object Oriented Programming Lecture No 08.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
3/20/2016Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23 OOP Polymorphism Reference: R.Sebesta, Chapter 12 Lafore, Chapter.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Visit for more Learning Resources
Friend Function.
CSC241: Object Oriented Programming
Abstract Data Types and Encapsulation Concepts
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
Dr. Bhargavi Dept of CS CHRIST
Object Oriented Programming Using C++
Class: Special Topics Overloading (methods) Copy Constructors
Objects as Function Arguments
Presentation transcript:

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

4/14/2015Assoc. Prof. Stoyan Bonev2 Lecture Contents: Part 1 –Operator Overloading (lecture 23m1) Part 2 –Friend functions (lecture 23m2)

Part 1 Operator Overloading (separate lecture 23m1)

Part 2 Friend Functions

4/14/2015Assoc. Prof. Stoyan Bonev5 Friend functions Basic idea: nonmember functions TO be able to access an object’s private or protected data Reminder: The concepts of data encapsulation and data hiding dictate that nonmember functions should NOT be able to access an object’s private or protected data However, there are situations where such rigid discrimination leads to considerable inconvenience

4/14/2015Assoc. Prof. Stoyan Bonev6 Friend functions (after MSDN) A friend function is a function that is not a member of a class but has access to the class's private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges. A friend function is declared by the class that is granting access. The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.

4/14/2015Assoc. Prof. Stoyan Bonev7 Friend functions, application Normal overloaded operator + and friend overloaded operator + Friends as Bridge among classes Friend functions for functional notation

4/14/2015Assoc. Prof. Stoyan Bonev8 Friend functions, application The rule to use friend functions: –A friend function must be defined outside of any class, i.e. a global function definition. –A friend function must be declared as such within the class whose data it will access. –The reserved word is friend.

4/14/2015Assoc. Prof. Stoyan Bonev9 Friend functions, application Normal overloaded operator + and friend overloaded operator +

4/14/2015Assoc. Prof. Stoyan Bonev10 Friend function overloading operator + Reminder: binary overloaded operator + as member function Distance operator+(Distance); Distance Distance::operator+(Distance d){ int f = feet + d.feet; float in = inches + d.inches; if (in >=12.) { f++; in-=12.;} return Distance(f,in); } Distance d1(6, 5.18), d2=3.5, d3, d4, d5; d3 = d1 + d2;d4 = d ;d5 = d1; d3 = d1.operator+(d2); OK! NOT OK! NOT OK! OK! OK!NOT OK!

4/14/2015 Assoc. Prof. Stoyan Bonev 11 Friend function overloading operator + Comments: The additions d3 = d1 + d2; d3 = d1.operator+(d2); will compile. OK! The left context of the + operator is object of Distance class and the compiler is clever enough to “guess” to activate the overloaded + operator

4/14/2015Assoc. Prof. Stoyan Bonev12 Friend function overloading operator + Comments: The additions d4 = d ; d4 = d1.operator+(10.0); will compile. OK! The left context of the + operator is object of Distance class and the compiler is clever enough to “guess” to activate the overloaded + operator. One more requirement is must: we need 1-arg constructor in order the compiler to succeed OK!. Otherwise it will compile NOT OK!

4/14/2015Assoc. Prof. Stoyan Bonev13 Friend function overloading operator + Comments: The addition d5 = d1; will always compile NOT OK! The left context of the + operator is basic data type (double) and the compiler will activate the basic arithmetic addition. It cannot “guess” to activate the overloaded + operator

4/14/2015Assoc. Prof. Stoyan Bonev14 Friend function overloading operator + How to resolve the NOT OK problem from previous slide? 1.We can get around the problem by creating a new object of class Distance. In this case addition d5 = d1; is modified to >>> d5=Distance(10, 0.) + d1; This is to illustrate nonintuitive and inelegant solution. 2.The elegant solution is based on the friend function concept.

4/14/2015Assoc. Prof. Stoyan Bonev15 Friend function overloading operator + One global function definition: friend Distance operator+(Distance, Distance); Distance operator+(Distance d1, Distance d2){ int f = d1.feet + d2.feet; float in = d1.inches + d2.inches; if (in >=12.) { f++; in-=12.;} return Distance(f,in); } Distance d1(6, 5.18), d2=3.5, d3, d4, d5; d3 = d1 + d2;d4 = d ;d5 = d1; d3 = operator+(d1, d2); OOP3e.cpp

4/14/2015Assoc. Prof. Stoyan Bonev16 Friend function overloading operator + const float MTF = ; class Distance { private: int feet; float inches; public:Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } Distance (float meters) { float fltfeet = meters * MTF; feet = int(fltfeet); inches = 12 *(fltfeet - feet); } void ShowDist() { cout <<"\nDistObject= " << feet <<" "<< inches;} friend Distance operator+(Distance, Distance); };

4/14/2015Assoc. Prof. Stoyan Bonev17 Friend function overloading operator + Distance operator+( Distance d1, Distance d2) { // first version of source text, introduced before two slides // int ft; float in; // ft = d1.feet + d2.feet; // in = d1.inches + d2.inches; // if (in >= 12.) { in -= 12.; ft++; } // return Distance(ft, in); // second version of source text Distance temp; temp.feet = d1.feet + d2.feet; temp.inches = d1.inches + d2.inches; if (temp.inches >= 12.) { temp.inches -= 12.; temp.feet++; } return temp; }// end of friend function operator+(Distance d1, Distance d2)

4/14/2015Assoc. Prof. Stoyan Bonev18 Friend function overloading operator + void main () { Distance d1(5, 6.8), d2(3, 4.5), d5, d6, d7; d1.ShowDist(); cout << " Distance d1 "; d2.ShowDist(); cout << " Distance d2 "; d5 = d1 + d2; d5.ShowDist(); cout << " Distance d5 = d1 + d2 "; d6 = d ; d6.ShowDist(); cout << " Distance d6 = d "; d7 = d1; d7.ShowDist(); cout << " Distance d7 = d1 "; }

4/14/2015Assoc. Prof. Stoyan Bonev19 Friend function overloading operator + One more global function definition : friend Distance operator+(Distance, Distance); Distance operator+(Distance d1, Distance d2){ int f = d1.feet + d2.feet; float in = d1.inches + d2.inches; if (in >=12.) { f++; in-=12.;} return Distance(f,in); } Distance d1(6, 5.18), d2=3.5, d3, d4, d5; d3 = d1 + d2;d4 = d ;d5 = d1; d3 = operator+(d1, d2); OOP3e.cpp

4/14/2015Assoc. Prof. Stoyan Bonev20 Friend functions, application Friends as Bridge among classes

4/14/2015Assoc. Prof. Stoyan Bonev21 Bridge among classes How to implement a function that manipulates objects of two different classes: as a friend for both classes. class beta; class alpha { int data;... friend int fun(alpha, beta);}; class beta { int data;... friend int fun(alpha, beta); }; // friend function int fun( alpha a, beta b) { return a.data + b.data; } void main() {alpha aa; beta bb; cout << fun (aa, bb); }

4/14/2015Assoc. Prof. Stoyan Bonev22 Friend functions, application Friend functions for functional notation

4/14/2015Assoc. Prof. Stoyan Bonev23 Friends for functional notation Friends allow a more obvious syntax for calling a function than does a member function. Example: function square() – to multiply by itself an object of class Distance and to return result Distance d1(5, 7.8), d2, d3; // member function// friend function d2 = d1.square();d3 = square(d1); OOP3f.cppOOP3g.cpp

4/14/2015Assoc. Prof. Stoyan Bonev24 OOP3f.cpp – square() as member function class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } void ShowDist() {cout <<"\nDistObject= " << feet <<" "<< inches; } // Distance square() {float fltfeet = feet + inches/12.; float fltsqrd = fltfeet * fltfeet; int ft = int(fltsqrd);float in = 12. * (fltsqrd - ft); return Distance(ft, in); } // end of function square( ) }; void main() { Distance d1(5, 6.8), d2(3, 4.5), d6, d7; d1.ShowDist(); cout << " Distance d1 "; d2.ShowDist(); cout << " Distance d2 "; d6 = d1.square(); d6.ShowDist(); cout << "\t Distance d6 = d1.square() "; d7 = d2.square(); d7.ShowDist(); cout << "\t\t Distance d7 = d2.square() "; }

4/14/2015Assoc. Prof. Stoyan Bonev25 Friends for functional notation Distance d1(5, 7.8), d2, d3; d2 = d1.square();d3 = square(d1); OOP3f.cppOOP3g.cpp

4/14/2015Assoc. Prof. Stoyan Bonev26 OOP3g.cpp – square() as friend function class Distance { private: int feet; float inches; public: Distance() { feet = 0; inches = 0.0; } Distance (int ft, float in) { feet = ft; inches = in; } void ShowDist() {cout <<"\nDistObject= " << feet <<" "<< inches; } friend Distance square(Distance); }; Distance square(Distance d) {float fltfeet = d.feet + d.inches/12.; float fltsqrd = fltfeet * fltfeet; int ft = int(fltsqrd);float in = 12. * (fltsqrd - ft); return Distance(ft, in); } // end of friend function square(Distance d) void main () { Distance d1(5, 6.8), d2(3, 4.5), d6, d7; d1.ShowDist(); cout << " Distance d1 "; d2.ShowDist(); cout << " Distance d2 "; d6 = square(d1); d6.ShowDist(); cout << "\t Distance d6 = square(d1) "; d7 = square(d2); d7.ShowDist(); cout << "\t Distance d7 = square(d2) "; }

4/14/2015Assoc. Prof. Stoyan Bonev27 Friends for functional notation Distance d1(5, 7.8), d2, d3; d2 = d1.square();d3 = square(d1); OOP3f.cppOOP3g.cpp

4/14/2015Assoc. Prof. Stoyan Bonev28 Friend classes The member functions of a class can all be made friend at the same time when you make the entire class a friend. class alpha { private: int data1; public: alpha() { data1 = 99; } friend class beta; }; class beta { public: void f1(alpha a){ cout << a.data1; } }; void main() { alpha a; beta b; b.f1(a); }

29 More on OOP Friend Functions Friend Functions in VBasic Friend Functions in C# Friend Functions in Java

30 VBasic: file oopFriend.vb Friend qualifier specifies that one or more declared programming elements are accessible only from within the assembly that contains their declaration. In many cases, you want programming elements such as classes and structures to be used by the entire assembly, not only by the component that declares them. However, you might not want them to be accessible by code outside the assembly (for example, if the application is proprietary). If you want to limit access to an element in this way, you can declare it by using the Friend modifier. Friend access is often the preferred level for an application's programming elements, and Friend is the default access level of an interface, a module, a class, or a structure.

31 C#, file oopFriend.cs C# does not support explicitly reserved word Friend. There is no friend function in c# but you can use the `internal` access modifier which will make this class accessible to the other classes in the same assembly, but not accessible outside the assembly. Alternatively you can also use `protected internal` where you restrict access to the current assembly or types derived from the containing class.

32 Java, file oopFriend.java Java does not support explicitly reserved word Friend. The closest thing Java has to C++ friends is the default access modifier, also known as package-protected or package- private. This allows access to members only from other classes within the same package. This is also the best reason to place classes in the same package, rather than grouping into subpackages based on functionality.

ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts

Copyright © 2009 Addison-Wesley. All rights reserved.1-34Copyright © 2009 Addison-Wesley. All rights reserved.1-34 Language Examples: C++ (continued) Friend functions or classes - to provide access to private members to some unrelated units or functions –Necessary in C++

Thank You For Your Attention