Download presentation
Presentation is loading. Please wait.
Published byTerence Kelley Modified over 9 years ago
1
1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z Email: Marc.Conrad@luton.ac.uk Rob.Manton@luton.ac.uk z Room: D104
2
2 Module Outline zIntroduction zNon object oriented basics zClasses z Inheritance z Aggregation z Polymorphism z Multifile Development
3
3 Today’s lecture zPolymorphism I ymethod overloading yoperator overloading
4
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
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 2 + 3 (add two integers) than in 1.7 + 3.3 (add two floating point numbers)
6
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
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
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
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
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();
11
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
12
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.
13
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
14 Operator Overloading - Motivation zQuestion: How many function calls are involved in the following statement? a = 2 + 3
15
15 Operator Overloading - Motivation zQuestion: How many function calls are involved in the following statement? a = 2 + 3 zThere are two functions implicitly involved: + and =. zLook at this statement as “assign(a, add(2,3));”
16
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
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
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
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
20 Operator Overloading - Example If we try adding the two objects together at the moment we get the expected error message
21
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
22 Overloading Operators - Example + operator overloaded to accept two BLT objects as arguments.
23
23 Overloading Operators - Example Note: return type is BLT
24
24 Overloading Operators - Example Because overloaded + operator returns a BLT object, this works!
25
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
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
27 Operator Overloading += operator overloaded to accept one extra BLT object as an argument -note void return type
28
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
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
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
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
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
33 Operator Overloading - Interesting Observation zcout << “Hello World\n”; Overloaded << operator
34
34 Operator Overloading - Interesting Observation The << operator is overloaded to take a BLT object as an argument
35
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
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.