Object Oriented Programming Development Polymorphism z By Marc
Object Oriented Programming Development - Polymorphism 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
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. 3
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) 4
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 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 z Method overloading can b) method overriding also be applied in nonobject oriented contexts c) run-time polymorphism and refers boths to functions and methods. 6
Types of Polymorphism z In Object Oriented Programming there z Method overriding and run are three types of-time polymorphism: polymorphism is specific to inheritance a) method overloading, with the special hierarchies and object and important case of operator oriented programming overloading b) method overriding c) run-time polymorphism 7
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 8
Method & Function Overloading z. Overloading a function simply means, that a function is not only defined 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(); 9
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 10
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 } 11 };
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 } 12 };
Operator Overloading Motivation z. Question: How many function calls are involved in the following statement? a=2+3 13
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)); ” 14
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. 15
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); 16
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) }; 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 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 }; 18
Overloading Operators Example // The C++ Syntax BLT operator+(BLT y, BLT z) { class BLT { bool b = y. bacon || z. bacon; public: float l = y. lettuce + z. lettuce; bool bacon; int t = y. tomatoes = z. tomatoes; float lettuce; BLT result(b, l, t); int tomatoes; return result; // Constructor: } BLT(bool b, float l, int t); // … (more code) }; 19
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 ||= other. bacon; z tomatoes += other. tomatoes; z lettuce += other. lettuce; z } z //… 20
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 ||= other. bacon; tomatoes += other. tomatoes; lettuce += other. lettuce; } 21
Operator Overloading z. Operators can also have other types as parameter: z class BLT { z // … z BLT operator*=(int factor) { z tomatoes *= 2; z lettuce *= 2; z } z //… 22
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 //… 23
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. 24
Operator Overloading Interesting Observation zcout << “Hello Worldn”; Overloaded << operator 25
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. 1. Note: In a good design it is important, that the normal meanings of operators are not distorted (don't subtract with a + operator) 26
Polymorphic Pointers z In C++ a pointer of a parent class is allowed to point to an object of the child class. E. g. class Vehicle { //. . . }; class Car : public Vehicle { //. . . }; //. . . Vehicle vp = new Car(); + + C Valid ! x a t syn 27
Overriding Methods z Methods in the parent class can be redifined in the child class. z class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); + + C Valid ! x a t syn 28
Overriding Methods z Methods in the parent class can be redifined in the child class. BUT: z class Vehicle { void move(int i); z Which of these }; two move() class Car : public Vehicle { void move(int i); methods will be }; called? //. . . Vehicle vp = new Car(); vp->move(100); 29
Overriding Methods z Methods in the parent class can be redifined in the child class. z class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); static typing 30
Overriding Methods z Methods in the parent class can be redifined in the child class. z class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); dynamic binding? 31
Overriding Methods z Methods in the parent class can be redifined in the child class. static typing! z class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); z In C++, static typing is the default behaviour. As vp is of type pointer to a Vehicle, the method of the Vehicle is called. 32
Overriding Methods The keyword virtual z Methods in the parent class can be redifined in the child class. dynamic binding! z class Vehicle { virtual void move(int i); }; class Car : public Vehicle { virtual void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); The keyword virtual allows the use of dynamic binding. As vp points to a Car object the method of the Car is called 33
Abstract Methods & Classes z Abstract methods are methods without any implementation (pure virtual methods). z class Vehicle { virtual void move(int i) = 0; }; class Car : public Vehicle { virtual void move(int i); }; //. . . Vehicle vp = new Car(); vp->move(100); Syntax for declaring abstract methods. Note that Vehicle objects cannot be instantiated (but Car objects). 34
Static typing & Dynamic binding z Static typing means that the legality of a member function invocation is checked at the earliest possible moment: by the compiler at compile time. The compiler uses the static type of the pointer to determine whether the member function invocation is legal. z Dynamic binding means that the address of the code in a member function invocation is determined at the last possible moment: based on the dynamic type of the object at run time. It is called "dynamic binding" because the binding to the code that actually gets called is accomplished dynamically (at run time). 35
Important stuff to remember z. Overriding methods z. Polymorphic pointers z. Static binding z. Dynamic binding - the virtual keyword z. Abstract methods 36
- Slides: 36