Operator overloading: extra examples

Similar documents
Operator overloading

Module Operator Overloading and Type Conversion. Table of Contents

7.1 Optional Parameters

Week 8: Operator overloading

Programming Assignment #2

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Overloading Operators in C++

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Operator Overloading

Introduction. W8.2 Operator Overloading

Getting started with C++ (Part 2)

W8.2 Operator Overloading

Program construction in C++ for Scientific Computing

Operator Overloading

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

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

CS 247: Software Engineering Principles. ADT Design

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Object oriented programming

CIS 190: C/C++ Programming. Classes in C++

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Come and join us at WebLyceum

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

Object-Oriented Principles and Practice / C++

Short Notes of CS201

Chapter 9 Technicalities: Classes, etc.

Flashback Technicalities: Classes, etc. Lecture 11 Hartmut Kaiser

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

CS2141 Software Development using C/C++ C++ Basics

CS201 - Introduction to Programming Glossary By

C++ Programming Assignment 3

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

III. Classes (Chap. 3)

Chapter 9 Technicalities: Classes, etc. Walter C. Daugherity Lawrence Pete Petersen Bjarne Stroustrup Fall 2007

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 7. Log into Linux New documents posted to course webpage

Chapter 9 Technicalities: Classes, etc.

For Teacher's Use Only Q No Total Q No Q No

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Roxana Dumitrescu. C++ in Financial Mathematics

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 9 Technicalities: Classes

A class is a user-defined type. It is composed of built-in types, other user-defined types and

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

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

COMP322 - Introduction to C++

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

G52CPP C++ Programming Lecture 17

PIC 10A Objects/Classes

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

SFU CMPT Topic: Classes

Computer Science II CSci 1200 Lecture 18 Operators and Friends

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Operator overloading. Instructor: Bakhyt Bakiyev

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

CE221 Programming in C++ Part 1 Introduction

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

W3101: Programming Languages C++ Ramana Isukapalli

BITG 1233: Introduction to C++

Introduction Of Classes ( OOPS )

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

typedef int Array[10]; String name; Array ages;

UNIT- 3 Introduction to C++

Chapter 2: Basic Elements of C++

Lecture 10: Boolean Expressions

Object oriented programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Fast Introduction to Object Oriented Programming and C++

Types, Operators and Expressions

Ch. 12: Operator Overloading

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Types, Operators and Expressions

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Functions and Recursion

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

CS201- Introduction to Programming Current Quizzes

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

pointers & references

EEE145 Computer Programming

Programming Language. Functions. Eng. Anis Nazer First Semester

Instantiation of Template class

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

ADTs & Classes. An introduction

C++ for Java Programmers

Lecture 23: Pointer Arithmetic

More Advanced Class Concepts

Computer Science II Lecture 1 Introduction and Background

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

The University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value

Solved Exercises from exams: Midterm(1)

Transcription:

Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading operator+ Overloading operator+ again 2 Unary operators 3 The Conditional Operator?: 4 Prefix and Postfix ++ 5 Overloading ++ Example: Date 6 friend functions 7 Another example: complex numbers 8 Constants 9 friends again CS319 Operator overloading: extra examples 1/30

Eg 1: Points in the (x, y)-plane We will create an object to represent points in the (x, y)-plane: with two private members: floats x and y, constructor to initialise x and y; it will have default values. a public function Get, used to find the values of x and y, a public function Set that can be used to set the values of x and y, a public overloaded operator + to add two points. CS319 Operator overloading: extra examples 2/30

Eg 1: Points in the (x, y)-plane Our first version of class declaration is shown opposite. The constructor function is defined in the declaration of the class, and has default values. 01Points.cpp 1 class Point private : 3 float x,y; public : 5 Point ( float i=0, float j =0) x=i; y=j ;; void Set ( float i, float j) x=i; y=j ;; 7 void Get ( float &i, float &j) i=x; j=y ;; ; If an object of type Point is declared as Point a; then a.point() is called as if it was Point(0.0, 0.0). If a is declared as: Point a(-2,3); then these two floats are passed to the constructor. The public Set() allows us to set the values of a.x and a.y In the definition of Get(), the integer variables are passed by reference and not by value therefore the value of i and j are modified by the function. CS319 Operator overloading: extra examples 3/30

