360 likes | 393 Views
Learn about the concept of polymorphism in OOP, including its types like method overloading and overriding. Explore examples and motivations behind operator overloading.
E N D
By: Marc Conrad & Rob Manton University of Luton Email: Marc.Conrad@luton.ac.uk Rob.Manton@luton.ac.uk Room: D104 Object Oriented ProgrammingDevelopment - Polymorphism
Introduction Non object oriented basics Classes Inheritance Aggregation Polymorphism Multifile Development Module Outline
The Meaning of the word. • From the Greek: • Polus + Morphe = Polumorphos (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.
The Meaning of the word. • In object-oriented computing it means: different forms of data being handled by the same type of process. • 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)
Types of Polymorphism • In Object Oriented Programming there are three types of polymorphism: • method overloading, with the special and important case of operator overloading • method overriding • run-time polymorphism
In Object Oriented Programming there are three types of polymorphism: method overloading, with the special and important case of operator overloading method overriding run-time polymorphism Method overloading can also be applied in non-object oriented contexts and refers boths to functions and methods. Types of Polymorphism
In Object Oriented Programming there are three types of polymorphism: method overloading, with the special and important case of operator overloading method overriding run-time polymorphism Method overriding and run-time polymorphism is specific to inheritance hierarchies and object oriented programming Types of Polymorphism
In Object Oriented Programming there are three types of polymorphism: method overloading, with the special and important case of operator overloading method overriding run-time polymorphism Run-time polymorphism, also called dynamic binding, or late binding is often considered as the object oriented feature of C++. Types of Polymorphism
Method & Function Overloading • Overloading a function simply means, that a function is not only defined its name but by its name and parameter types. • The following functions are different in C++: • int makeBreakfast(int i, int k); • void makeBreakfast(Creature who); • float makeBreakfast();
class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class born1997
class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class These two methods are different. born1997
class Creature { private: int yearOfBirth; public: void setYearOfBirth(int year) { yearOfBirth = year; } void setYearOfBirth(Creature other) { yearOfBirth = other.yearOfBirth; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class These two methods are different because they have different argument types. born1997
Operator Overloading - Motivation • Question: How many function calls are involved in the following statement? a = 2 + 3
Operator Overloading - Motivation • Question: How many function calls are involved in the following statement? a = 2 + 3 • There are two functions implicitly involved:+ and =. • Look at this statement as “assign(a, add(2,3));”
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.
class BLT { public: bool bacon; float lettuce; int tomatoes; // Constructor: BLT(bool b, float l, int t); // … (more code) }; A Sandwich filling. may contain bacon (yes/no). a fraction of a lettuce-leaf. a number of tomato slices. 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*/ 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 */ Overloading Operators - Example This is the operator we want to overload
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 y, BLT z) { bool b = y.bacon || z.bacon; float l = y.lettuce + z.lettuce; int t = y.tomatoes = z.tomatoes; BLT result(b,l,t); return result; } Overloading Operators - Example
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; • } • //…
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*/ 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; • } • //…
Operator Overloading • Operators can also have other types as parameter: • class BLT { • // … • BLT operator*=(int factor) { • tomatoes *= 2; • lettuce *= 2; • } • //…
BLT filling1(false,0.5,2); … filling1 *= 2; ... /* Should give a filling with no bacon, 1 lettuce and 4 tomatoes */ Operator Overloading • Operators can also have other types as parameters: • class BLT { • // … • BLT operator*=(int factor) { • tomatoes *= factor; • lettuce *= factor; • } • //…
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.
Operator Overloading - Interesting Observation • cout << “Hello World\n”; Overloaded << operator
Operator Overloading - Summary • Operators may be overloaded to work with user defined data types (objects). • The syntax for overloading involves the 'operator' keyword and the operator. • Note: In a good design it is important, that the normal meanings of operators are not distorted (don't subtract with a + operator)
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(); Valid C++ syntax! Polymorphic Pointers
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); Valid C++ syntax! 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); BUT: Which of these two move() methods will be called? 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 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? 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! In C++, static typing is the default behaviour. Overriding Methods As vp is of type pointer to a Vehicle, the method of the Vehicle is called.
Methods in the parent class can be redifined in the child class. class Vehicle { virtual void move(int i); }; class Car : public Vehicle { virtual void move(int i); }; // ... Vehicle vp = new Car(); vp->move(100); dynamic binding! The keyword virtual allows the use of dynamic binding. Overriding Methods - The keyword virtual As vp points to a Car object the method of the Car is called
Abstract methods are methods without any implementation (pure virtual methods). 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. Abstract Methods & Classes Note that Vehicle objects cannot be instantiated (but Car objects).
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. 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). Static typing & Dynamic binding
Important stuff to remember • Overriding methods • Polymorphic pointers • Static binding • Dynamic binding - the virtual keyword • Abstract methods