Operator Overloading www.lpuians.com.

Slides:



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

Introduction to Programming Lecture 31. Operator Overloading.
Operator Overloading Fundamentals
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator Overloading. 2 Objectives Discuss operator overloading –definition –use –advantages –limitations Present type conversion operators.
Operator overloading Object Oriented Programming.
Type Conversions. 2 When different types of variables are mixed in an expression, C applies automatic type conversion to the operands The type of data.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Learners Support Publications Constructors and Destructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
Constructors and Destructors
Type conversion RITIKA SHARMA.
Operator Overloading Ritika Sharma.
User-Written Functions
Chapter 13: Overloading and Templates
Introduction to C++ Systems Programming.
Programming with ANSI C ++
Java Primer 1: Types, Classes and Operators
Developed By : Ms. K. S. Kotecha
Object-Oriented Programming (OOP) Lecture No. 21
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Operators and Expressions
Contents Introduction to Constructor Characteristics of Constructor
Andy Wang Object Oriented Programming in C++ COP 3330
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Operator Overloading, Friends, and References
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Chapter 9: Value-Returning Functions
COP 3330 Object-oriented Programming in C++
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Chapter 6 Polymorphism.
Andy Wang Object Oriented Programming in C++ COP 3330
Unit-1 Introduction to Java
Constructors & Destructors
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Operator Overloading www.lpuians.com

Introduction Mechanism of giving special meaning to an operator is known as operator overloading. Gives option to give new definitions to operators. Syntax can’t be changed; but semantics can be.

Excluded operators Class member access operators: ( . , .* ) Scope resolution operator ( :: ) Size operator ( sizeof ) Conditional operator ( ?: )

Defining operator overloading To define additional task to operator, define its meaning. Special function called operator function is used. General form of operator function is:

Operator functions Operator functions must be either member or friend functions. Friend Function Member Function Unary Operator 1 Binary Operator 2

Operator invocation Overloaded operator functions can be invoked (for unary operator) as: op x or x op Can be interpreted as: operator op (x) Example: - S or S - Object

Operator invocation Overloaded operator functions can be invoked (for binary operator) as: x op y Can be interpreted as: x . operator op (y)  for member function operator op (x , y)  for friend function

Overloading Unary operators

Overloading Unary operators Using Friend function, it works as:

Overloading Binary operators Consider following example to add 2 complex numbers using friend function as:

Overloading + operator

+ operator overloaded

The invocation: C3 = C1 + C2 is equivalent to: Data members of C1 are accessed directly & that of C2 are accessed using dot operator

Overloading Binary operators using Friends Friend functions can be used instead of member functions for overloading binary operator. It requires 2 arguments to be passed explicitly; whereas member function requires only one.

Previous example can be modified as: Complex operator + (complex a, complex b) { complex temp; temp.x = a.x + b.x; temp.y = a.y + b.y; return (temp); }

Difference between Friend & Member function Consider situation where 2 different data types are added as: A = B + 2; works for member function, but statement: A = 2 + B; will not work. 1st argument used to invoke function

Difference between Friend & Member function However, friend function allows both approaches as: X op Y is interpreted as: operator op (X , Y) And for member function, it can be interpreted as: X . operator op (Y)

Declaration of Operator function Example Declaration of Operator function

Definition of operator * functions Example (cont.) Definition of operator * functions

Example (cont.)

Point to be noted

Type Conversion The statements: converts x to integer before assigning to m. Type conversions are automatic for built-in data-types.

Consider following statement: v3 = v1 + v2 //v1, v2, v3 are objects When objects are of same class, compiler doesn’t complain. Compiler doesn’t support type conversion for user-defined data-types. Therefore, conversion routines have to be developed.

www.lpuians.com Hence, 3 situations arrive:

Basic  Class type Built-in variable (duration) assigned to member variables Constructor does the job

Class  Basic type C++ allows us to define overloaded casting operator to convert class-type to basic-type. It converts class-type data to typename. Example: int num = obj; //object to int

Returning “double” value Example Returning “double” value

www.lpuians.com

One Class  other Class type Destination Class object Source Class object Conversion can be carried out using: Constructor function (or) Conversion function

(a) Conversion function (used in source class) Recall that, Casting operator function: operator typename() converts: class object  typename() typename() can be built-in or user-defined type.

(b) Constructor function (used in destination class) Here, argument belongs to source class and passed to destination class for conversion. Hence, conversion constructor is placed in destination class.

Example

Conversion between objects

www.lpuians.com Conversion summary