1 / 10

C++ Lecture 7

C++ Lecture 7.  Function/operator overloading  Friend functions – C++ example  UML – UML diagrams  Use-case diagram  Class diagram  sequence diagram  Unit Assessment. Function/operator overloading.  Function overloading – constructor functions

Download Presentation

C++ Lecture 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Lecture 7  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. +,-,*,/,<<,>> 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@, where @ 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 @ objy where objx and objy are instances of some class and @ is a binary operator such as +,-,*,/, << or >>

  5. Operators as functions objx @ objy is interpreted by the C++ compiler in on of two ways  objx.operator@(objy) – i.e. the compiler looks for a a member function called operator@ 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@(objx, objy) – i.e. the compiler looks for an ordinary function called operator@ 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 @ 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@(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 << and >> 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

More Related