Eg 1: Points in the (x, y)-plane Overloading operator+ Now we add the code for the + operator. First, to the class definition we add the declaration of the operator: So now the class definition is Point operator+(point b); class Point 2 private : float x,y; 4 public : Point ( float i=0, float j =0) x=i; y=j ;; 6 void Set ( float i, float j) x=i; y=j ;; void Get ( float &i, float &j) i=x; j=y ;; 8 Point operator +( Point b); ; Notice that it seems to suggest that + takes just one argument... CS319 Operator overloading: extra examples 4/30

Eg 1: Points in the (x, y)-plane And then we give the definition: 10 Point Point :: operator +( Point b) 12 Point temp ; temp.x = x + b.x; 14 temp.y = y + b.y; return temp ; 16 Overloading operator+ The first thing to notice is that, although + is a binary operator, our function takes only one argument. This is because, when we call the operator, e.g., c = a + b then a is passed implicitly to the function and b is passed explicitly. Therefore a.x is known to the function simply as x. The temporary object temp is used inside the object to store the result. It is this object that is return. Neither a or b are modified.. CS319 Operator overloading: extra examples 5/30

Eg 1: Points in the (x, y)-plane Overloading operator+ again We ll now try overloading the + operator again, for the purpose of introducing the *this pointer. Suppose that we have a point b = (3.1, 2.2). If we write a = b + 0.5 and mean that we want to set a = (3.6, 2.7). class Point private: float x,y; public: Point(float i=0, float j=0) x=i; y=j;; void Set(float i, float j) x=i; y=j;; void Get(float &i, float &j) i=x; j=y;; Point operator+(point b); Point operator+(float p); ; CS319 Operator overloading: extra examples 6/30

Eg 1: Points in the (x, y)-plane Overloading operator+ again And the rest of the code would be or Point Point::operator+(float p) Point temp; temp.x = this->x + p; temp.y = this->y + p; return temp; Point Point::operator+(float p) Point temp(*this); temp.x += p; temp.y += p; return temp; CS319 Operator overloading: extra examples 7/30

Unary operators So far we have discussed just the binary operator +. That is, it is an example of an operator that takes two arguments. But many operators in C/C++ are Unary: they take only one argument. The most common examples of unary operators are ++ and --, but we ll first over load the - (minus) operator. Note that this can be used in two ways: c = a b c = a (binary) (unary). In the second case here, minus is an example of a prefix operator. These are the easiest. CS319 Operator overloading: extra examples 8/30

Unary operators ////////////////////// // Overloading minus Point Point::operator-(Point b) Point temp; temp.x = x - b.x; temp.y = y - b.y; return temp; Point Point::operator-(void) Point temp; temp.x = -x; temp.y = -y; return temp; CS319 Operator overloading: extra examples 9/30

The Conditional Operator?: C and C++ have a selection of unary operators, e.g., +, -,!, ++ and --. And binary operators, e.g., +, -, *, /, %, =, <, >, +=, etc. But it has only one ternary operator (one that takes 3 arguments), and that is the conditional operator?:. Syntax: Cond? Op of Cond True : Op if Cond False CS319 Operator overloading: extra examples 10/30

The Conditional Operator?: 8 int Score ; std :: string Grade, Outcome ; See 02Ternary.cpp for more details std :: cout << " Enter your score : "; 12 std :: cin >> Score ; 14 ( Score >= 40)? Grade =" Pass " : Grade =" Fail "; std :: cout << " You have " << Grade << " ed." << std :: endl ; // Alternative 18 std :: cout << " Enter another score : "; std :: cin >> Score ; 20 Outcome = ( Score >= 40)? " will not " : " will "; std :: cout << " You " << Outcome << " have to come back in Augest!" 22 << std :: endl ; CS319 Operator overloading: extra examples 11/30

Prefix and Postfix ++ We all know that in C/C++ one can use the ++ and -- operators in prefix and post fix forms, e.g., ++a (prefix) a++ (postfix) But what is the difference? In C/C++, all expressions including assignments such as a++ must evaluate as something. So ++a evaluates as a+1, the value after incrementation, a++ evaluates as a, the value before incrementation. output int a, b; 2 a =1; b=( a ++); std :: cout << "a=" << a << ", b=" << b << endl ; a =1; b =(++ a); 6 std :: cout << "a=" << a << ", b=" << b << endl ; a=2, b=1 a=2, b=2 CS319 Operator overloading: extra examples 12/30

