Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.

Slides:



Advertisements
Similar presentations
Operator Overloading Fundamentals
Advertisements

Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Case Study - Fractions Timothy Budd Oregon State University.
Introduction to Classes in C++
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+
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
CS Object Oriented Programming Using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Introduction to C++ Systems Programming.
Motivation and Overview
Object Oriented Programming COP3330 / CGS5409
Polymorphism in C++ Operator Overloading
Constructor & Destructor
Pointers and Pointer-Based Strings
Chapter 11: More About Classes and Object-Oriented Programming
Chapter 15: Overloading and Templates
Memberwise Assignment / Initialization
The dirty secrets of objects
Chapter 14: More About Classes.
Inheritance Using work from:
Operator Overloading; String and Array Objects
Operator Overloading.
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
Operator Overloading, Friends, and References
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Pointers and Pointer-Based Strings
Operator Overloading; String and Array Objects
Chapter 9 Introduction To Classes
Class rational part2.
Presentation transcript:

Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect the original variable’s value in the caller. pass-by-reference The caller passes the address of its data. The caller gives the called function the ability to access the caller’s data directly and to modify it if the called function chooses to do so.

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that the parameter is passed by reference. All operations performed on the alias (i.e. the reference) are actually performed on the original variable. The alias is simply another name for the original variable.

Reference Parameters "pass-by-value in C" example: #include <stdio.h> void swap(int first, int second); // prototype int main() { int x = 5; int y = 6; printf("Before: x = %d y = %d\n", x, y); swap(x, y); //were the integers swapped? printf("After: x = %d y = %d\n", x, y); return 0; } void swap(int first, int second) int temp; temp = first; first = second; second = temp; Output: Before: x = 5 y = 6 After: x = 5 y = 6 "simulating pass-by-reference in C" example: #include <stdio.h> void swap(int *first, int *second); // prototype int main() { int x = 5; int y = 6; printf("Before: x = %d y = %d\n", x, y); swap(&x, &y); //were the integers swapped? printf("After: x = %d y = %d\n", x, y); return 0; } void swap(int *first, int *second) int temp; temp = *first; *first = *second; *second = temp; Output: Before: x = 5 y = 6 After: x = 6 y = 5

Reference Parameters in C++ C++ has true reference true "pass-by-reference". Use the & operator to denote a reference parameter, e.g. void swap(int &a, int &b); or void swap(int &, int &);

Reference Parameters in C++ "pass-by-value in C++" example: #include <iostream> using namespace std; void swap(int first, int second); // prototype int main() { int x = 5; int y = 6; cout << "\nBefore: x = " << x << " y = "<< y; swap(x, y); //were the integers swapped? cout << "\nAfter: x = " << x << " y = " << y << endl; return 0; } void swap(int first, int second) { int temp; temp = first; first = second; second = temp; Output: Before: x = 5 y = 6 After: x = 5 y = 6 "pass-by-reference in C++" example: #include <iostream> using namespace std; void swap(int &first, int &second); // prototype int main() { int x = 5; int y = 6; cout << "\nBefore: x = " << x << " y = " << y; swap(x, y); //were the integers swapped? cout << "\nAfter: x = " << x << " y = " << y << endl; return 0; } void swap(int &first, int &second) { int temp; temp = first; first = second; second = temp; Output: Before: x = 5 y = 6 After: x = 6 y = 5

Reference Parameters Pass-by-reference is easier and more natural for some programmers than simulating pass-by-reference with pointers. Some programmers prefer using pointers, because * in the parameter list and the & in the call, make it explicitly clear that the called function will alter the caller’s data. With call-by-reference, the parameter list in the function call is identical to the parameter list for call-by-value.

Function Overloading A class may have several methods that have the same name. This is referred to as function overloading. When a function name is overloaded, the implementation that is actually used is the one whose formal parameters match the actual arguments being passed by the caller in both number of arguments and type of argument. Therefore, it is mandatory that each implementation has distinguishable parameters.

