13'5 Operator Overloading - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

13'5 Operator Overloading

Description:

Note that the parameter, Right, was declared as a reference for efficiency ... Also note that the parameter was declared constant so the function will not ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 26
Provided by: christophe52
Category:

less

Transcript and Presenter's Notes

Title: 13'5 Operator Overloading


1
13.5 Operator Overloading
  • Operators such as , , and others can be
    redefined when used with objects of a class
  • The name of the function for the overloaded
    operator is operator followed by the operator
    symbol, e.g.,
  • operator to overload the operator, and
  • operator to overload the operator
  • Prototype for the overloaded operator goes in the
    declaration of the class that is overloading it
  • Overloaded operator function definition goes with
    other member functions

2
13.5 Operator Overloading
  • Although copy constructors solve the
    initialization problems with objects containing
    pointer members, they do not work with simple
    assignment statements.
  • Person person1, person2
  • Person2 person1
  • In order to change the way the assignment
    operator works, it must be overloaded
  • C allows you to have special member functions
    called operator functions which permit you to
    redefine an existing operators behavior when
    used with a class object.

3
Operator Overloading
  • Prototype
  • void operator(const SomeClass rval)
  • Operator is called via object on left side

parameter for object on right side of operator
return type
function name
4
Invoking an Overloaded Operator
  • Operator can be invoked as a member function
  • object1.operator(object2)
  • It can also be used in more conventional manner
  • object1 object2
  • Note that the parameter, Right, was declared as a
    reference for efficiency purposes the reference
    prevents the compiler form making a copy of the
    object being passed into the function.
  • Also note that the parameter was declared
    constant so the function will not accidentally
    change the contents of the argument.

5
Program 1
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • class Person
  • private
  • char name
  • int age
  • public
  • Person(char , int) // 2 argumument
    constructor
  • Person(const Person ) // copy constructor
  • // overloaded assignment operator
  • void operator(const Person )
  • void display()
  • Person() // destructor

6
Program 1
  • PersonPerson() delete name
  • PersonPerson(const Person obj)
  • name new charstrlen(obj.name) 1
  • strcpy(name, obj.name)
  • age obj.age
  • void Personoperator (const Person right)
  • delete name
  • name new charstrlen(right.name) 1
  • strcpy(name, right.name)
  • age right.age
  • void Persondisplay()
  • cout ltlt "Name " ltlt name ltlt "\tAge " ltlt age ltlt
    endl
  • PersonPerson(char n,int a)
  • name new charstrlen(n) 1
  • strcpy(name, n)

7
Program 1
  • int main()
  • Person object1("David", 21)
  • Person object2 object1 // will call copy
    constructor
  • Person object3("Mark", 19)
  • cout ltlt "Object 1" ltlt endl
  • object1.display()
  • cout ltlt "Object 2" ltlt endl
  • object2.display()
  • cout ltlt "Object 3" ltlt endl
  • object3.display()
  • object3 object2 // will call overloaded
    assignment operator
  • cout ltlt "Object 3 After assignment " ltlt endl
  • object3.display()
  • return 0

8
Program 1 Output
  • Object 1
  • Name David Age 21
  • Object 2
  • Name David Age 21
  • Object 3
  • Name Mark Age 19
  • Object 3 After assignment
  • Name David Age 21

9
The Operators Return Value
  • If the operator function returns a void, as in
    the above example, then multiple assignment
    statements wont work.
  • To enable a statement such as
  • person3 person2 person1
  • You must have the following prototype
  • Person operator(const Person right)

10
The this Pointer
  • this is a special built in pointer that is
    available in any member function. this contains
    the address of the object that called the member
    function.
  • The this pointer is passed as a hidden argument
    to all non-static member functions.

11
Updated overloaded assignment operator
  • Person Personoperator (const Person right)
  • delete name
  • name new charstrlen(right.name) 1
  • strcpy(name, right.name)
  • age right.age
  • return this
  • Another problem
  • person1 person1

12
Solution Final implementation
  • Person Personoperator (const Person right)
  • if(this right)
  • return this
  • delete name
  • name new charstrlen(right.name) 1
  • strcpy(name, right.name)
  • age right.age
  • return this

13
Program 2 Class having private pointer variable
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • class Overloading
  • private
  • int num
  • public
  • Overloading()
  • Overloading(int )
  • Overloading(const Overloading )
  • Overloading operator(const Overloading )
  • void Display(int)
  • Overloading operator(const Overloading )
  • Overloading()

