By: Marc Conrad & Rob Manton University of Luton Email: Marc - Conrad@luton - Ac.uk Rob - Manton@luton - Ac.uk Room: D104
By: Marc Conrad & Rob Manton University of Luton Email: Marc - Conrad@luton - Ac.uk Rob - Manton@luton - Ac.uk Room: D104
By: Marc Conrad & Rob Manton University of Luton Email: [email protected] [email protected] Room: D104
1
Module Outline
Introduction Non object oriented basics Classes
(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.
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
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 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
In Object Oriented Programming and Method overriding there are three types of polymorphism: run-time polymorphism is specific to the special a) method overloading, with inheritance hierarchies and and important case of operatorobject oriented programming overloading b) method overriding c) run-time polymorphism
7
Types of Polymorphism
In Object Oriented Programming there 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 as the often considered overloading object oriented feature of b) method overridingC++. c) run-time polymorphism
8
class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } };
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 } };
13
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.
15
A Sandwich filling. may contain bacon (yes/no). a fraction of a lettuce-leaf. a number of tomato slices.
l, int t);
16
Operator Overloading
Operators can also be overloaded as methods, e.g. the operator +=:
class BLT { // BLT operator+=(BLT other) { bacon ||= other.bacon; tomatoes += other.tomatoes; lettuce += other.lettuce; } //
20
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 ||= other.bacon; tomatoes += other.tomatoes; lettuce += other.lettuce; }
21
Operator Overloading
Operators can also have other types as parameter:
class BLT { // BLT operator*=(int factor) { tomatoes *= 2; lettuce *= 2; } //
22
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; } //
23
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.
24
26
Polymorphic Pointers
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();
27
Overriding Methods
Methods in the parent class can be redifined in the child class.
class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle vp = new Car(); vp->move(100);
28
Overriding Methods
Methods in the parent class can be redifined in the child class. BUT: class Vehicle { void move(int i); 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
Methods in the parent class can be redifined in the child class.
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
Methods in the parent class can be redifined in the child class.
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
Methods in the parent class can be redifined in the child class. static typing! class Vehicle {
void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle vp = new Car(); vp->move(100);
33
36