Introduction to Programming Lecture 31. Operator Overloading.

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Operator overloading redefine the operations of operators
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
CSCE 2100: Computing Foundations 1 Intro to Advanced C++
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
Monday, 10/14/02, Slide #1 CS 106 Intro to CS 1 Monday, 10/14/02  QUESTIONS??  Today: More on classes!  Reading: None new  Exercises: Implement and.
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
1 CSC241: Object Oriented Programming Lecture No 05.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
SE1011 Week 8, Class 1 Today Return Half Exam 3 (I have it with me) Object Oriented Programming Details References as arguments Overloaded methods Garbage.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Image from
Programming with ANSI C ++
Operator Overloading.
C++ : Operator overloading example
Introduction to Programming
Operator Overloading Ritika Sharma.
Operator Overloading.
Lecture 9: Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Object-Oriented Programming (OOP) Lecture No. 17
Operator Overloading BCA Sem III K.I.R.A.S.
Real People: Healthy Body, Healthy Body Image
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Object-Oriented Programming (OOP) Lecture No. 18
Introduction to Programming
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 20
Introduction to Programming
Introduction of Programming
Object Oriented Programming
Introduction to Programming
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Operator Overloading I
Review of Function Overloading
Object oriented programming (OOP) Lecture No. 6
Unit-1 Introduction to Java
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 18 October 31, 2008.
Introduction to Programming
Group #? member name Architect’s Name Here.
Presentation transcript:

Introduction to Programming Lecture 31

Operator Overloading

Today’s Lecture – Operators – Syntax for overloading operators operators – How to overload operators ?

Complex Number

complex c1, c2, x ; x = cadd ( c1, c2 ) ;

x = cadd ( cadd ( a, b ), c ) ;

Operators The complete list of C++ operators that are overloaded is as follows The complete list of C++ operators that are overloaded is as follows + - * / % ^ & | ~ ! = += -= *= /= %= ^= &= |= > >>= > >>= <<= = = != <= >= && | | > *, -> [ ] ( ) new new[ ] delete delete [ ]

a + b

Date.day

Example Return_type operator + (Argument_List) { // Body of function }

a * b + c ;

x = y + z ;

Example class Complex { private : double real ; double imag ; public : // member function }

Example Complex c1, c2 ; c1 = c2 ; Is equivalent to c1.real = c2.real ; c1.imag = c2.imag ;

Complex operator + ( Argument_ list ) ;

Complex Complex :: operator + ( Complex c ) { Complex temp ; temp.real = real + c.real ; temp.imag = imag + c.imag ; return temp ; } Example

Complex x, y, z ; z = x + y ;

z = x + d ; Complex Number Double Precision Number

Complex operator + ( double d ) ;

z = x + y ; z = x + d ;

Complex Complex :: operator + ( Complex c ) { Complex temp ; temp.real = real + d ; temp.imag = imag ; return temp ; } Example

z = d + x ; Complex Number Double Precision Number Complex Number

Friend Function

User Defined Data types

<<

Example main ( ) { Complex c1 ( 1, 2 ), c2 ( 3, 4 ), c3 ; c3 = c1 + c2 ; c1.display ( ) ; c2.display ( ) ; c3.display ( ) ; }

Complex operator + ( Complex & c ) ; C is a reference to a complex number

i += 2 ; i = i + 2 ;

c1 += c2 ;

Example Complex operator += ( Complex & c )

Example Complex Complex :: operator += ( Complex & c ) { real += c.real ; imag += c.imag ; }

Complex operator + ( Complex & c1, Complex & c2 ) { Complex temp ; temp.real = c1.getreal ( ) + c2.getreal ( ) ; temp.imag = c1.getimag ( ) + c2.getimag ( ) ; return temp ; } Example

Example class String { private : private : char s [ 30 ] ; char s [ 30 ] ; public : public : String ( ) String ( ) { strcpy ( s, "" ) ; strcpy ( s, "" ) ; } // Declaration (prototype) of overloaded sum operator // Declaration (prototype) of overloaded sum operator String operator + ( String c ) ; String operator + ( String c ) ; } ;

Example String String :: operator + ( String c ) { String temp ; String temp ; strcpy ( temp.s, "" ) ; strcpy ( temp.s, "" ) ; strcat ( temp.s, s ) ; strcat ( temp.s, s ) ; strcat ( temp.s, c.s ) ; strcat ( temp.s, c.s ) ; return temp ; return temp ;}