Download presentation

Presentation is loading. Please wait.

Published byJakayla Burlock Modified over 2 years ago

1
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

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

3
Part 1 Operator Overloading (separate lecture 23m1)

4
Part 2 Friend Functions

5
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

6
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.

7
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

8
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.

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

10
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 = d1 + 10.0;d5 = 10.0 + d1; d3 = d1.operator+(d2); OK! NOT OK! NOT OK! OK! OK!NOT OK!

11
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

12
4/14/2015Assoc. Prof. Stoyan Bonev12 Friend function overloading operator + Comments: The additions d4 = d1 + 10.0; 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!

13
4/14/2015Assoc. Prof. Stoyan Bonev13 Friend function overloading operator + Comments: The addition d5 = 10.0 + 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

14
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 = 10.0 + 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.

15
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 = d1 + 10.0;d5 = 10.0 + d1; d3 = operator+(d1, d2); OOP3e.cpp

16
4/14/2015Assoc. Prof. Stoyan Bonev16 Friend function overloading operator + const float MTF = 3.2808; 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); };

17
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)

18
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 = d1 + 10.0; d6.ShowDist(); cout << " Distance d6 = d1 + 10.0 "; d7 = 10.0 + d1; d7.ShowDist(); cout << " Distance d7 = 10.0 + d1 "; }

19
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 = d1 + 10.0;d5 = 10.0 + d1; d3 = operator+(d1, d2); OOP3e.cpp

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

21
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); }

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

23
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

24
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() "; }

25
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

26
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) "; }

27
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

28
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
29 More on OOP Friend Functions Friend Functions in VBasic Friend Functions in C# Friend Functions in Java

30
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
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
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.

33
ISBN 0- 321-49362-1 Chapter 11 Abstract Data Types and Encapsulation Concepts

34
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++

35
Thank You For Your Attention

Similar presentations

OK

Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++

Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on non agricultural activities Ppt on zener diodes Ppt on zener diode voltage Ppt on cleanliness of surroundings download Ppt on resume writing Download ppt on indus valley civilization Ppt on high power transmission Ppt on stock exchange market Ppt on textile industry in mumbai Sample ppt on importance of english