Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Operator Overloading Fundamentals
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Object Oriented Programming in C++ Chapter5 Operator Overloading.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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 and type convesions BCAS,Bapatla B.mohini devi.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CONSTRUCTOR AND DESTRUCTORS
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Ritika Sharma.
Programming with ANSI C ++
Introduction to Computers Computer Generations
Chapter 13: Overloading and Templates
Data types Data types Basic types
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Programming with ANSI C ++
Constructor & Destructor
Chapter 15: Overloading and Templates
Precedence and Associativity
Programming with ANSI C ++
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Programming with ANSI C ++
Overview of C++ Overloading
Operator overloading Dr. Bhargavi Goswami
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
Languages and Compilers (SProg og Oversættere)
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Presentation transcript:

Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

and User Defined Conversions Chapter 6 Operator Overloading and User Defined Conversions

Introduction Give an additional meaning to an existing operator A Meaningful meaning! Is important The operator functions

Operator Overloading and User Defined Conversions Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -

Introduction Give an additional meaning to an existing operator A Meaningful meaning! Is important The operator functions

Restrictions on use of OO Cannot change the original meaning New operators cannot be devised Ops will not have additional precedence. cannot change number of arguments at least one argument as user defined type Except (), no other operator can have a default argument. same return type for new, delete and ->

Operators which can not be overloaded The dot operator for member access The dereference member to class operator .* Scope resolution operator :: Size of operator sizeof Conditional ternary operator ?: Casting operators static_cast<>, dynamic_cast<>, reinterpret_cast<>, const_cast<> # and ## tokens for macro preprocessors

Operators which can not be overloaded as friends Assignment Operator = Function call operator () Array Subscript operator [] Access to class member using pointer to object operator -> To ensure their first operand is lvalue! = has global meaning as well!

The operator function The need for word operator preceding the real operator like operator + No explicit call! Better as a library function! Users do not need to remember function names No ambiguity about parameters and call Readability increases No misspelled functions

Types of operators Unary Operators Binary Operators The dummy arguments for postfix versions of increment and decrement operators Binary Operators Overloading shorthand operators while overloading arithmetic operators The single ternery operator is not available

Using Friends The lvalue problem for cases like Object + scaler and Scaler + Object calls to + The body for both the cases can be different! The operator << and >> case The need to return reference

Overloading other Operators The = operator Normal Overloading Extending = in the derived class The [] operator The safearray class example The need to have reference as return type Can be extended using templates

Overloading other Operators The () operator The function object vs. Function pointers Inlining is possible May have cache info and help The new and delete using malloc and free The New and delete using ::new and ::delete

Overloading new and delete Before Insertion After First Insertion Old Pointer Table Space pointer Current Pointer Null After 8th Insertion Overloading new and delete

User Defined Conversions Conversion from built-in data type to an object Object to a built-in data type The need for such operator: the wrapper class example Object to Object Conversion using constructors using conversion functions

Operator Overloading and User Defined Conversions Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -