Object Oriented Programming Development Polymorphism I z By
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: D 104 1
Module Outline z Introduction z Non object oriented basics z Classes z Inheritance z Aggregation z Polymorphism z Multifile Development 2
Today’s lecture z. Polymorphism I ymethod overloading yoperator overloading 3
The Meaning of the word. z. From the Greek: y. Polus + Morphe = Polumorphos (many ) (shape/form) y. The English word "polymorphe" dates from the 19 th century and was applied to different animal forms arising in the same species. 4
The Meaning of the word. z. In object-oriented computing it means: different forms of data being handled by the same type of process. z. 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 z 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 z In Object Oriented Programming there are three types of polymorphism: a) method overloading, with the special and important case of operator overloading z 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 z Method overriding and z In Object Oriented Programming there run-time polymorphism are three types of polymorphism: are specific to inheritance hierarchies a) method overloading, with the special and object oriented and important case of operator programming overloading z (more about this next b) method overriding week. . ) c) run-time polymorphism 8
Types of Polymorphism z In Object Oriented Programming there z Run-time polymorphism, are three types of polymorphism: also called dynamic a) method overloading, with the special binding, or late binding is and important case of operator often considered as the overloading object oriented feature of b) method overriding. C++. c) run-time polymorphism 9
Method & Function Overloading z. Overloading a function simply means, that a function is not only defined by its name but by its name and parameter types. z. The following functions are different in C++: yint make. Breakfast(int i, int k); yvoid make. Breakfast(Creature who); yfloat make. Breakfast(); 10
Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(int year) { year. Of. Birth = year; } void set. Year. Of. Birth(Creature other) { year. Of. Birth = other. year. Of. Birth; } int get. Year. Of. Birth() { return year. Of. Birth; } }; born 1997 11
Example: The Creature class Creature { private: These two methods int year. Of. Birth; are different. public: void set. Year. Of. Birth(int year) { year. Of. Birth = year; } void set. Year. Of. Birth(Creature other) { year. Of. Birth = other. year. Of. Birth; } int get. Year. Of. Birth() { return year. Of. Birth; born 1997 } 12 };
Example: The Creature class Creature { These two methods private: are different because int year. Of. Birth; they have different public: argument types. void set. Year. Of. Birth(int year) { year. Of. Birth = year; } void set. Year. Of. Birth(Creature other) { year. Of. Birth = other. year. Of. Birth; } int get. Year. Of. Birth() { return year. Of. Birth; born 1997 } 13 };
Operator Overloading Motivation z. Question: How many function calls are involved in the following statement? a=2+3 14
Operator Overloading Motivation z. Question: How many function calls are involved in the following statement? a=2+3 z. There are two functions implicitly involved: + and =. z. Look at this statement as “assign(a, add(2, 3)); ” 15
Operator Overloading z. So, operators as +, -, *, <<, =, etc. can be seen as “functions” as well. That means we can overload operators. z. 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. z may contain bacon (yes/no). z a fraction of a lettuce-leaf. z a number of tomato slices. l, int t); 17
Overloading Operators Example BLT filling 1(true, 0. 5, 2); BLT filling 2(false, 0. 2, 0); class BLT {. . . public: BLT filling 3 = filling 1 + filling 2; 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 filling 1(true, 0. 5, 2); BLT filling 2(false, 0. 2, 0); class BLT { … public: BLT filling 3 = filling 1 + filling 2; 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 z. Operators can also be overloaded as methods, e. g. the operator +=: z class BLT { z // … z BLT operator+=(BLT other) { z bacon =( bacon || other. bacon); z tomatoes += other. tomatoes; z lettuce += other. lettuce; z } z //… 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 filling 1(true, 0. 5, 2); BLT filling 2(false, 0. 2, 0); Operator Overloading … filling 1 += filling 2; z. Operators can also be. . . overloaded as methods, e. g. the operator +=: /* Should give a filling with z class BLT { bacon, 0. 7 lettuce and 2 z // … tomatoes*/ z BLT operator+=(BLT other) { z z z //… bacon =( bacon || other. bacon); tomatoes += other. tomatoes; lettuce += other. lettuce; } 29
Operator Overloading z. Operators can also have other types as parameter: z class BLT { z // … z BLT operator*=(int factor) { z tomatoes *= factor; z lettuce *= factor; z } z //… 30
BLT filling 1(false, 0. 5, 2); … Operator Overloading filling 1 *= 2; . . . z. Operators can also have other as no /* Should give a types filling with parameters: bacon, 1 lettuce and 4 z class BLT { tomatoes z // … */ z BLT operator*=(int factor) { z tomatoes *= factor; z lettuce *= factor; z } z //… 31
Operator Overloading z. The following operators can be overloaded: ynew, delete, +, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, <<, >>=, <<=, ==, !=, <=, >=, &&, ||, ++, --, , , ->*. ->, (), [] z Note that "=" has already a default behaviour. When "overloaded" it will be in fact overridden. 32
Operator Overloading Interesting Observation zcout << “Hello Worldn”; Overloaded << operator 33
Operator Overloading Interesting Observation The << operator is overloaded to take a BLT object as an argument 34
Operator Overloading Interesting Observation z z BLT my. Filling(1, 0. 5, 4); cout << my. Filling << endl; Now we can perform class-specific output using the standard << syntax! 35
Operator Overloading Summary z Operators may be overloaded to work with user defined data types (objects). z The syntax for overloading involves the 'operator' keyword and the operator. z Note: In a good design it is important, that the normal meanings of operators are not distorted (don't subtract with a + operator) 36
- Slides: 36