CompE260 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CompE260

Description:

C allows the programmer to define the meaning of an operator for a class. This is called overloading the operator. ... this- varA = arg1; ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 10
Provided by: brettb2
Category:
Tags: compe260 | vara

less

Transcript and Presenter's Notes

Title: CompE260


1
Lecture 14
  • CompE260

2
Overloading
  • Overloading allows the programmer to use the same
    name or symbol for different purposes.
  • Things that can be overloaded included
  • Operators (symbols) ,, -, , /, gt, lt, lt, gt,
    , , , , ,
  • Method (function) names

3
Operator Overloading
  • C allows the programmer to define the meaning
    of an operator for a class. This is called
    overloading the operator.
  • Example, if I define a Matrix class, then I will
    probably want to define Matrix operations such as
    addition, subtraction, etc. Operator overloading
    allows you to use a symbol such as as the name
    of a function.

4
Operator Overloading II
  • The compiler detects whether an operator is
    overloaded, the by looking for a function of the
    form
  • return type operator symbol(ptype p1 )
  • CplxNum operator(CplxNum A)
  • Overloaded Operators may be either members of a
    class or not members of a class.

5
Overloaded Operators that are Members of a class
  • Prototype
  • CplxNum CplxNumoperator(CplxNum Num)
  • CplxNum CplxNumoperator(int real)
  • CplxNum CplxNumoperator-(CplxNum Num)
  • Usage
  • CplxNum A, B, C
  • Int real
  • C A B is understood to mean C
    A.operator(B)
  • C A real is understood to mean C
    A.operator(real)
  • Where operator is the name of a function that is
    a member of the class CplxNum.

6
Overloaded Operators that are Not Members of a
class
  • Prototype
  • CplxNum operator(CplxNum Num1, CplxNum Num2)
  • Note 2 arguments NOT 1
  • Usage
  • CplxNum A, B, C
  • C A B is understood to mean C operator(A,
    B)
  • Where operator is the name of a function that is
    a NOT a member of the class CplxNum.
  • Since the function operator is not a member of
    the class CplxNum, it does not have access to the
    private members of the class.

7
Overloading of Class Methods
  • A class is permitted to have multiple functions
    (methods) that share the same name. If this
    occurs, such methods are called overloaded.
  • The compiler determines which function to
    actually call by looking at the combination of
    the
  • Class Name
  • Method Name
  • Method Parameter List or Signature

8
Assignment Operator
  • When you use the assignment operator () with a
    class, the compiler will copy all the data
    members from the object on the right side of the
    operator to the object on the left side of the
    operator.
  • After the assignment operation, the members of
    each object are the same!!
  • The operator will NOT follow any references for
    dynamically assigned memory.
  • You need to overload the operator to perform
    this action.

9
this
  • When a class method is called
  • CplxNum A, B
  • A.SetMagnitude(1)
  • Calls the class method SetMagnitude with a
    parameter of 1 for the object A.
  • B.SetMagnitude(1)
  • Calls the class method SetMagnitude with a
    parameter of 1 for the object B.
  • The compiler passes the method a pointer to the
    object for which the method was called.
  • The SetMagnitude method has a pointer to the
    object A.
  • The pointer is passed in a predefined location
    called this.
  • The programmer can use the this pointer to access
    the objects data attributes or other functions by
    reference rather than value.
  • this-gtvarA arg1
Write a Comment
User Comments (0)
About PowerShow.com