100 likes | 110 Views
Explore function/operator overloading in C++, including friend functions and UML diagrams. Learn how functions can be overloaded based on arguments, and how operators can be treated as functions. Understand the concept of friend functions, breaking encapsulation for specific access. Dive into Unified Modelling Language (UML) with examples of use-case, class, and sequence diagrams using Rational Rose.
E N D
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 – 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
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.
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 >>
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.
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?
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!
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.
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