+ All Categories
Home > Documents > 4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend...

4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend...

Date post: 14-Dec-2015
Category:
Upload: jakayla-burlock
View: 215 times
Download: 2 times
Share this document with a friend
Popular Tags:
35
03/21/22 Assoc. Prof. Stoyan Bonev 1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Transcript

04/18/23 Assoc. Prof. Stoyan Bonev 1

COS220 Concepts of PLs AUBG, COS dept

Lecture 23m2

OOPFriend Functions

Ref: Sebesta, Chapter 12; Lafore, Chapter 11

04/18/23 Assoc. Prof. Stoyan Bonev 2

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

04/18/23 Assoc. Prof. Stoyan Bonev 5

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

04/18/23 Assoc. Prof. Stoyan Bonev 6

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.

04/18/23 Assoc. Prof. Stoyan Bonev 7

Friend functions, application

• Normal overloaded operator + and

friend overloaded operator +

• Friends as Bridge among classes

• Friend functions for functional notation

04/18/23 Assoc. Prof. Stoyan Bonev 8

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.

04/18/23 Assoc. Prof. Stoyan Bonev 9

Friend functions, application

Normal overloaded operator + and

friend overloaded operator +

04/18/23 Assoc. Prof. Stoyan Bonev 10

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!

04/18/23 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

04/18/23 Assoc. Prof. Stoyan Bonev 12

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!

04/18/23 Assoc. Prof. Stoyan Bonev 13

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

04/18/23 Assoc. Prof. Stoyan Bonev 14

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.

04/18/23 Assoc. Prof. Stoyan Bonev 15

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

04/18/23 Assoc. Prof. Stoyan Bonev 16

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

};

04/18/23 Assoc. Prof. Stoyan Bonev 17

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

04/18/23 Assoc. Prof. Stoyan Bonev 18

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 ";

}

04/18/23 Assoc. Prof. Stoyan Bonev 19

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

04/18/23 Assoc. Prof. Stoyan Bonev 20

Friend functions, application

Friends as Bridge among classes

04/18/23 Assoc. Prof. Stoyan Bonev 21

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

}

04/18/23 Assoc. Prof. Stoyan Bonev 22

Friend functions, application

Friend functions for

functional notation

04/18/23 Assoc. Prof. Stoyan Bonev 23

Friends for functional notationFriends 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.cpp OOP3g.cpp

04/18/23 Assoc. Prof. Stoyan Bonev 24

OOP3f.cpp – square() as member functionclass 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() "; }

04/18/23 Assoc. Prof. Stoyan Bonev 25

Friends for functional notation

Distance d1(5, 7.8), d2, d3;

d2 = d1.square(); d3 = square(d1);

OOP3f.cpp OOP3g.cpp

04/18/23 Assoc. Prof. Stoyan Bonev 26

OOP3g.cpp – square() as friend functionclass 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) ";}

04/18/23 Assoc. Prof. Stoyan Bonev 27

Friends for functional notation

Distance d1(5, 7.8), d2, d3;

d2 = d1.square(); d3 = square(d1);

OOP3f.cpp OOP3g.cpp

04/18/23 Assoc. Prof. Stoyan Bonev 28

Friend classesThe 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 0-321-49362-1

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 YouFor

Your Attention


Recommended