1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.

Slides:



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

Operator overloading redefine the operations of operators
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
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.
Classes Separating interface from implementation
CMSC 2021 Stream I/O Operators and Friend Functions.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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+
Object-Oriented Programming in C++
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
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.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Operator Overloading.
CS410 – Software Engineering Lecture #12: C++ Basics V
Operator Overloading Introduction
Function Overloading Can enables several function Of same name
Programming with ANSI C ++
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Review What is an object? What is a class?
Templates.
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.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
Operator Overloading, Friends, and References
Operator Overloading.
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++
Templates I CMSC 202.
Operator Overloading I
Template.
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.
Overloading the << operator
CS410 – Software Engineering Lecture #6: Advanced C++ I
Presentation transcript:

1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar functions that can be applied to objects of different types. Example: void swap(int& x, int& y) { int temp = x; x = y; y = temp; } void swap(char& x, char& y) { char temp = x; x = y; y = temp; } int main () { int nums[5] = {5,2,3,4,1}; char letters[3] = {‘t’, ‘a’, ‘r’}; swap(nums[0], nums[4]); swap(letters[0], letters[3]); return 0; } compiler calls this swap() compiler calls this swap()

2 Overloading functions Operators such as =, ==, +, etc. are often overloaded for various user-defined types. We have already seen examples of an overloaded operator= and operator== Can we overload << for objects of our MemberInfo class so that we can do: and have the name and age printed on the screen? The answer is yes, but... MemberInfo president(“J. Smith”, 50); cout << president << endl;

3 Overloading functions In cout << president; the left operand of << is cout and NOT a MemberInfo object. Therefore, the operator<< may not be a member function of MemberInfo However, operator<< must be able to access the private members of MemberInfo. Both of these requirements can be satisfied, if the overloaded operator<< becomes a friend of MemberInfo.

4 friend functions In many cases, we would like a function to have access to private data members of the class, without the function being a member of the class. Examples: the << problem we just discussed a function that needs to operate on two or more objects of the same class e.g. a function that takes as arguments two Points and computes the distance between them a function that needs to operate on objects of different classes. e.g. a function HaveCollided that takes as arguments a Ship and a Torpedo object.

5 friend functions A class may allow a function to access its private data members by declaring as a friend function. Example: class Torpedo; class Ship { friend bool HaveCollided(Torpedo&, Ship& ); private: Shiptype type; char *name; Coords position; public:... };... bool HaveCollided(Torpedo& t, Ship& s) {... }

6 friend classes We may also declare a class A to be a friend of class B. This will give A access to the private members of B. IMPORTANT: This does not mean that B has access to the private data members of A class CityNetwork { private: City *citylist; Road *highways; public:... }; class City { friend class CityNetwork; private: Coords latitude; Coords longitude; public:... }; Now, CityNetwork can access latitude and longitude

7 evil friends! Friendship may only be granted, not taken. Friend functions and classes violate the principles on encapsulation and information hiding. They should be avoided whenever possible. You must always have very good reasons for using friends. Keep in mind that a friend function is dependent on the implementation of the class that declared it as a friend. If the implementation changes, the function may need to be modified and will certainly need to be recompiled.

8 back to overloading << As discussed earlier, we have strong justification for making the overloaded operator<< a friend of our MemberInfo class. But what does its implementation look like? ostream& operator<< (ostream& output, const MemberInfo& member) { output << “Name = “ << member.name << endl << “Age = “ << member. age << endl; return output; }

9 Overloading gone wild Will the following code compile? int add(int a, int b) { return a+b; } double add(double a, double b) { return a+b; } int main () { cout << add(3.14, 99) << endl; return 0; }

10 Overloading gone really wild Will this code compile? class A { private: int a; public: A() {a=1;} int incr() { a ++; return a; } friend int incr(int x); }; int incr(int x) { x++; return x; } double incr(double x) { x++; return x; }