OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Overview of C++ Polymorphism
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
Computer Science Department Inheritance & Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
Advanced Programming in Java
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism Lec
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Chapter 11 Class Inheritance
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8

OOP Spring 2007 – Recitation 72 Polymorphic Inheritance

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.

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

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.

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.

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"; } };

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.

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" }

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 ).

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."

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.

OOP Spring 2007 – Recitation 713 Abstract Base Classes

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.

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.

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 }

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.

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 ::.