Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

Similar documents
PIC 10A. Lecture 17: Classes III, overloading

And Even More and More C++ Fundamentals of Computer Science

More Advanced Class Concepts

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Object-Oriented Programming, Iouliia Skliarova

Friend Functions and Friend Classes

IS0020 Program Design and Software Tools Midterm, Fall, 2004

OBJECT ORIENTED PROGRAMMING USING C++

Functions. Arizona State University 1

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CS 162, Lecture 25: Exam II Review. 30 May 2018

CSI33 Data Structures

Ch. 12: Operator Overloading

ComS 228 Exam 1. September 27, 2004

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

More class design with C++ Starting Savitch Chap. 11

pointers + memory double x; string a; int x; main overhead int y; main overhead

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Friends and Overloaded Operators

Classes. Christian Schumacher, Info1 D-MAVT 2013

ADTs & Classes. An introduction

Friends and Overloaded Operators

Fast Introduction to Object Oriented Programming and C++

CSC 1300 Exam 4 Comprehensive-ish and Structs

Programming in C++: Assignment Week 4

Exam 3 Chapters 7 & 9

Memory and Pointers written by Cathy Saxton

Make Classes Useful Again

Program construction in C++ for Scientific Computing

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

Function Overloading

Array Elements as Function Parameters

Programming in C++: Assignment Week 4

LECTURE 5 Control Structures Part 2

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Module 7 b. -Namespaces -Exceptions handling

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSCI 123 Introduction to Programming Concepts in C++

C++_ MARKS 40 MIN

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Roxana Dumitrescu. C++ in Financial Mathematics

Homework #3 CS2255 Fall 2012

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Inheritance, and Polymorphism.

Separate Compilation Model

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

Do not start the test until instructed to do so!

LECTURE 02 INTRODUCTION TO C++

CMSC 202 Midterm Exam 1 Fall 2015

a data type is Types

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Functions, Pointers, and the Basics of C++ Classes

Object oriented programming with C++

B16 Object Oriented Programming

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Lecture 3 ADT and C++ Classes (II)

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Programming II Lecture 2 Structures and Classes

Programming. C++ Basics

CS 247: Software Engineering Principles. ADT Design

Boolean Algebra Boolean Algebra

Constants, References

B16 Object Oriented Programming

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Overloading & Polymorphism

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Overloading Operators in C++

Assumptions. History

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Classes in C++98 and C++11

Programming Language. Functions. Eng. Anis Nazer First Semester

CS11 Introduction to C++ Fall Lecture 7

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

CSC 210, Exam Two Section February 1999

Object Oriented Programming: Inheritance Polymorphism

CS 376b Computer Vision

Operator overloading: extra examples

Due Date: See Blackboard

C++ Final Exam 2017/2018

UNIVERSITY OF SWAZILAND

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Pointers II. Class 31

Introduction Of Classes ( OOPS )

Polymorphism CSCI 201 Principles of Software Development

COEN244: Class & function templates

VIRTUAL FUNCTIONS Chapter 10

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Transcription:

Classes and Objects in C++: Operator Overloading CSC 112 Fall 2009

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4); cout << (r == s) << endl;

Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4); cout << (r == s) << endl; Illegal with our current definition of Rectangles (what we had learned up until 11/4). Essentially, compiler doesn t know what it means for two Rectangles to be the same.

Fortunately, C++ allows the programmer to redefine what a built-in in C++ operator means within user-defined types. Note, one cannot make use of built-in in operators in Java! As an example, the C++ test for equality is == int a = 3; int b = 3; if (a == b) Rectangle r(0,0,1,1); (,,,); Rectangle s(0,0,2,2); (,,,); if (r == s)

Must adhere to language defined operator signature when overloading Need to implement a definition for the operator given the context of the class the operator is defined in Define equality of Rectangles as: Same xposition,yposition start points Same width, same height

// tests whether input rectangle s is equal to // our rectangle, given definition on previous slide // signature needs to be in header file as well bool lr Rectangle::operator== (const Rectangle& r) { if ((xposition == r.xposition) && (yposition == r.yposition) && (height = r.height) && (width = r.width)) return true; else return false; } // Checks for equality in x1,y1,h,w,,

Shortcut Equality Test If actually working with same physical object, then pointers to two objects will be the same. Pointer to current object: this Pointer to other object: &inputobject Test for same object: if (this == &inputobject) if (this == &inputobject) Potential to save a lot of time if comparing large objects and often compare the same underlying object

// tests whether input rectangle s is equal to // our rectangle, given definition on previous slides bool Rectangle::operator== (const Rectangle& r) { if (this == &s) return true; else if ((xposition == r.xposition) && (yposition == r.yposition) && (height = r.height) height)&& )&&(width = r.width)) return true; else return false; } // checks if same object first (this pointer) // then checks for equality in xposition, yposition, height, width

A Key Idea: Calling A == B, where == is a function defined for the class, can also be done with this code: A.operator==(B) You are calling the member function operator== on object A with parameter object B. Fits with our syntax for defining overloaded operators Present in header and implemention file scoped as part of object Holds for most cases! There are some exceptions we ll see!

Assignment: Object objectone, objecttwo; objectone = objecttwo; objectone s variables are updated to be equal to objecttwo s variables Addition: Object objectone, objecttwo, objectthree; objectthree = objectone + objecttwo; objectone is not changed, even though + is called on it

Addition Semantics: Object objectone, objecttwo, objectthree; objectthree = objectone + objecttwo; Addition Implementation: Should generate new object of object type Should update variables of new object to be sum of variables from two input objects (the calling object and the parameter object). Return that new object.

Not all operators are member functions Don t make sense in the context of working on the current object

When using non-member functions: Not actually calling member operation on the object, but instead passing object in Can overload other operators this way, such as addition: friend Rectangle operator+ (const Rectangle & r1, const Rectangle & r2);

Symmetric Operations In some cases, need to use free, non-member functions to allow symmetry of operator Assume scaling operation using * (multiply), where width and height of rectangle are multiplied by a double input value: Rectangle r = r * 2; Rectangle r = 2 * r; Would like these to be equivalent

Symmetric Operations If * is a member function: Rectangle Rectangle::operator*(double input) Then having both operations work: Rectangle r = r * 2; Rectangle r = 2 * r; isn t yet defined in the language! The left-hand hand-side of the * must be a rectangle object, if only the above function is defined.

Symmetric Operations Actually need two functions to support symmetry: // member or non-member - Rectangle on Left Hand Side Rectangle Rectangle::operator*(double input); (equivalent to: Rectangle operator*(const Rectangle & r, double input); ) // and a non-member function Rectangle Right Hand Side Rectangle operator*(double input, const Rectangle & r);

Friend Functions For non-member functions to make use of private member variables or private member functions, they need to be declared as a friend. In Rectangle header file, define operator as: friend Rectangle operator +(const Rectangle & r1, const Rectangle & r2); (friend is only needed in the.h file, not the.cpp file) Meaning that the operator + function can directly read and write all private Rectangle variables and functions.

Friends A function is granted friend status from a class, allowing it to read and write private parts of the class. Friendship is a one-way function. The friend can read and write variables and functions from the granting class. The granting class does not have any privileged status with the friend just because it made it a friend.