Operator Overloading - PowerPoint PPT Presentation

1 / 69
About This Presentation
Title:

Operator Overloading

Description:

6.6 Case Study: AC Current Calculator. 3. Introduction ... of poor programming practice may cause difficulties in source code comprehension. ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 70
Provided by: deronCsi
Category:

less

Transcript and Presenter's Notes

Title: Operator Overloading


1
Chapter 6
  • Operator Overloading

2
Outlines
  • 6.1 Fundamentals of Operator Overloading
  • 6.2 Operator Functions
  • 6.3 Overloading Binary Operators
  • 6.4 Overloading Unary Operators
  • 6.5 Overloading the Stream Operators
  • 6.6 Case Study AC Current Calculator

3
Introduction
  • The use of operators enables programmers to write
    concise expressions and produce clearer code.
  • It provides a tool called operator overloading
    that enables programmers to use existing
    operators to manipulate objects of their own
    classes.

4
FUNDAMENTALS OF OPERATOR OVERLOADING
  • Operator overloading is a special type of
    function overloading.
  • It is a process in which programmers instruct the
    C compiler to apply existing operators in an
    effort to manipulate objects of their own
    abstract data types.
  • An operator is overloaded if it can be used to
    perform multiple operations.
  • The operator
  • used to multiply two values of built in types
    such as int, ,float, or double,
  • to declare or dereference a pointer.
  • It can be further overloaded to multiply two
    objects of user defined types.

5
Example
  • int x3, y5, z
  • zxy // multiplication
  • float f3.4
  • float fptf // is used to declare a pointer
  • fpt5.6 // is used to dereference the pointer
  • Current c1(1,45), c2(2,0), c3
  • c3c1c2
  • // is used to perform a user defined operation
  • //relative to the Current class

6
Operator
  • Please note that there are four operators that
    can be overloaded in both unary and binary forms
  • -
  • Five operators cannot be overloaded
  • . . ? sizeof

7
Operator
  • Operators that can be overloaded
  • new new delete delete
  • - /
  • ! lt gt
  • - /
  • ltlt gtgt ltlt gtgt ! lt
  • gt -- , -gt
  • -gt ()

8
  • When an operator is overloaded, it keeps its
    original meanings and gains an additional meaning
    through the overloading process that is relative
    to the class for which it is defined.
  • Most of the operators in C can have only one
    form
Write a Comment
User Comments (0)
About PowerShow.com