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.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
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.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
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.
Definition Class In C++, the class is the construct primarily used to create objects.
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.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and 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:
C++ Lecture 7 Function/operator overloading
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Case Study - Fractions Timothy Budd Oregon State University.
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+
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
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 – .
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.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
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.
Chapter 18 - C++ Operator Overloading
CS410 – Software Engineering Lecture #12: C++ Basics V
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Function Overloading Can enables several function Of same name
Chapter 14: More About Classes.
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?
Overloading Operator MySting Example
C++ Programming Functions
Review What is an object? What is a class?
Object-Oriented Design (OOD) and C++
Operator Overloading Part 2
A First C++ Class – a Circle
The dirty secrets of objects
Friends and Unary Operators
Chapter 14: More About Classes.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Overloading the << operator
Operator Overloading; String and Array Objects
Advanced Program Design with C++
Object Oriented Programming Using C++
Operator Overloading, Friends, and References
Classes.
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++
Operator Overloading I
Eighth step for Learning C++ Programming
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.
Class: Special Topics Overloading (methods) Copy Constructors
Overloading the << operator
Presentation transcript:

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 functions that can be applied to objects of different types. Example: compiler calls this print() int main () { print("Vana"); print(32); return 0; } void print(string name) { cout << "My name is " << name << endl; } void print(int age) { cout << "I am " << age << " years old" << endl; compiler calls this print()

Operator Overloading Operators such as =, ==, +, << etc. are often overloaded for various user-defined types. What does x+y mean (assuming x is an object of type T)? It means "apply operator + to object x, with input y" Another way to write this is x.operator+(y) operator+ is a member of class T. Its prototype is: T operator+ (const T&) 1. Take an argument of type T 2. Add it to the current object 3. Return the result

Overloading functions What does cout << x; mean? In cout << x; the left operand of << is cout and NOT a T object. Therefore, the operator<< cannot be a member function of class T However, operator<< will probably need to access the private data members of T. This can be done through accessors, OR We can declare operator<< as a friend of class T.

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.

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

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 City { private: Coords latitude; Coords longitude; public: ... friend class CityNetwork; }; class CityNetwork { private: City *citylist; Road *highways; public: ... }; Now, CityNetwork can access latitude and longitude

evil friends! Friendship may only be granted, not taken. Friend functions and classes violate the principles of 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.

back to overloading << As discussed earlier, we have strong justification for making the overloaded operator<< a friend of our T class. But what does its implementation look like? class T { int a; int b; ... } ostream& operator<< (ostream& output, const T& member) { output << a << " " << b << endl; return output; } this allows chaining: T obj1, obj2; cout << obj1 << obj2;

More overloading See the sample code in ~b11/labs/Overloading on T-lab. Experiment with it!