Operator Overloading Most operators can be overloaded. The ones that cannot are . . * :: ?: and sizeof Operator overloading is actually part of the function overloading mechanism. The name of the function for an overloaded operator is operator followed by the operator symbol, e.g., Operator Function name + operator+ - operator- * operator* <= operator<= -> operator->

Operator Overloading Operator overloading is restricted to existing operators. Thus it is not legal to try to overload ** **operator** Operators can be overloaded as instance member functions, or as friend functions

Operator Overloading The operator functions work "almost" exactly like "regular" functions. They can even be invoked using their operator+ or operator- names. Keeping this fact in mind will remove much of the mystery from how they work and how they must be implemented. The "almost" qualifier above reflects necessity to remember that almost all C operators take either one or two operands. Thus an operator function has at most two parameters.

Operator Overloading The C addition operator takes two operands: a + b Therefore the operator+ function will have two parameters: the first will represent the left side operand and the second the right side operand. The C logical not operator takes one operand: !value Therefore the operator! function will have one parameter and it will represent the right side operand.

Operator Overloading Operator overloading must preserve the "aryness" (unary or binary) nature of the operator. Thus, the ! operator could be overloaded to compute the length of a single vector but could not be used to compute a dot product. The operators &, *, +, - have both binary and unary versions that may be overloaded separately. It is not possible to change the precedence or associativity of an overloaded operator. Most operator functions return a value that replaces the operator and its operand(s) in an expression. However, that is not mandatory. For example a side effect operator such as ++ may not need to return a value.

Overloading operators as instance members Operator overloading must preserve the "aryness" (unary or binary) nature of the operator. Thus, the ! operator could be overloaded to compute the length of a single vector but could not be used to compute a dot product. The operators &, *, +, - have both binary and unary versions that may be overloaded separately. It is not possible to change the precedence or associativity of an overloaded operator. Most operator functions return a value that replaces the operator and its operand(s) in an expression. However, that is not mandatory. For example a side effect operator such as ++ may not need to return a value.

Overloading Operators as Instance Members A binary operator that is overloaded as an instance member needs only one parameter, which represents the operand on the right: class OpClass { private: int x; public: OpClass operator+(OpClass right); };

Overloading Operators as Instance Members The left operand of the overloaded binary operator is the calling object The implicit left parameter is accessed through the this pointer OpClass OpClass::operator+(OpClass r) { OpClass sum; sum.x = this->x + r.x; return sum; }

Invoking an Overloaded Operator Operator can be invoked as a member function: OpClass a, b, s; s = a.operator+(b); It can also be invoked in the more conventional manner: s = a + b;

Overloading Assignment Overloading the assignment operator solves problems with object assignment when an object contains pointer to dynamic memory. Assignment operator is most naturally overloaded as an instance member function It needs to return a value of the assigned object to allow cascaded assignments such as a = b = c; See pr11-09.cpp, overload.h, and overload.cpp

