Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Elhanan Borenstein Lecture #5.

Similar presentations


Presentation on theme: "Object Oriented Programming Elhanan Borenstein Lecture #5."— Presentation transcript:

1 Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #5

2 Agenda Friendship Inner types and enums Namespaces Operator Overloading

3 Friendship, Inner Types & Namespaces

4 Friendship Permissions As we recall, private members are accessible only to member functions of that class. The class can, however, permit certain functions or classes to access its private members using the keyword “friend”. This mechanism should be used wisely as it somewhat violates the concepts of OOP Friend permissions can be given to global functions or classes. Friendship

5 Friendship Permissions A function can be declared within the class as a “friend”. This function will be a global function(!!!) but can access private data members of that class. A friend function cannot be defined as const. WHY? Example: cpoint_friend Friend Global Functions

6 Friendship Permissions A different class can be declared within the class as a “friend”. This class will be able to access (and change) private data members. Example: cline_friend Friend Classes

7 Inner Classes/Enums A class or an enum can be define within another class. When will we use inner classes? Example – link list, tree inner types/class can be defined as either private or public. When will we use private inner types? A public inner class can be used anywhere in the application by using its full name:  ext_class::inner_class An inner class function can also be implemented externally using its full name:  ext_class::inner_class::func(…)

8 Namespaces When merging code from different sources (e.g. libraries) we may run into dual naming problems. We can however define namespaces and refer to different classes using their full names. (somewhat similar to the inner types notion) Every class or global function always belongs to a namespace:  either to a specific namespace (explicitly defined by the programmer)  or…the global namespace. Motivation

9 Namespaces A namespace is define with the following format: namespace { …} To refer to a class/function that belong to a different namespace, we can:  either use its full name ( :: )  or…declare we wish to use this namespace from now on: using namespace ; Example: namespace Usage

10 Namespaces namespaces can be nested. We can use more than one namespace simultaneously. When using two namespaces that have classes/functions with identical names, an ambiguity problem may arise:  A compilation error will occur only when trying to call the problematic function/class.  using the full name will solve the problem. The local scope will have precedence over other namespaces. The global namespace will not have precedence  using ::, will solve the problem. Guidelines

11 Operators Overloading

12 So far we have managed to show analogous behavior between fundamental types (variables) and classes (objects). One of the common operations on variables though, is employing operators. We wish to support the same behavior (when reasonable !!!) for objects. For example: Motivation CPoint p1(2,3), p2(1,6); CPoint p3 = p1+p2; p3.Print();

13 Operators Overloading Most operators cannot be automatically applied to objects though. (which can?) However… we can define (overload) all available operators for classes too. For example: Operator Overloading Example class CPoint { int m_x, m_y; public: CPoint(int ax, int ay):m_x(ax), m_y(ay){} CPoint operator+(const CPoint& p) const; }; CPoint CPoint::operator+(const CPoint& p) const { return CPoint(m_x + p.m_x, m_y + p.m_y); }

14 Operators Overloading Available Operators Arithmetics: +, -, *, /, % ++, -- +=, -=, *=, /=, %= Bitwise Operators: >>, << (shift) &, |, ~, ^ >>=, <<=, &=, |=, ~=, ^= Others: = (assignment) ( ), [ ], *, ->,,, ! (Casting), new, delete Conditions: ==, >=, <=, != &&, || Some operators are frequently overloaded (=, +) while other (&&, ||, comma ) never do.

15 Operators Overloading An operator should be overloaded only when its behavior is obvious. Otherwise, a function should be used. An overloaded operator should work in an analogous manner to the fundamental types. It should also have the same behavior. Efficiency is essential:  Object parameters will pass ByRef  Object parameters that should not be changed will be defined as const  An operator that returns a local object should return it ByVal (preferably as a temp object within the return command)  Operators that returns the activating object or an object that was passed as a parameter should return it ByRef Guidelines

16 Operators Overloading An unary operator operates on one object  the operating object For example : the unary minus A binary operator operates on two objects  the operating object + parameter object For example : the binary minus Binary and Unary Operators

17 Operators Overloading Operators can be overloaded in two manners: 1) As a method (member function) – An activating object is mandatory (parameters are optional)  The assignment operator can be overloaded only as a method. 2) As a global function - Only parameters  We will usually prefer to define the function as a friend function Access to private data members Declaration within the class  In some cases, we must use global function rather than a method. When using a global function, there is always an additional parameter. (the first parameter will substitute for the activating object) Methods (Member Functions) vs. Friend Functions

18 Operators Overloading The default assignment operation performs shallow assignment. When a class implements dynamic allocation and the assignment operator is not overloaded, we can end up with dual pointing. Such a scenario may result with severe problems. We may, however, overload the assignment operator and make sure assignment is performed properly. We can also declare an empty assignment operator in the private section of the class, preventing assignment from taking place. The Assignment Operator

19 Operators Overloading The CArray class. Example

20 Questions?


Download ppt "Object Oriented Programming Elhanan Borenstein Lecture #5."

Similar presentations


Ads by Google