OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.

Slides:



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

OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
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.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract 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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
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.
OOP, Virtual Functions and Inheritance
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter -6 Polymorphism
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.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
OOP, Virtual Functions and Inheritance
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
CS212: Object Oriented Analysis and Design
Polymorphism Lec
Java Programming Language
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.
Jeff West - Quiz Section 12
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.
Presentation transcript:

OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7

OOP Etgar 2008 – Recitation 72 Polymorphic Inheritance

OOP Etgar 2008 – 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 Etgar 2008 – Recitation 74 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 Etgar 2008 – Recitation 75 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 Etgar 2008 – Recitation 76 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 Etgar 2008 – Recitation 77 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 Etgar 2008 – Recitation 78 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 Etgar 2008 – Recitation 79 Slicing When a function’s parameter is a base object passed by value, the object passed to it is copied using base copy constructor. void func(Shape s); // Shape's copy ctor applies This ctor knows nothing about subclasses. Thus, only the base part of the object is copied This is called slicing and can be a source of errors.

OOP Etgar 2008 – 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 Etgar 2008 – 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 Etgar 2008 – 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. virtual and static are mutually exclusive.

OOP Etgar 2008 – Recitation 713 Virtual Recommendations operator= shouldn’t be virtual. Never redefine inherited non- virtual methods. Overloaded functions shouldn’t be virtual. If the class is intended to be a base class, the destructor should be virtual. Compiler-generated destructor is not virtual, unless it’s for a class that inherits from a base with virtual destructor.

OOP Etgar 2008 – Recitation 714 Abstract Base Classes

OOP Etgar 2008 – Recitation 715 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 Etgar 2008 – Recitation 716 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 Etgar 2008 – Recitation 717 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 Etgar 2008 – Recitation 718 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 Etgar 2008 – Recitation 719 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 ::.