Operator Overloading.

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Operator Overloading Fundamentals
Operator overloading. Operatorsand precedence zLeft to right associative y:: scope resolution y. direct member access y-> indirect member access y[] subscripting.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Object Oriented Programming in C++ Chapter5 Operator Overloading.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Operator Overloading in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.
OPERATOR OVERLOADING WEEK 4-5 CHAPTER 19. class Money {private:int lira; int kurus; public: Money() {}; Money(int l, int k) { lira=l+ k/100; kurus=k%100;
CONSTRUCTOR AND DESTRUCTORS
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 08.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++
Department of Software.
Programming with ANSI C ++
C++ : Operator overloading example
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Yan Shi CS/SE 2630 Lecture Notes
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Operator Overloading; String and Array Objects
Computing Fundamentals
Visit for more Learning Resources
Polymorphism in C++ Operator Overloading
Objectives Define polymorphism Overload functions
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Chapter 15: Overloading and Templates
Precedence and Associativity
Operator Overloading BCA Sem III K.I.R.A.S.
Operators and Expressions
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Operator Overloading
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Operators Lecture 10 Fri, Feb 8, 2008.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Starting Out with C++: From Control Structures through Objects
C Operators, Operands, Expressions & Statements
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
Operator overloading Dr. Bhargavi Goswami
Operator Overloading, Friends, and References
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Introduction to Programming – 4 Operators
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 4 PART 2 GEORGE KOUTSOGIANNAKIS
Andy Wang Object Oriented Programming in C++ COP 3330
Constructors and Deconstructor
Presentation transcript:

Operator Overloading

Outline Revision What is Operator Overloading Why Operator Overloading Overloading Unary operators Overloading Binary operators Overloading Assignment operator

Revision Operator Overloading is a mechanism to redefine built-in operations for user defined types It allows to give normal C++ operators such as +,-,==,< additional meanings Operator Overloading should be used to perform the same function or similar function on class objects as the built-in behavior

Revision Operator overloading is a very neat feature of object oriented programming in C++ Each individual operator must be overloaded for use with user defined types Overloading the assignment operator and the subtraction operator does not overload the -= operator.

Things to keep in mind At least one of the operands in any overloaded operator must be a user-defined type operator + for one integer and one double, NOT possible Can only overload the operators that exist. All operators keep their current precedence and associativity, regardless of what they're used for

Operator Functions Operator functions may be defined as either member functions or as non-member functions. Non-member functions are usually made friends for performance reasons. Member functions usually use the this pointer implicitly.

Commonly Overloaded Operators Unary ++, --, (increment, decrement) Binary +, -, *, /, % (arithmetic) =, +=, -=, *=, /=, %= (assignment) ==, !=, >, <, >=, <= (relational) <<, >> (I/O) ||, && (logical) &, |, ^, ^=, &=, |= (bitwise)

Non-overloadable Operators . (dot operator / member selector) :: (scope resolution operator) ?: (conditional operator / arithematic if) .* (member pointer selector) sizeof

Overloading Unary Operator Increment Operators ( ++) Unary Operators ( --)

Example #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } void operator ++ () //increment (prefix) ++count; } };

int main() { Counter c1, c2; //define and initialize cout << “\nc1=” << c1.get_count(); //display cout << “\nc2=” << c2.get_count(); ++c1; //increment c1 ++c2; //increment c2 cout << “\nc1=” << c1.get_count(); //display again cout << “\nc2=” << c2.get_count() << endl; return 0; }

Overloading Binary Operators Arithmetic Operators Assignment Operators Comparison Operators Arithmetic Assignment Operators