Overloaded Operators, Functions, and Students

Similar documents
More Advanced Class Concepts

Overloading & Polymorphism

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)

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

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

Ch. 12: Operator Overloading

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Program construction in C++ for Scientific Computing

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

OBJECT ORIENTED PROGRAMMING USING C++

Instantiation of Template class

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Object-Oriented Design (OOD) and C++

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

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

Overloading Operators in C++

Classes and Operators. Ali Malik

Stacks and their Applications

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

Object-Oriented Principles and Practice / C++

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

Fundamentals of Programming CS-110. Lecture 3

2 ADT Programming User-defined abstract data types

Fast Introduction to Object Oriented Programming and C++

Operation Overloading.

CS 247: Software Engineering Principles. ADT Design

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Ch 8. Operator Overloading, Friends, and References

CSI33 Data Structures

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

A <Basic> C++ Course

A <Basic> C++ Course

Functions. Ali Malik

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

CS11 Intro C++ Spring 2018 Lecture 5

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

Assignment operator overloading in c++ syntax. Assignment operator overloading in c++ syntax.zip

Friends and Unary Operators

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

Object oriented programming

Computer Science II CSci 1200 Lecture 18 Operators and Friends

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

W3101: Programming Languages C++ Ramana Isukapalli

Do not write in this area TOTAL. Maximum possible points: 75

G52CPP C++ Programming Lecture 17

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

LECTURE 17. Expressions and Assignment

Operator overloading. Instructor: Bakhyt Bakiyev

AIMS Embedded Systems Programming MT 2017

PIC 10A Objects/Classes

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

PIC10B/1 Winter 2014 Exam I Study Guide

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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.

Operator overloading. Conversions. friend. inline

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Java is an objet-oriented programming language providing features that support

Fundamentals of Programming Session 7

IS0020 Program Design and Software Tools Midterm, Fall, 2004

7.1 Optional Parameters

COMP6771 Advanced C++ Programming

A flow chart is a graphical or symbolic representation of a process.

ADTs & Classes. An introduction

Where do we go from here?

Chapter 9. Operator Overloading. Dr Ahmed Rafat

Operator Overloading

Chapter 3. Numeric Types, Expressions, and Output

Object oriented programming

Operators in C. Staff Incharge: S.Sasirekha

Roxana Dumitrescu. C++ in Financial Mathematics

Programming in C++ 5. Integral data types

Module Operator Overloading and Type Conversion. Table of Contents

Lecture 3 ADT and C++ Classes (II)

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Inheritance and Polymorphism

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Binary Tree Implementation

Visual C# Instructor s Manual Table of Contents

SFU CMPT Topic: Class Templates

Functions. Ali Malik

JAVA OPERATORS GENERAL

Inheritance and Polymorphism

Operators & Expressions

Chapter 18 - C++ Operator Overloading

Lesson 13 - Vectors Dynamic Data Storage

Binary Tree Implementation

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

Lecture 4 Tao Wang 1

Operator overloading

Evolution of Programming Languages

A Case Study of Gang of Four (GoF) Patterns : Part 7

Circle all of the following which would make sense as the function prototype.

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

Introduction to C ++

C++ (Non for C Programmer) (BT307) 40 Hours

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

pointers & references

CS 7B - Spring Final Exam

Transcription:

, Functions, and Students Division of Mathematics and Computer Science Maryville College

Outline Overloading Symbols 1 Overloading Symbols 2 3

Symbol Overloading Overloading Symbols A symbol is overloaded if it has multiple meanings. Overloading is frequently called "ad-hoc polymorphism". Also referred to as syntactic sugar. Overloading is meant to keep code close to the actual program domain. Establishes different behaviors while allowing the programmer to express intent.

Overloaded Function Example: abs In C int abs(int j); double fabs(double x); In C++ int abs(int j); double abs(double x);

C++ Function Signature A Normal Function Signature Consists Of Name Number of Parameters Parameter Types C++ Overloading Rules In C++, overloaded functions share the same name. They must vary in at least one other part of the normal signature.

The Overloaded Accessor/Mutator Idiom std::string firstname(); void firstname(const std::string & _firstname); Both functions have the same name firstname Both functions differ by number of arguments. The compiler uses the function signature to determine which function to execute. Reusing the same name means less information for the the programmer to remember!

Operators as Functions An operator could be viewed as a function! A binary operator takes two arguments: a + b == add(a, b) A unary operator takes one argument: ++a == increment(a) Like functions, operators can be overloaded. Existing operators cannot be overridden, but you can overload types so long as they use at least one class or enumerated type.

Operator Overloading Syntax <return_type> operator<symbol>(<parameters>) Example an Overloaded < Operator bool operator<(const PhoneRecord &lhs, const PhoneRecord &rhs) { if(lhs.lastname() < rhs.lastname()) { return true; } } return lhs.firstname() < rhs.firstname();

Overloading Arithmetic Operators type operator+(const type &lhs, type operator-(const type &lhs, type operator*(const type &lhs, type operator/(const type &lhs, type operator%(const type &lhs,

Overloading Increment and Decrement Operators Prefix Operators ++x; type & operator++(type & lhs); type & operator--(type & lhs); Postfix Operators x++; type operator++(type & lhs, int); type operator--(type & lhs, int);

Overloading Equality and Comparison Operators bool operator==(const type &lhs, bool operator!=(const type &lhs, bool operator<(const type &lhs, bool operator<=(const type &lhs, bool operator>(const type &lhs, bool operator>=(const type &lhs,

Overloading Assignment Operators type operator=(type &lhs, type operator+=(type &lhs, type operator-=(type &lhs, type operator*=(type &lhs, and so on...

Overloading Index Operators Often we want to create indexable containers. Consider vector indexing: v[0]; This is an overloaded operator!: type & operator[](type &lhs, int index);

Overloaded Stream Operators Insertion Operator ostream &operator<<(ostream &os, Extraction Operator istream &istream>>(istream &is,

as Member Methods An overloaded operator can be a member method! When an overloaded operator is a member method, the left hand side is the current object. Only the right hand side is specified in the parameter list. Example: The PhoneRecord s index operator. PhoneNumber & operator[](int index);

Don t Mislead Users of Your Classes! Overloaded operators are meant to make code match its problem domain. Your overloaded operators should match the original purpose of the operator. Avoid side effects, unless they are expected. If an operator doesn t have an obvious relationship to your class s operations, don t overload it!

Meet User s Expectations! It is a good idea to overload at least assignment operators. If your class is likely to have objects in a container, provide at least == and < operators. Insertion and Extraction Operators are sometimes expected, but don t use them in situations where the I/O of the object is not well defined. If you do provide insertion and extraction operators, the insertion operator s output should be valid input for the extraction operator.

Keep it Inside the Class. Unless its impossible, operators should be member classes. Operators that do not change the current object should be marked const. Avoid the use of friend for insertion and extraction operators. If you find you must make a stream operator a friend, chances are your class API is incomplete!