C Lecture 4 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

C Lecture 4

Description:

Function overloading constructor functions. actual function called determined by the number and type ... Friendship should be used thoughtfully and sparingly ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 11
Provided by: alang66
Category:

less

Transcript and Presenter's Notes

Title: C Lecture 4


1
C Lecture 4
  • Function/operator overloading
  • Friend functions C example
  • UML
  • UML diagrams
  • Use-case diagram
  • Class diagram
  • sequence diagram
  • Unit Assessment

2
Function/operator overloading
  • Function overloading constructor functions
  • actual function called determined by the number
    and type of the arguments
  • Operators
  • Unary e.g. -, !, , , --
  • Binary e.g. ,-,,/,ltlt,gtgt etc.
  • Operators are treated as functions by C
  • Therefore the operators can be overloaded

3
Operator overloading
  • It is therefore possible to write operator
    functions for the operators.
  • The functions will be called operator_at_, where _at_
    is the actual operator
  • There will possibly be many operator functions
    all with the same name. They must differ in the
    number and type of their arguments.
  • This is operator overloading.

4
Operators as functions
  • Consider an object obj of class CAny
  • CAny obj
  • The normal way of invoking a member function of
    the class CAny is -
  • obj.func()
  • Now consider a binary operator e.g. objx _at_
    objy
  • where objx and objy are instances of some
    class and _at_ is a binary operator such as ,-,,/,
    ltlt or gtgt

5
Operators as functions
  • objx _at_ objy is interpreted by the C compiler in
    on of two ways
  • objx.operator_at_(objy)
  • i.e. the compiler looks for a a member function
    called operator_at_ in the class of which objx is an
    instance.
  • if such a member function cannot be found in the
    class then the following is tried
  • operator_at_(objx, objy)
  • i.e. the compiler looks for an ordinary function
    called operator_at_ that takes two arguments with
    the appropriate data types.

6
Operator overloading
  • We have a choice over which method to implement
  • either as a member function of a class
  • or as an ordinary function
  • objx _at_ objy is treated as objx.operator(objy)
  • If the operator is a member function then this
    implies that the object to the left of the
    operator MUST be an instance of the class
    containing the operator function.
  • What if the object on the left is not an instance
    of the class containing the operator function?

7
Friend Functions
  • We might therefore implement the operator as an
    ordinary function-
  • operator_at_(objx, objy)
  • Since this is NOT a member function how can it
    access the private data of the objects
  • the objx, objy class could have appropriate
    accessor and mutator functions. This would be
    less efficient than if the operator function
    could access the private data directly.
  • C added Friends!

8
Friend Functions
  • Friends are functions or whole classes which are
    granted access to the private data of a
    class.This breaks the encapsulation
  • A friend is declared within the class which is
    allowing friendship.
  • A function cannot make itself a friend of a
    class.
  • Friendship should be used thoughtfully and
    sparingly
  • A common use of friend functions is to allow the
    use of ltlt and gtgt with cout and cin for output and
    input of user defined types.

9
Unified Modelling Language - UML
  • UML is a modelling tool to capture and represent
    in the form of diagrams and text the various
    stages of the software development process
  • There are 8 different diagram types. We shall
    only consider 3
  • Use-case capture user interaction with the
    system
  • Class show relationship between objects and
    classes
  • Sequence show the interaction of objects.
  • Rational Rose UML development tool

10
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com