1 CSC241: Object Oriented Programming Lecture No 07.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
1 Overloading Operators COSC 1567 C++ Programming Lecture 7.
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Classes: A Deeper Look Systems Programming.
Data types and variables
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 2 Data Types, Declarations, and Displays
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Objectives You should be able to describe: Data Types
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
1 CSC241: Object Oriented Programming Lecture No 06.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
1 CSC241: Object Oriented Programming Lecture No 02.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Recap……Last Time [Variables, Data Types and Constants]
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Programming Techniques Classes II Important Class Features Spring 2009.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CSC241: Object Oriented Programming Lecture No 08.
Programming Fundamentals Enumerations and Functions.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Java Primer 1: Types, Classes and Operators
Operator Overloading BCA Sem III K.I.R.A.S.
Introduction to C++ Programming
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
10.2 const (Constant) Objects and const Member Functions
Operator Overloading.
Operator Overloading.
Operator Overloading; String and Array Objects
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 07

2 Previous Lecture static class member – Data member – Member function Information hiding Distance.h Distance.cpp Distance.a Distance.h main.cpp Developer sideClient side

3 Today’s Lecture Abstract data type (ADT) Container classes Proxy classes Operator overloading

4 Abstract data type Consider built-in type int Most people associate it with an integer in mathematics Actually, int is an abstract representation of an integer ints are fixed in size (32-bits) if result is out of range – “Overflow” error occurs – "quietly" produce an incorrect result Mathematical integers do not have this problem

5 Cont. Built-in data types are only approximations or imperfect models of real-world concepts Types like int, double, char and others are all examples of abstract data types An abstract data types captures two concepts – Data representation – Operations that can be perform on that data For example – int contain integers values – Operations: add, subtract, multiply, division

6 ADT in C++ In C++, the programmer uses classes to implement abstract data types and their services For example, to implement a array ADT – subscript range checking – an arbitrary range of subscripts instead of having to start with 0 – array assignment – array comparison – array input/output

7 Container classes – real world In real life, we use containers all the time For example – pages in your book come inside a cover or binding – you might store any number of items in containers in your garage Without container it is inconvenient to work with many objects – Imagine trying to read a book that didn’t have any sort of binding – Eat cereal that didn’t come in a box without using a bowl

8 Container classes – C++ Container classes designed to hold collections of instance of other classes Arrays, stacks, queues, trees and linked lists are examples of container classes

9 Two important principles 1.Separating interfaces from implementation 2.Hiding implementation from user/client We achieve this by defining a class in header file and implementation of class function in a separate cpp source file

10 Proxy class Proxy class allows to hide the private data and other functions of a class from clients Providing client with proxy class enables it to use your class services with know implementation detail of your class Implementation class Interface classClient class Go to program

11 Static library Contains class definition of Implementation class Implementation.h Contains class definition of interface class interface.h Contains definition of member function of interface class interface.cpp Library.a Contains class definition of interface class interface.h Go to program

12 Operator overloading Services are obtained from objects by sending messages i.e. function call This function call is cumbersome for certain kinds of classes e.g. mathematical classes For example, statements like – d3.addobjects(d1, d2); or – d3 = d1.addobjects(d2); can be changed to the much more readable – d3 = d1 + d2;

13 Cont. Operator overloading refers to giving the normal C++ operators, such as +, *, <=, and etc., additional meanings when they are applied to user-defined data types Operator overloading gives you the opportunity to redefine the C++ language – By using classes to create new kinds of variables, – operator overloading to create new definitions for operators

14 Examples of c++ overloaded operator Addition + and subtraction – operator  +, – operators perform differently, depending on their context in integer arithmetic, floating-point arithmetic and pointer arithmetic. Stream insertion ( >) operator (cout >)  << is used both as the stream insertion operator and as the bitwise left-shift operator  >> is used both as the stream extraction operator and as the bitwise right-shift operator  > are overloaded in the C++ Standard Library std

15 Fundamentals of Operator Overloading Jobs performed by overloaded operators can also be performed by explicit function calls Programmers can use operators with user- defined types C++ does not allow new operators to be created It does allow most existing operators to be overloaded so that, when these operators are used with objects This is a powerful capability

16 Cont. An operator is overloaded by writing a non-static member function definition Function name becomes the keyword operator followed by the symbol for the operator being overloaded – E.g. operator + () { …. } Function name operator+ would be used to overload the addition operator (+) Assignment operator (=) may be used with every class to perform member wise assignment of the data members – d1 = d2;

17 Restrictions on Operator Overloading Operators that can be overloaded Operators that cannot be overloaded Attempting to overload a non overloadable operator is a syntax error

18 Precedence, Associativity and No. of Operands Precedence means which operator to solve first (+, -, *, /, = ) The precedence of an operator cannot be changed by overloading The associativity of an operator cannot be changed by overloading Overloaded unary operators (++, --) remain unary operators overloaded binary operators remain binary operators

19 Creating New Operators It is not possible to create new operators only existing operators can be overloaded – E.g. ** can be used for exponential in some programming languages – ** is not in the list od existing operator so it cannot be overloaded Attempting to create new operators via operator overloading is a syntax error

20 Operators for Fundamental Types The meaning of how an operator works on fundamental types cannot be changed by operator overloading For example, programmer cannot change the meaning of how + adds two integers Operator overloading works only with – objects of user-defined types or – a mixture of an object of a user-defined type and an object of a fundamental type

21 Examples c1++; or c1 --; – Overloading ++ and -- unary operator dist3 = dist1 + dist2; – Overloading assignment and addition binary operator – It does not mean that += is also overloaded for Distance objects dist1+= dist2; or dist1 - =dist2

22 Overloading Unary Operators Examples of unary operators are the increment and decrement operators ++ and --, and the unary minus, as in -33 Counter class example – To keep track of a count. – Objects of that class were increment the count value by calling a member function: c1.inc_count(); But it would be more readable if we could have used the increment operator ++ ++c1;

23 Example class Counter{ private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } void operator ++ (){ //increment (prefix) ++count; } }; Program Output main(){ Counter c1, c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count(); ++c1; ++c2; ++c2; cout << “\nc1=” << c1.get_count(); cout << “\nc2=” << c2.get_count() << endl; } c1=0 c2=0 c2=2 c1=1

24 The operator Keyword The keyword operator is used to overload the ++ operator void operator ++ () The return type (void in this case) comes first, followed by the keyword operator, followed by the operator itself (++), and finally the argument list enclosed in parentheses This declarator syntax tells the compiler to call this member function whenever the ++ operator is encountered

25 Cont. The compiler can distinguish between overloaded functions in the following ways – data types of arguments and – the number of their arguments The only way a compiler can distinguish between overloaded operators is by looking at the data type of their operands. – If the operand is a basic type such as an int then the compiler will use its built-in routine to increment an int

26 Operator Arguments the ++ operator is applied to a specific object, as in the expression ++c1 What does this operator increment? It increments the count data in the object of which it is a member Since member functions can always access the particular object for which they’ve been invoked, this operator requires no arguments

27 Operator Return Values The operator++() function has a defect. You will discover it if you use a statement like this in main(): c1 = ++c2; The compiler will complain. Why Because ++ operator is defined to have a return type of void in the operator++() function, while in the assignment statement it is being asked to return a variable of type Counter The normal ++ operator, applied to basic data types such as int, would not have this problem int x,y; y=10; x = y ++;

28