Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

Object Oriented Programming Development - Polymorphism I

By: Marc Conrad & Rob Manton University of Luton Email: Marc.Conrad@luton.ac.uk Rob.Manton@luton.ac.uk Room: D104
1

Module Outline
Introduction Non object oriented basics Classes Inheritance Aggregation Polymorphism Multifile Development

Todays lecture
Polymorphism I
method overloading operator overloading

The Meaning of the word.


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

The Meaning of the word.


In object-oriented computing it means: different forms of data being handled by the same type of process. Example: The operator + has a different meaning in the expression 2 + 3 (add two integers) than in 1.7 + 3.3 (add two floating point numbers)
5

Types of Polymorphism
In 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
6

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

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

c) run-time polymorphism

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

Method & Function Overloading


Overloading a function simply means, that a function is not only defined by its name but by its name and parameter types. The following functions are different in C++:
int makeBreakfast(int i, int k); void makeBreakfast(Creature who); float makeBreakfast();

10

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

Example: The Creature class

born1997

class Creature { private: These two methods int yearOfBirth; are different. 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 { are different private: because they have int yearOfBirth; different argument public: void setYearOfBirth(int year) { types. yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; born1997 } };

Example: The Creature class These two methods

Operator Overloading Motivation


Question: How many function calls are involved in the following statement? a=2+3

14

Operator Overloading Motivation


Question: How many function calls are involved in the following statement? a=2+3 There are two functions implicitly involved: + and =. Look at this statement as assign(a, add(2,3));
15

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

16

Overloading Operators Example


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

l, int t);

17

Overloading Operators Example BLT filling1(true,0.5,2);


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

Overloading Operators Example BLT filling1(true,0.5,2);


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

Operator Overloading Example

If we try adding the two objects together at the moment we get the expected error message

20

Overloading Operators Example // The C++ Syntax


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

Overloading Operators Example

+ operator overloaded to accept two BLT objects as arguments.


22

Overloading Operators Example


Note: return type is BLT

23

Overloading Operators Example

Because overloaded + operator returns a BLT object, this works!


24

Overloading Operators Example


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

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

Operator Overloading

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

27

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

28

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

29

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

BLT filling1(false,0.5,2); Operator Overloading filling1 *= 2; ... Operators can alsoShould other types with no /* have give a filling as parameters: bacon, 1 lettuce and 4 class BLT { tomatoes // */
BLT operator*=(int factor) { tomatoes *= factor; lettuce *= factor; } //

31

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

Operator Overloading Interesting Observation


cout << Hello World\n;

Overloaded << operator


33

Operator Overloading Interesting Observation

The << operator is overloaded to take a BLT object as an argument


34

Operator Overloading Interesting Observation


BLT myFilling(1,0.5,4); cout << myFilling << endl;

Now we can perform class-specific output using the standard << syntax!
35

Operator Overloading Summary


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

You might also like