1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Object Oriented Programming Development - Week 5 z By: Marc Conrad University of Luton z z Room: D104.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
Chapter 12: Adding Functionality to Your Classes.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Object Oriented Programming Development
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Object Oriented Programming Development - Multi File Development z By: Marc Conrad & Rob Manton University of Luton z
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Chapter -6 Polymorphism
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Overview of C++ Polymorphism
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
1 CSC241: Object Oriented Programming Lecture No 08.
Exception Handling How to handle the runtime errors.
Polymorphism Lecture - 9.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Object-Oriented Programming (OOP) Lecture No. 16
Overloading C++ supports the concept of overloading Two main types
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Review What is an object? What is a class?
Constructor & Destructor
Pointers and Pointer-Based Strings
Object-Oriented Programming (OOP) Lecture No. 16
Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC 143 Inheritance.
CISC/CMPE320 - Prof. McLeod
Variables, Data Types & Math
COP 3330 Object-oriented Programming in C++
Overview of C++ Polymorphism
Inheritance and Polymorphism
Pointers and Pointer-Based Strings
CIS 199 Final Review.
Java Programming Language
Operator Overloading; String and Array Objects
C++ Programming CLASS This pointer Static Class Friend Class
Templates CMSC 202, Version 4/02.
Computer Science II for Majors
Presentation transcript:

1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z z Room: D104

2 Module Outline zIntroduction zNon object oriented basics zClasses z Inheritance z Aggregation z Polymorphism z Multifile Development

3 Today’s lecture zPolymorphism I ymethod overloading yoperator overloading

4 The Meaning of the word. zFrom the Greek: yPolus + Morphe = Polumorphos (many ) (shape/form) yThe English word "polymorphe" dates from the 19th century and was applied to different animal forms arising in the the same species.

5 The Meaning of the word. zIn object-oriented computing it means: different forms of data being handled by the same type of process. zExample: The operator + has a different meaning in the expression (add two integers) than in (add two floating point numbers)

6 Types of Polymorphism zIn Object Oriented Programming there are three types of polymorphism: a)method overloading, with the special and important case of operator overloading b)method overriding c)run-time polymorphism

7 Types of Polymorphism zIn Object Oriented Programming there are three types of polymorphism: a)method overloading, with the special and important case of operator overloading b)method overriding c)run-time polymorphism z Method overloading can also be applied in non- object oriented contexts and refers both to functions and methods.

8 Types of Polymorphism zIn Object Oriented Programming there are three types of polymorphism: a)method overloading, with the special and important case of operator overloading b)method overriding c)run-time polymorphism z Method overriding and run-time polymorphism are specific to inheritance hierarchies and object oriented programming z (more about this next week..)

9 Types of Polymorphism zIn Object Oriented Programming there are three types of polymorphism: a)method overloading, with the special and important case of operator overloading b)method overriding c)run-time polymorphism z Run-time polymorphism, also called dynamic binding, or late binding is often considered as the object oriented feature of C++.

10 Method & Function Overloading zOverloading a function simply means, that a function is not only defined by its name but by its name and parameter types. zThe following functions are different in C++: yint makeBreakfast(int i, int k); yvoid makeBreakfast(Creature who); yfloat makeBreakfast();

Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; born1997

Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; born1997 These two methods are different.

Example: The Creature class class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; born1997 These two methods are different because they have different argument types.

14 Operator Overloading - Motivation zQuestion: How many function calls are involved in the following statement? a = 2 + 3

15 Operator Overloading - Motivation zQuestion: How many function calls are involved in the following statement? a = zThere are two functions implicitly involved: + and =. zLook at this statement as “assign(a, add(2,3));”

16 Operator Overloading zSo, operators as +, -, *, <<, =, etc. can be seen as “functions” as well. That means we can overload operators. zThe C++ syntax uses “function names” prefixed with “operator” for overloading operators.