Prefix and Postfix ++ And there is a further distinction between the preincrement (++a) and postincrement (a++) operators: the preincrement operator can be used as the left operand in an assignment. // Use prefix and postfix ++ 2 # include <iostream > int main ( void ) 4 int a, b; a =10; b =20; 8 // The following would be illegal // (a++)=b; 10 (++ a)=b; // <- But this is OK! std :: cout << "a=" << a << ", b=" << b << std :: endl ; 12 return (0); CS319 Operator overloading: extra examples 13/30

Overloading ++ Overloading both the prefix and postfix versions of ++ involve a number of technicalities, specifically (1) There has to be a way of distinguishing between the two forms. This is achieved by giving the postincrement operator a dummy int as an argument. That way the two versions have different signatures. (2) We may wish to maintain the usual way the operators work (i.e., whether they evaluate as the original or incremented version, and if they can be used as left values in assignment operations). This is achieved as follows: (a) The postincrement operator makes a copy of the object before the increment, and then returns the (original, unincremented) copy. (b) Typically, preincrement operator will return a reference (address), and the postincrement operator will return a value. This allows the preincrement op to be used as the left value in an assignment. The example we ll use to study this is a class to implement the current date. CS319 Operator overloading: extra examples 14/30

Overloading ++ Example: Date 03Date++.cpp 1 // CS319 Week 9 (extras) // Example of overloading the ++ operator 3 // Implemented class represents a calendar date # include <iostream > int StandardDaysMonth [] = 7 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; 9 class Date public : 11 Date ( int d=1, int m=1, int y =1901); void SetDate ( int dd, int mm, int yy ); 13 int GetDay ( void ) return day ;; int GetMonth ( void ) return month ;; 15 int GetYear ( void ) return year ;; bool IsLeapYear ( void ); Date & operator ++( void ); // prefix version 19 Date operator ++( int Dummy ); // postfix 21 private : int day, month, year ; 23 int * DaysMonth ; void Increment ( void ); 25 ; CS319 Operator overloading: extra examples 15/30

Overloading ++ Example: Date 28 // Constructor Date :: Date ( int d, int m, int y) 30 DaysMonth = new int [13]; 32 for ( int i=0; i <=12; i++) DaysMonth [i] = StandardDaysMonth [i]; 34 SetDate (d,m,y); 38 bool Date :: IsLeapYear ( void ) 40 if ( ( year %400 == 0) ( ( year %4 == 0) && ( year %100!= 0))) 42 return true ; else 44 return false ; CS319 Operator overloading: extra examples 16/30

Overloading ++ Example: Date void Date :: SetDate ( int d, int m, int y) 48 year =y; 50 if ( IsLeapYear ()) DaysMonth [2]=29; month = ( (m >=1) && (m <=12) )? m : 1; 56 day = ( (d >=1) && (d <= DaysMonth [ month ]) )? d : 1; 58 // Will call this in both prefix and postfix versions. void Date :: Increment ( void ) 60 if ( day!= DaysMonth [ month ]) 62 day ++; else if ( month!= 12 ) 64 month ++; day =1; 66 else 68 SetDate (1,1, year +1); // Need to set the leap year CS319 Operator overloading: extra examples 17/30

Overloading ++ Example: Date Date & Date :: operator ++( void ) // Prefix version 72 Increment (); 74 return (* this ); Date Date :: operator ++( int Dummy ) // Postfix version 78 Date temp =* this ; 80 Increment (); return ( temp ); 82 CS319 Operator overloading: extra examples 18/30

Overloading ++ Example: Date 84 int main ( void ) 86 Date today (01,03,2017); Date tomorrow, NextMonday ; cout << " Today is " << today. GetDay () << "/" << 90 today. GetMonth () << "/" << today. GetYear () << endl ; 92 tomorrow =(++ today ); cout << " Tomorrow is " << tomorrow. GetDay () << "/" << 94 tomorrow. GetMonth () << "/" << tomorrow. GetYear () << endl ; for ( int i=1; i <5; i++) 98 ++ today ; 100 NextMonday = today ; cout << " Next Monday is " << NextMonday. GetDay () << "/" << 102 NextMonday. GetMonth () << "/" << NextMonday. GetYear () << endl ; 104 cout << endl ; 106 return (0); CS319 Operator overloading: extra examples 19/30

