Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 8 Operator Overloading, Friends, and References.
Operator overloading redefine the operations of operators
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
EC-241 Object-Oriented Programming
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.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class.
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.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
CS 117 Spring 2002 More Classes. IEEE Student Conference Student Professional Awareness Conference –Paul Kostek: career options for 21st Century –Panel.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Coyright © 2008 Pearson Addison-Wesley. All rights reserved. Friends, Overloaded Operators, and Arrays in Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Friends, Overloaded Operators, and Arrays in Classes.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
Chapter 8 Operator Overloading, Friends, and References.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
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 Chapter 8 Operator Overloading, Friends, and References.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
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?
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
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.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
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.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
1 C++ Classes and Data Structures Course link…..
Learning Objectives Pointers as dada members
Programming with ANSI C ++
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?
Operator Overloading CMSC 202.
CISC181 Introduction to Computer Science Dr
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?
Const in Classes CSCE 121 J. Michael Moore.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Friend Functions.
Introduction to Programming
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
Function Overloading.
Operator Overloading I
Review of Function Overloading
const A& B::blah (const C& c) const {...} Passed in a reference to a constant object ‘c’  ‘c’ cannot be modified in the function const A& B::blah.
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Presentation transcript:

Friend

Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload ‘==‘: Money m1(5,25),m2(5,25); … If (m1==m2) cout << “m1 and m2 are the same amount of money\n”; …

Overloading as non-Member Functions Equality operator, == –Not defined as member function –Cannot access member variables by name –Overhead: two invocations of getDollars() and two invocations of getCents() –Hard to read bool operator==(const Money& m1, const Money& m2) { return ((m1.getDollars()==m2.getDollars()) && m1.getCents()==m2.getCents()); };

Friend Functions Friends can directly access private class data –No overhead, more efficient Operator must have access anyway –So: best to make nonmember operator overloads friends! Friends can be any function

Friend Function Declarations class Money { public: Money(int D, int C); int getDollars(); int getCents(); friend bool operator ==(const Money& m1, const Money& m2); friend Money operator +(const Money& m1, const Money& m2); private: int dollars; int cents; };

Friend Function Definitions bool operator ==(const Money& m1, const Money& m2) { return ((m1.dollars==m2.dollars) && m1.cents==m2.cents); };

Friend Class Entire classes can be friends –Similar to function being friend to class –class F is friend of class C All class F member functions are friends of C NOT reciprocated class F; class C { public: … friend class F; … }; class F { … };