17 Overloading Operators - Example class BLT { public: bool bacon; float lettuce; int tomatoes; // Constructor: BLT(bool b, float l, int t); // … (more code) }; A Sandwich filling. z may contain bacon (yes/no). z a fraction of a lettuce-leaf. z a number of tomato slices.

18 Overloading Operators - Example class BLT { public: bool bacon; float lettuce; int tomatoes; // Constructor: BLT(bool b, float l, int t); // … (more code) }; BLT filling1(true,0.5,2); BLT filling2(false,0.2,0);... BLT filling3 = filling1 + filling2;... /* Should give a filling with bacon, 0.7 lettuce and 2 tomatoes*/

19 Overloading Operators - Example class BLT { public: bool bacon; float lettuce; int tomatoes; // Constructor: BLT(bool b, float l, int t); // … (more code) }; BLT filling1(true,0.5,2); BLT filling2(false,0.2,0); … BLT filling3 = filling1 + filling2;... /* Should give a filling with 3 bacon slices, 0.7 lettuce and 2 tomatoes */ This is the operator we want to overload

20 Operator Overloading - Example If we try adding the two objects together at the moment we get the expected error message

21 Overloading Operators - Example class BLT { public: bool bacon; float lettuce; int tomatoes; // Constructor: BLT(bool b, float l, int t); // … (more code) }; // The C++ Syntax BLT operator+(BLT x, BLT y) { bool b =x.bacon || y.bacon; float l = x.lettuce + y.lettuce; int t = x.tomatoes = y.tomatoes; BLT result(b,l,t); return result; }

22 Overloading Operators - Example + operator overloaded to accept two BLT objects as arguments.

23 Overloading Operators - Example Note: return type is BLT

24 Overloading Operators - Example Because overloaded + operator returns a BLT object, this works!

25 Overloading Operators - Example void operator+=(BLT &x, BLT y) { bool bacon =( x.get_bacon() float lettuce =x.get_lettuce() + int toms=x.get_tomato_slices()+ x.set_bacon(bacon); x.set_lettuce(lettuce); x.set_tomato_slices(toms); } Where an operator like += actually needs to change the first operand, we need to use the & (call by reference) syntax

26 Operator Overloading zOperators can also be overloaded as methods, e.g. the operator +=: zclass BLT { z // … z BLT operator+=(BLT other) { z bacon =( bacon || other.bacon); z tomatoes += other.tomatoes; z lettuce += other.lettuce; z } z//…

27 Operator Overloading += operator overloaded to accept one extra BLT object as an argument -note void return type

28 Operator Overloading The const keyword indicates to the compiler that you are not going to change the other BLT object in any way

29 Operator Overloading zOperators can also be overloaded as methods, e.g. the operator +=: zclass BLT { z // … z BLT operator+=(BLT other) { z bacon =( bacon || other.bacon); z tomatoes += other.tomatoes; z lettuce += other.lettuce; z } z//… BLT filling1(true,0.5,2); BLT filling2(false,0.2,0); … filling1 += filling2;... /* Should give a filling with bacon, 0.7 lettuce and 2 tomatoes*/

30 Operator Overloading zOperators can also have other types as parameter: zclass BLT { z // … z BLT operator*=(int factor) { z tomatoes *= factor; z lettuce *= factor; z } z//…

31 Operator Overloading zOperators can also have other types as parameters: zclass BLT { z // … z BLT operator*=(int factor) { z tomatoes *= factor; z lettuce *= factor; z } z//… BLT filling1(false,0.5,2); … filling1 *= 2;... /* Should give a filling with no bacon, 1 lettuce and 4 tomatoes */

32 Operator Overloading zThe following operators can be overloaded: ynew, delete, +, -, *, /, %, ^, &, |, ~, !, =,, +=, -=, *=, /=, %=, ^=, &=, |=, >, >>=, =, &&, ||, ++, --,,, ->*. ->, (), [] zNote that "=" has already a default behaviour. When "overloaded" it will be in fact overridden.

33 Operator Overloading - Interesting Observation zcout << “Hello World\n”; Overloaded << operator

34 Operator Overloading - Interesting Observation The << operator is overloaded to take a BLT object as an argument

35 Operator Overloading - Interesting Observation zBLT myFilling(1,0.5,4); zcout << myFilling << endl; Now we can perform class-specific output using the standard << syntax!

36 Operator Overloading - Summary zOperators may be overloaded to work with user defined data types (objects). zThe syntax for overloading involves the 'operator' keyword and the operator. zNote: In a good design it is important, that the normal meanings of operators are not distorted (don't subtract with a + operator)