Friend Functions 04/12/10. Today  Use reference parameters when passing an object.  Use friend functions with a class.

Slides:



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

Operator overloading redefine the operations of operators
Copyright © 2003 Pearson Education, Inc. Slide 1.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Chapter 14: Overloading and Templates
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
CS 117 Spring 2002 More Classes. IEEE Student Conference Student Professional Awareness Conference –Paul Kostek: career options for 21st Century –Panel.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
C++ data types. Structs vs. Classes C++ Classes.
Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Chapter 13: Overloading.
CSE 250: Data Structures Week 3 January 28 – February 1, 2008.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Chapter 8 Operator Overloading, Friends, and References.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Learning Objectives Pointers as dada members
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 Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
A First C++ Class – a Circle
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?
Chapter 15: Overloading and Templates
Operator Overloading
Overloading the << operator
Andy Wang Object Oriented Programming in C++ COP 3330
More on Classes and Objects
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
COP 3330 Object-oriented Programming in C++
CIS 199 Final Review.
Class.
Andy Wang Object Oriented Programming in C++ COP 3330
Class rational part2.
Overloading the << operator
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Constructors & Destructors
SPL – PS3 C++ Classes.
Presentation transcript:

Friend Functions 04/12/10

Today  Use reference parameters when passing an object.  Use friend functions with a class.

Pass Objects By-reference  More efficient  Opens possibility of changing Object  const in front to protect  Sec6.4/point/point1.cpp Equal function compares two points Equal function compares two points

Friend Functions

Use of Accessors Awkward  An equal function is in the point program. sec6.4/point1.cpp sec6.4/point1.cpp  Used accessors, but awkward  Instead use friend functions. Sec6.4/point1a.cpp Sec6.4/point1a.cpp

Friend function  Allows access to private member variables  Doesn't have to use accessors  Placed in class definition  "friend" in front of definition  Is not a member function Definition does not need class name Definition does not need class name

When to Use friend Function  Function involving two objects  Objects equally important  Not appropriate to make it a member function of either object

Midpoint Example  Want to add a function to the point class that finds the midpoint of two points.  Why should it be a friend function.  Example of a function that returns an object.  point1b.cpp

Another example  Sec6.4/timer/TIMER Make a copy of timer.cpp to work on. Make a copy of timer.cpp to work on.

Overloaded Operators

Operator Overload  Would like to be able to use == to compare points.  Instead of a friend function  example sec6.4/point/point2.cpp

Overloaded Operators  Syntax returntype operator op (parameters);

Overloaded Operators //friend function friend bool equal(Point C, Point D); //operator overload friend bool operator==(Point C, Point D);

Overload of *  Multiplication of fractions  sec6.4/fracOps.cpp Finish the friend function Finish the friend function Change the friend function to operator overload Change the friend function to operator overload

On Your Own  Write an overload of == to compare the volume of two Cylinders like those on p. 227 #3

Inline Function  Can give the function definition within the class definition.  Point class on next slide has in-line functions. One of them is the default constructor.