Professional Documents
Culture Documents
Week 10
Week 10
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
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
10
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 } };
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 } };
14
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
l, int t);
17
If we try adding the two objects together at the moment we get the expected error message
20
23
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
Now we can perform class-specific output using the standard << syntax!
35