Operator Overloading Chapter 11 1. Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.

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.
1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Computer Science 1620 Function Scope & Global Variables.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Binary Search Trees II Morse Code.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
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 – .
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading CS 3370 – C++ Chapter 14.
Overloading C++ supports the concept of overloading Two main types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
C++ Templates.
Overloading Operator MySting Example
A First C++ Class – a Circle
Overloading the << operator
Overview of C++ Overloading
Abstraction: Generic Programming, pt. 2
Friends, Overloaded Operators, and Arrays in Classes
Classes.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming February 21 Class Meeting
Overloading the << operator
Presentation transcript:

Operator Overloading Chapter 11 1

Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use friend f unctions. 2

Operator Overloading Recall function overloading We can write multiple functions with the same name providing they have different signatures. Operators are just an alternative notation for function calls. Each operator has an equivalent method. We can define overloaded versions in our own classes. 3

Why bother with this? Permits more user-friendly versions of methods. Use + to add two objects Where the concept of addition make sense. Consider + for string contatenation in C++ vs. strcat() in C. 4

5 > Operator for Circles If c1 and c2 are Circle objects, c1 > c2 compiles as c1.operator>(c2)

Add to Circle.h bool operator>(const Circle& other) const; 6

7 Add to Circle.cpp bool Circle::operator>(const Circle& other) const { return this->radius > other.radius; }

8 Circles_Test.cpp if (*c1 > *c2) { cout Name() << " is greater than " Name() << endl; } else if (*c2 > *c1) { cout Name() << " is greater than " Name() << endl; } else { cout Name() Name() << " are the same size\n"; }

9 Works the Same

10 Overloading the Insertion Operator Let's look at overloading a different operator  The "insertion" operator, << cout << myString;  says to insert myString into the console output stream.

11 Overloading the Insertion Operator << works with all native C++ types. Overloaded definitions for all native types are included in What about our own classes? Would like to be able to write cout << my_circle << endl; If want it to work as expected, we have to provide a new overload of the << operator for that class: void operator<<(ostream& os, const Circle& c); Cannot be a member function. Why?

Friend Methods A class can declare a non-member function as a friend. Function has the same access to class members as a member method. The function is normally defined in the same cpp file as the member functions. Effectively part of the interface published by the class. Read about this in Chapter

Circle.h #pragma once #include using namespace std; class Circle { private: double radius; string name; public: Circle(string Name, double Radius); … bool operator>(const Circle& other) const; friend void operator<<(ostream& os, const Circle& c); }; 13

Add to Circle.cpp void operator<<(ostream& os, const Circle& c) { os << c.name << " Radius " << c.radius; } Note: NOT a member of class Circle. No Circle:: 14

In Circle_Test.cpp 15 int main() { Circle* c1 = Create_Circle(); double c1_area = c1->Area(); cout Name() << " is " << c1_area << endl; cout << endl; cout << *c1; cout << endl; Circle* c2 = Create_Circle(); double c2_area = c2->Area(); cout Name() << " is " << c2_area << endl; cout << *c2; cout << endl;

Program Running 16

But there is a flaw Suppose we want to put more output after c1 int main() { Circle* c1 = Create_Circle(); double c1_area = c1->Area(); cout Name() << " is " << c1_area << endl; cout << endl; cout << *c1 << endl; This doesn’t work. Why? 17

Compile Time Errors 18

The error line 19 The << operator requires an ostream object on the left side. Our operator << is a void method.

20 Correction in Circle.h friend ostream& operator<<(ostream& os, const Circle& c);

21 Correction in Circle.cpp ostream& operator<<(ostream& os, const Circle& c) { os << c.name << " Radius " << c.radius; return os; }

Now compiles and works 22

An Alternative It didn't have to be a friend. If the << operator used accessor functions it would not need to be a friend. Move declaration outside class definition and remove “friend”. 23

Circle.h class Circle {... double Radius() const {return radius;}; const char* Name() const {return name;};... }; ostream& operator<<(ostream& os, const Circle& c); 24

Circle.cpp Now uses accessor methods rather than directly accessing member variables of class Circle. ostream& operator<<(ostream& os, const Circle& c) { os << c.Name() << " Radius " << c.Radius(); return os; } 25

Works the Same 26

Summary Overloaded insertion operator, operator<<, should be defined with the class, but cannot be a member. Could be a friend. Could use accessor methods. 27