Download presentation
Presentation is loading. Please wait.
1
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8
2
OOP Spring 2007 – Recitation 72 Polymorphic Inheritance
3
OOP Spring 2007 – Recitation 73 The Motivation Inheritance alone does not give us much strength except from moving the common code into base class. We would want the behavior to be dependent on the exact object’s type. Both OperaSingers and OrientalSingers are Singers, and they sing, but they do it differently.
4
OOP Spring 2007 – Recitation 74 The Motivation In the regular inheritance pointer/ reference to the base class can point/refer to object of the derived class Function that gets a pointer/reference to a base class can get a parameter of a derived class void func(Person* p); func(&s); // ok! Even when s is a student but… p->print(); will call Person::print() even if Student has a print() function as well polymorphic inheritanceThe way of calling the right print is virtual functions and polymorphic inheritance
5
OOP Spring 2007 – Recitation 75 Static and Dynamic Binding We would like a way to “say” that a derived may provide own implementation for base’s method. This is called polymorphism – the behavior exhibited is dependent on the actual object type. This is done using dynamic binding – the compiler defers the decision which function to execute until runtime. The decision depends on the exact type of the object. With static binding the decision is made at compile-time.
6
OOP Spring 2007 – Recitation 76 Virtual Keyword virtual on a method specifies that a derived class may provide its own implementation for that method (override). If the derived doesn’t want to override a virtual method, it doesn’t provide an implementation, and the implementation is inherited from base. A virtual method is dynamically bound, while non- virtual – statically.
7
OOP Spring 2007 – Recitation 77 Example class Shape { public: virtual void draw() { cout << "I'm a shape"; } }; class Circle : public Shape { public void draw() { cout << "I'm a circle"; } };
8
OOP Spring 2007 – Recitation 78 When Polymorphism Applies Polymorphic behavior requires two conditions: –Methods called must be virtual. –Objects must be manipulated through references or pointers. When manipulating an object directly, the compiler knows the exact type, so polymorphism is not used.
9
OOP Spring 2007 – Recitation 79 Example void drw_ref(Shape& s) {s.draw();}// Polymorphic void drw_ptr(Shape* s) {s->draw();}// Polymorphic void drw_direct(Shape s) {s.draw();}// Statically bound int main() { Shape s; Circle c; drw_ref(s); drw_ptr(s); drw_direct(s); // Prints "I'm a shape" drw_ref(c); drw_ptr(c);// Prints "I'm a circle" drw_direct(c);// Prints "I'm a shape" }
10
OOP Spring 2007 – Recitation 710 Overloading vs. Overriding Overloading means differentiating functions by their names and parameters. Overriding means providing own implementation for an inherited virtual method. Mixing the two is a bad idea – overriding an overloaded base method will hide all other versions (even if declared virtual ).
11
OOP Spring 2007 – Recitation 711 Virtual and Scope Operator If the scope resolution operator :: is used, the virtual mechanism is ignored: void Circle::draw() { Shape::draw();// Static binding cout << "and a circle"; } c.draw(); // Prints "I'm a shape and a circle."
12
OOP Spring 2007 – Recitation 712 Virtual Notes Constructors can’t be virtual. Calls to class’s virtual methods in its constructor and destructor are statically bound (but should be avoided). Default arguments are statically bound, even for virtual methods.
13
OOP Spring 2007 – Recitation 713 Abstract Base Classes
14
OOP Spring 2007 – Recitation 714 Interfaces While exploring the idea of polymorphism we discover that sometimes there is no meaningful implementation for the base class. There is no sensible Shape implementation and no sense creating Shape objects. The Shape is an interface – it specifies what behavior all its subclasses exhibit, but can’t provide a “default” implementation.
15
OOP Spring 2007 – Recitation 715 ABC An interface in C++ can be specified by abstract base class (ABC). To make a class an ABC, make one or more of its methods a pure virtual function. A virtual function is “made pure” by the initializer =0.
16
OOP Spring 2007 – Recitation 716 Example class Shape { public: virtual void draw() = 0;// Pure virtual virtual void rotate(int) = 0;// Pure virtual virtual move(int);// Non-pure }; int main() { Shape s;// Error Circle c;// Ok }
17
OOP Spring 2007 – Recitation 717 What Does It Mean? An abstract class can be used only as a base for other classes (interface) – no objects of that class can be created. Derived classes provide implementations for pure virtual functions. A pure virtual function that is not defined in a derived remains pure virtual, and such derived class is also an abstract base.
18
OOP Spring 2007 – Recitation 718 ABC Notes An ABC typically doesn’t need a constructor (but does need a virtual destructor). Pure virtual function can have an implementation in the ABC, and it can be called using scope resolution operator ::.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.