friend functions In all the examples that we have seen so far, the only functions that may access private data belonging to an object has been a member function/method of that object. However, it is possible to designate non-member as being friends of a class. For non-operator functions, there is nothing that complicated about friends. However, care must be taken when overloading operators as friends. In particular: All arguments are passed explicitly to friend functions/operators. Certain operators, particularly the Put to << and Get from >> operators can only be overloaded as friends. CS319 Operator overloading: extra examples 20/30

friend functions 04DatePutTo.cpp 1 class Date // New version to overload << 3 friend std :: ostream & operator <<( std :: ostream &, const Date &); 5 public : Date ( int d=1, int m=1, int y =1901); 7.. 9. std :: ostream & operator <<( std :: ostream & output, const Date &d) 11 std :: string MonthName [13]= "", " Jan ", " Feb ", " Mar ", " Apr ", 13 " May ", " Jun ", " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec "; 15 output << d. day << "/" << MonthName [d. month ] << "/" << d. year ; 17 return ( output ); CS319 Operator overloading: extra examples 21/30

Another example: complex numbers In order to increase the number of examples of classes that you have studied, and to give a framework to consider come technical issues, we introduce a class for Complex Numbers. A complex number is z = a + bi where a and b are real numbers and i = 1. So our class will have two private data elements for the real and imaginary parts, as we as methods to get and set their values. CS319 Operator overloading: extra examples 22/30

Another example: complex numbers 05Complex.cpp c l a s s Complex p r i v a t e : f l o a t r e a l, imag ; p u b l i c : Complex ( f l o a t r =0.0, f l o a t i =0.0) Set ( r, i ) ; ; f l o a t GetReal ( v o i d ) r e t u r n ( r e a l ) ; ; f l o a t GetImag ( v o i d ) r e t u r n ( imag ) ; ; v o i d Set ( f l o a t r, f l o a t i = 0. 0 ) ; ; v o i d Complex : : Set ( f l o a t r, f l o a t i ) r e a l=r ; imag=i ; CS319 Operator overloading: extra examples 23/30

Constants Constant Variables: We are familiar with the idea of a const variable. This is one whose value can not be changed after initialisation. Constant Functions: In C++ one may also force a class method to be constant. This is done by placing the keyword const at the end of the line containing the function prototype and header. The effect is that the function cannot modify the object. Furthermore, if you wish to call a method belonging to a constant object, then the method must be constant. In our example, we might define const Complex I(0,1) to represent i = 1. But a call to I.GetReal() or I.GetImag() as presented above would give an error, such as error: passing const Complex as this argument of float Complex::GetImag() discards qualifiers CS319 Operator overloading: extra examples 24/30

Constants c l a s s Complex p r i v a t e : f l o a t r e a l, imag ; p u b l i c : Complex ( f l o a t r =0.0, f l o a t i =0.0) Set ( r, i ) ; ; f l o a t GetReal ( v o i d ) c o n s t ; f l o a t GetImag ( v o i d ) c o n s t ; v o i d Set ( f l o a t r, f l o a t i = 0. 0 ) ; ; f l o a t Complex : : GetReal ( v o i d ) c o n s t r e t u r n ( r e a l ) ; f l o a t Complex : : GetImag ( v o i d ) c o n s t r e t u r n ( imag ) ; CS319 Operator overloading: extra examples 25/30

friends again Let s return to the idea of a friend function. This is a function that does not belong to the class, but still has access to private elements of a class. There are two reasons you might want a function to be a friend of a class Efficiency: If the called function is not a friend, it will have to use some method to access private members. This can be slower than being able to access it directly. Overloading: When overloading an member operator for a class, the left argument must be an object of that class. So, for example, Complex x ( 2. 0, 3. 0 ), z ; f l o a t f =3.0; z = x x ; // * operator can be a member z = x f ; // * operator can be a member z = f x ; // * operator can t be a member CS319 Operator overloading: extra examples 26/30

friends again It is because the left argument of an overloaded operator mush be an object of the class, that we can only overload the stream insertion operator << and stream extraction operator >> as friends. To see how this is done, have a look at 06ComplexMult.cpp This example also shows the three ways of overloading the * operator. Note that, for a member operator, the left argument is implicit, and only the right argument is listed explicitly, for a non-member function, both the left and right arguments must be listed. CS319 Operator overloading: extra examples 27/30