Friend Functions.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

For(int i = 1; i
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
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.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
Operator Overloading Fundamentals
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
Negation Operator. Code Trace // main.cpp... g = -f;... // fraction.h... class Fraction { friend Fraction operator-(const.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Learners Support Publications Classes and Objects.
Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 8 Operator Overloading, Friends, and References.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming Elhanan Borenstein Lecture #5.
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.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
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.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Static Members.
Regarding assignment 1 Style standards Program organization
Learning Objectives Pointers as dada members
Friend functions.
Access Functions and Friend Functions
Object Oriented Programming COP3330 / CGS5409
Object-Oriented Programming (OOP) Lecture No. 17
Class: Special Topics Copy Constructors Static members Friends this
Templates.
CISC/CMPE320 - Prof. McLeod
More on Classes Programming in C++ Fall 2008
מערכת הרבייה באדם ערכו וכתבו: אורית עמיאל-שרון והעיקר האהבה
Overloading the << operator
Accessor and Mutator Functions
The Run-Time Stack and Reference Parameters
Introduction to Programming
Static in Classes CSCE 121 J. Michael Moore.
Operator Overloading, Friends, and References
Bracket Operator.
Default Arguments.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Lesson Subject Book Week 4 lesson 1 Class, copy-constructor
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Object Oriented Programming (OOP) Lecture No. 11
Defining Class Functions
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Overloading the << operator
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Class Circle { Float xc, yc, radius;
Presentation transcript:

Friend Functions

Recall Private Access Woes //fraction.h ... class Fraction { private: int m_Numerator; int m_Denominator; }; //fraction.cpp ... Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator;

Recall Private Access Woes //fraction.h ... class Fraction { private: int m_Numerator; int m_Denominator; }; //fraction.cpp ... Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator;

Friend Functions A friend function is a nonmember function to a class that has been given direct access rights to the private section of the class. The keyword friend is always used inside the class definition of the class granting the access rights. Never use the word friend outside of a class definition.

Friends! //fraction.h ... class Fraction { friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); private: int m_Numerator; int m_Denominator; }; //fraction.cpp ... Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator;

Friends! //fraction.h ... class Fraction { friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); private: int m_Numerator; int m_Denominator; }; //fraction.cpp ... Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator;

Full Function //fraction.h ... class Fraction { friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); }; //fraction.cpp ... Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; temp.m_Denominator = lhs.m_Denominator * rhs.m_Denominator; return temp; }

Don’t Do This //fraction.h ... class Fraction { friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs); }; //fraction.cpp ... friend Fraction mult_fracs(const Fraction & lhs, const Fraction & rhs) { Fraction temp; temp.m_Numerator = lhs.m_Numerator * rhs.m_Numerator; temp.m_Denominator = lhs.m_Denominator * rhs.m_Denominator; return temp; }

End of Session