14
Program 2
  • OverloadingOverloading()
  • cout ltlt "Inside Default Constructor" ltlt endl
  • num new int
  • OverloadingOverloading(int value)
  • cout ltlt "Inside One Argument Constructor" ltlt
    endl
  • num new int
  • num value
  • OverloadingOverloading(const Overloading
    right)
  • cout ltlt "Inside Copy Constructor" ltlt endl
  • num new int
  • num (right.num)

15
Program 2
  • Overloading Overloadingoperator(const
    Overloading right)
  • cout ltlt "Inside Overloaded Assignment Operator"
    ltlt endl
  • if(this right)
  • return this
  • delete num
  • num new int
  • num (right.num)
  • return this
  • Overloading Overloadingoperator(const
    Overloading right)
  • cout ltlt "Inside Overloaded addition operator" ltlt
    endl
  • Overloading temp
  • (temp.num) num (right.num)
  • return temp

16
Program 2
  • void OverloadingDisplay(int objNum)
  • cout ltlt "Object " ltlt objNum ltlt " num " ltlt num
    ltlt endl
  • OverloadingOverloading()
  • delete num
  • int main()
  • Overloading object1
  • Overloading object2(10)
  • object1 object2
  • Overloading object3 object1
  • object1 object2 object3
  • object1.Display(1)
  • object2.Display(2)

17
Program 2 Output
  • Inside Default Constructor
  • Inside One Argument Constructor
  • Inside Overloaded Assignment Operator
  • Inside Copy Constructor
  • Inside Overloaded addition operator
  • Inside Default Constructor
  • Inside Copy Constructor
  • Inside Overloaded Assignment Operator
  • Object 1 num 20
  • Object 2 num 10
  • Object 3 num 10

18
Program 3 Class having no private pointer
variable
  • include ltiostreamgt
  • using namespace std
  • class Overloading
  • private
  • int num
  • public
  • Overloading()
  • Overloading(int )
  • Overloading(const Overloading )
  • Overloading operator(const Overloading )
  • void Display(int)
  • Overloading operator(const Overloading )

19
Program 3
  • OverloadingOverloading()
  • cout ltlt "Inside Default Constructor" ltlt endl
  • num 0
  • OverloadingOverloading(int value)
  • cout ltlt "Inside One Argument Constructor" ltlt
    endl
  • num value
  • OverloadingOverloading(const Overloading
    right)
  • cout ltlt "Inside Copy Constructor" ltlt endl
  • num right.num

20
Program 3
  • Overloading Overloadingoperator(const
    Overloading right)
  • cout ltlt "Inside Overloaded Assignment Operator"
    ltlt endl
  • if(this right)
  • return this
  • num right.num
  • return this
  • Overloading Overloadingoperator(const
    Overloading right)
  • cout ltlt "Inside Overloaded addition operator" ltlt
    endl
  • Overloading temp
  • temp.num num right.num
  • return temp
  • void OverloadingDisplay(int objNum)

21
Program 3
  • int main()
  • Overloading object1
  • Overloading object2(10)
  • object1 object2
  • Overloading object3 object1
  • object1 object2 object3
  • object1.Display(1)
  • object2.Display(2)
  • object3.Display(3)
  • return 0

22
Program 3 Output
  • Inside Default Constructor
  • Inside One Argument Constructor
  • Inside Overloaded Assignment Operator
  • Inside Copy Constructor
  • Inside Overloaded addition operator
  • Inside Default Constructor
  • Inside Copy Constructor
  • Inside Overloaded Assignment Operator
  • Object 1 num 20
  • Object 2 num 10
  • Object 3 num 10

23
Notes on Overloaded Operators
  • Can change meaning of an operator
  • Cannot change the number of operands of the
    operator
  • Only certain operators can be overloaded. Cannot
    overload the following operators
  • ? . . sizeof

24
13.7 Object Composition
  • Object composition a class is a member of a
    class
  • Supports the modeling of has a relationship
    between classes enclosing class has a
    enclosed class
  • Same notation as for structures within structures

25
13.7 Object Composition
  • class Customer
  • private
  • string name
  • string address
  • Account savings
  • Account checking
  • The relationship between the different objects
    that make up this class can be described as
    follows
  • the Customer has a name
  • the Customer has an address
  • the Customer has a savings account
  • the Customer has a checking account
Write a Comment
User Comments (0)
About PowerShow.com