Overloading Assignment Assignment overloaded as a member function: class CpClass { int *p; public: CpClass(int v=0) { p = new int; *p = v; ~CpClass(){delete p;} CpClass operator=(CpClass); };

Overloading Assignment Implementation returns a value: CpClass CpClass::operator=(CpClass r) { *p = *r.p; return *this; }; Invoking the assignment operator: CpClass a, x(45); a.operator=(x); // either of these a = x; // lines can be used

Overloading << and >> The overloaded operators << and >> can be used to print and to read numeric and character string values to stdout and stderr and from stdin, respectively. True to form, they can be further overloaded to print and read a complete object, such as a Rational object.

Overloading << and >> Given the following class declaration for class Rational: class Rational { public: Rational ( ); Rational (int wholeNumber); Rational (int theNumerator, int theDenominator); void assign(int newNumerator, int newDenominator); int getNumerator ( ) const; int getDenominator ( ) const; Rational add(Rational &rational); Rational subtract(Rational &rational); Rational multiply(Rational &rational); Rational divide(Rational &rational); private: int numerator, denominator; int gcd (int a, int b); Rational reduce (Rational rational); };

Operator Overloading We can overload the following operators to add, subtract, multiply, and divide two Rational objects: Rational operator+ (const Rational &rational) ; Rational operator- (const Rational &rational) ; Rational operator* (const Rational &rational) ; Rational operator/ (const Rational &rational) ;

Overloading + Rational Rational::operator+ (const Rational &rational) { Rational sum; sum.numerator = numerator * rational.denominator + rational.numerator * denominator; sum.denominator = denominator * rational.denominator; return reduce (sum); }

Overloading << and >> Given: Rational r1; We want to be able to do something like cout << r1; or cin >> r1; where r1 is a Rational object. Since the left hand side of >> or << is not a Rational, we must use the friend function form. So in rational.h we include: friend ostream &operator<<(ostream &out, const Rational &r);

Overloading << and >> We provide the implementation in either rational.cpp or inline in the class definition. ostream &operator<<(ostream &out, const Rational &r) { out << r.numerator << "/" << r.denominator << endl; return(out); } Note that our new overload just uses the built in overload of << to output each component of the Rational object along with '/' and a newline. This is the key to cascading application of the << operator

Overloading << and >> The implementation of >> is similar istream &operator>>(istream &in, const Rational &r) { in >> r.numerator >> r.denominator; return in; }

Notes on Overloaded Operators Overloading can change the entire meaning of an operator Most operators can be overloaded Cannot change the number of operands of the operator Cannot overload the following operators: . .* ?: sizeof See Length.h, Length.cpp, pr11-10.cpp

Overloading Types of Operators ++, -- operators overloaded differently for prefix vs. postfix notation Overloaded relational operators should return a bool value Overloaded stream operators >>, << must return istream, ostream objects and take istream, ostream objects as parameters See Length1.h, Length1.cpp, pr11-11.cpp

Overloaded [] Operator Can be used to create classes that behave like arrays, providing bounds-checking on subscripts Overloaded [] returns a reference to object, not an object itself See pr11-12.cpp, intarray.h, intarray.cpp, and pr11-13.cpp

Type Conversion Operators Conversion Operators are member functions that tell the compiler how to convert an object of the class type to a value of another type The conversion information provided by the conversion operators is automatically used by the compiler in assignments, initializations, and parameter passing

Syntax of Conversion Operators Conversion operator must be a member function of the class you are converting from The name of the operator is the name of the type you are converting to The operator does not specify a return type

Conversion Operator Example To convert from a class IntVal to an integer: class IntVal { int x; public: IntVal(int a = 0) x = a; } operator int() return x; }; See Length2.h, Length2.cpp, and pr11-15.cpp

Conversion Operator Example Automatic conversion during assignment: IntVal obj(15); int i; i = obj; cout << i; // prints 15 See Length2.h, Length2.cpp, and pr11-15.cpp

Convert Constructors Convert constructors are constructors that take a single parameter of a type other than the class in which they are defined class CCClass { int x; public: CCClass() //default CCClass(int a, int b); CCClass(int a); //convert CCClass(string s); //convert };

Example of a Convert Constructor The C++ string class has a convert constructor that converts from C-strings: class string { public: string(char *); //convert … };

Uses of Convert Constructors They are automatically invoked by the compiler to create an object from the value passed as parameter: string s("hello"); //convert C-string CCClass obj(24); //convert int The compiler allows convert constructors to be invoked with assignment-like notation: string s = "hello"; //convert C-string CCClass obj = 24; //convert int See Convert.h, Convert.cpp, and pr11-16.cpp

Uses of Convert Constructors Convert constructors allow functions that take the class type as parameter to take parameters of other types: void myFun(string s); // needs string // object myFun("hello"); // accepts C-string void myFun(CCClass c); myFun(34); // accepts int