Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Operator Overloading Fundamentals
Interfaces A Java interface is a collection
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
C++ Features and Constructs Ch. 3 except 3.2, 3.4, 3.9, 3.11.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
C++ data types. Structs vs. Classes C++ Classes.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
CSE 250: Data Structures Week 3 January 28 – February 1, 2008.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Pointer to an Object Can define a pointer to an object:
Review What is an object? What is a class?
Object Oriented Programming COP3330 / CGS5409
Templates.
Andy Wang Object Oriented Programming in C++ COP 3330
Const in Classes CSCE 121 J. Michael Moore.
Operator Overloading
User Defined Functions
6.12 Default Arguments.
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 4 Writing Classes.
Reference Parameters.
Parameters and Overloading
Friends, Overloaded Operators, and Arrays in Classes
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Java Programming Language
Function Templates Class Templates
Class rational part2.
C++ data types.
Corresponds with Chapter 5
Functions Chapter No. 5.
Presentation transcript:

Overview Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator

Object parameters l Objects can be passed by: l Value l Reference l Constant Reference

Fraction Class // class declaration class Fraction { private: int num, den; public: Fraction(int=0, int=1); void add(Fraction, Fraction); void reduce(); float decimal(); void reveal(int &, int &); };

Object parameters There are three ways to declare the add() member function of the Fraction class: version 1: void add(Fraction, Fraction); version 2: void add(Fraction &, Fraction &); version 3: void add(const Fraction &, const Fraction &);

Object parameters Here is part of the main(): Fraction x(1,2); Fraction y(3,4); Fraction z; z.add(x,y); Function invocation same regardless of its declaration

num 1 den 2 num 3 den 4 num 0 den 1 x z y num 1 den 2 num 3 den 4 B A main() add() Object parameters: Diagram of version 1 when z.add(x,y) executes

Parameters passed by value A copy of the actual parameters x and y are sent to formal parameters A and B. This is OK for small object but very inefficient for larger objects Object parameters: Explanation of version 1 when z.add(x,y) executes

num 1 den 2 num 3 den 4 num 0 den 1 x z y B A main() add() Object parameters: Diagram of version 2 when z.add(x,y) executes

Parameters passed by reference More efficient Dangerous: function can inadvertently (or maliciously) change an actual parameter (in the main) by changing the formal parameter. It is not the case here. Object parameters: Explanation of version 2 when z.add(x,y) executes

num 1 den 2 num 3 den 4 num 0 den 1 x z y B A main() add() Object parameters: Diagram of version 3 when z.add(x,y) executes

Object parameters: Explanation of version 3 when z.add(x,y) executes Parameters passed by constant reference Both of the best world: security and efficiency No Danger: compiler will not allow to modify the actual parameter, even the formal parameters. If the function tries to do this: A.num = 1000; //Will not be allowed by the compiler int i; i = A.num ; //Will be allowed by the compiler

A friend function is not a member function of a class but is allowed to touch anything protected or private It is not a category such as Private or Public Friend Functions

Friend or Foe?? Let's look at Fraction again. Consider the Fraction::add() function. It's a __________ function. Can it be restructured as a friend function?

Friend Functions To convert Fraction::add() function from a member function to a friend function: 1. put it onto the friends list 2. change its return type 3. remove its class scope operator

declaration: void add(Fraction, Fraction); definition: void Fraction::add(Fraction A, Fraction B) { num = A.num*B.den + B.num*A.den; den = A.den * B.den; } add() – Member Version

invocation: Fraction x(1,2), y(3,4), z; z.add(x,y);//now z holds the value 5/4 add() – Member Version

declaration: friend Fraction add(Fraction, Fraction); definition: Fraction add(Fraction A, Fraction B) { Fraction C; C.num = A.num*B.den +B.num*A.den; C.den = A.den * B.den; return C; } add() – Friend Version

invocation: Fraction x(1,2), y(3,4), z; z = add(x,y); add() – Friend Version

Member function example: z.reduce(); Friend function example: z = reduce(z); How would you make reduce() a friend function??? Friend Functions

Review: Function overloading What does it mean to "overload" a function? same function name with different argument types functions must differ by more than return type!

An example: a findLowest function char findLowest(char, char, char); int findLowest(int, int, int); float findLowest(float, float, float); double findLowest(double, double, double); long findLowest(long, long, long); BankAccount findLowest(BankAccount,BankAccount,BankAccount); Another example: constructor functions: Triangle(); Triangle(int, int, int); Friend Functions

It's a very short jump from the friend add function to an overloaded operator: Fraction add(Fraction A, Fraction B) { fraction C; C.num = A.num + B.num; C.den = B.den * A.den; C.reduce(); return C; } Friend Functions

Fraction operator+ (fraction A, fraction B) { Fraction C; C.num = A.num + B.num; C.den = B.den * A.den; C.reduce(); return C; } Friend Functions

Now in main instead of z = add(x, y) we can say: z = x + y; How would the declaration of the fraction class need to change? In fraction.h: friend Fraction add(Fraction, Fraction); friend Fraction operator+(Fraction, Fraction); Friend Functions

We can also overload C++ operators. We can make our own = operator: in.h file void operator = (Fraction); in.cpp file void Fraction::operator= (Fraction rightSide) { num = rightSide.num; den = rightSide.den; } Operator Overloading

We can make our own = operator: in main Fraction x(1,2); Fraction z; z.operator=(x); //or just z = x; Operator Overloading

#include #include "fraction_class.h" void main() { int i, j; Fraction x(1,2); x.reveal(i,j); cout<<"Fraction x is "<<i<<"/"<<j<<endl; Fraction z; z.operator=(x); z.reveal(i,j); cout<<"Fraction z is "<<i<<"/"<<j<<endl; z = x; z.reveal(i,j); cout<<"Fraction z is "<<i<<"/"<<j<<endl; } Fraction x is 1/2 Fraction z is 1/2

What happens when Fraction::operator=() is called? Operator Overloading

in.h file void operator = (Fraction &); in.cpp file void Fraction::operator= (Fraction & rightSide) { num = rightSide.num; den = rightSide.den; } What is the danger now?? Operator Overloading

in.h file void operator = (const Fraction&); in.cpp file void Fraction::operator= (const Fraction & rightSide) { num = rightSide.num; den = rightSide.den; } This is an example of "overloading" an operator. Operator Overloading

How would we overload the + operator for fractions?? Two ways: 1) use analogy to the operator= example 2) use a friend function Operator Overloading