Object Oriented Programming Elhanan Borenstein Lecture #5.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Class and Objects.
Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Review of C++ Programming Part II Sheng-Fang Huang.
Object Oriented Programming Elhanan Borenstein Lecture #6.
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Object Oriented Programming Elhanan Borenstein Lecture #4.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Learners Support Publications Classes and Objects.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
More C++ Features True object initialisation
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object Oriented Programming Elhanan Borenstein Lecture #7.
1 CSC241: Object Oriented Programming Lecture No 08.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Learning Objectives Pointers as dada members
Classes (Part 1) Lecture 3
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Object Oriented Programming
Constructor & Destructor
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Constructors and Destructors
Classes and Objects.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Submitted By : Veenu Saini Lecturer (IT)
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Object Oriented Programming Elhanan Borenstein Lecture #5

Agenda Friendship Inner types and enums Namespaces Operator Overloading

Friendship, Inner Types & Namespaces

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

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

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

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(…)

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

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

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

Operators Overloading

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();

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); }

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

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

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

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

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

Operators Overloading The CArray class. Example

Questions?