Object Oriented Programming Elhanan Borenstein Lecture #7.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Object Oriented Programming Elhanan Borenstein Lecture #8 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
Object Oriented Programming Elhanan Borenstein Lecture #6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
Polymorphism &Virtual Functions
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming Elhanan Borenstein Lecture #4.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Object Oriented Programming Elhanan Borenstein Lecture #5.
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
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
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.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CS 3370 – C++ Object-oriented Programming
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
CS212: Object Oriented Analysis and Design
Object Oriented Programming
Inheritance, Polymorphism and the Object Memory Model
Virtual Functions Department of CSE, BUET Chapter 10.
Objects Managing a Resource
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Presentation transcript:

Object Oriented Programming Elhanan Borenstein Lecture #7

Agenda Inheritance Polymorphism

Inheritance & Polymorphism

Inheritance Example class Base1 { int var1; public: Base1() {…} // constructor void OldFunc() {…} void Init() {…} }; class Derived1 : [public|protected|private] Base1 { int var2; public: Derived1() {…} // constructor void Init() {…} // override void Do(){…} };

Inheritance When inheriting a class, we inherit all its members and they will be included in the derived class: Somewhat similar to object data members (embedded): To differentiate, we can check the logics behind the implemented entities:  Inheritance: The derived class is a type of the base class.  Inner class: The inner class is a part of the external class. The Logic behind Inheritance Base (father) Additions Derived (son)  (SIZE?) Additions embedding class inner object

Inheritance Understanding Inheritance - Example class GraphicItem { int color; public: GraphicItem() {…} // constructor void Draw() {…} void ChangeColor(int n_clr) {…} }; class CPoint : public GraphicItem { int m_x, m_y; public: CPoint() {…} // constructor void Draw() {…} // override void Align(){…} }; … GraphicItem G, *pG; CPoint P, *pP; … G = P; P = G; pG = pP; pP = pG; …

Inheritance All the data members of the base class also exists in the derived class. But…:  There are cases where we cannot access these members.  They will still be included in the derived object size. When won’t we be able to access the base class members:  Hiding – The derived class has defined a data member with the same name. We can still access the hidden data member using its full name (base_class::member)  Private Permissions – Any private member of the base class cannot be accessed in the derived class. Note: the private data member is still included in the derived class (and can be accessed using a public method of the base class). A good design, will include in the base class only data members that are common (and can be accessed) by all derived classes. Accessing the Base Class Data Members

Inheritance The derived class also inherit all the base class methods. The derived class can override inherited methods. When activating a method of an object from the derived class: > If the inherited method was not overridden the base method (from the base class) will be activated. > If the method was overridden in the derived class, the method of the derived class will be activated.  We can still activate the base method using its full name.  It is very common to include a call to the base method from the derived method. C’tor, D’tor and C.C. are the only methods that have a different behavior when inherited. Using the Base Class Member Functions

Inheritance C’tor and D’tor are not inherited (WHY?). Constructing an object of the derived class, always activates first the C’tor of the base class (to create the base part of the object). The order is thus as follows:  The C’tor of the base class is activated.  The additional data members of the derived class are created.  If some of the data members are objects  the C’tor of each such object is activated.  The C’tor of the derived class is activated The order of the D’tor’s activation is of course reversed. What if the C’tor of the base class has parameters? C’tor and D’tor in Inherited Classes

Inheritance If the C’tor of the base class has parameters we must use the initialization line (in an analogous manner to object data members): C’tor and D’tor in Inherited Classes – Cont’ class CRect { CPoint pnt1, pnt2; public: CRect(int x1, int y1, …) : pnt1(x1,y1), pnt2(0,0) {…} // c’tor }; class CGraphicRect : public GraphicItem { … public: CGraphicRect(int color) : GraphicItem(color) {…} // c’tor }; Embedded Objects Inheritance

The assignment operator in not inherited. If the derived class did not implement the assignment operator, when copying, the base class assignment operator will not be activated but rather the default assignment operator of the derived class. But… as part of the default assignment operator action, (when the base class members should be copied), the base class assignment operator will be activated. Example: Stack Assignment Operator (=) in Inherited Classes

Inheritance Rule of thumb: A derived class should implement an assignment operator only if it performs dynamic allocation. The same is true for the copy constructor and the destructor. Remember the triplet:  Copy Constructor  Destructor  Assignment Operator Example: AB.cpp Assignment Operator (=) in Inherited Classes

Polymorphism The Polymorphism mechanism allows for an action to performs differently according to the object type, while being transparent to the programmer. As we recall, a pointer to the base class can also point to objects from a derived class.  There will be no data loss  We will be able to activate methods on that object, only if they exist in the base class. The issue is however, which method will be activated (base or derive)? Introduction

Polymorphism Example class Employee { double Salary; public: void Print() const {…} void RaiseSalary (double r) { Salary += r; Print(); } }; class Manager : public Employee { … public: void Print() {…} }; main () { Employee *pE; pE = new Manager; pE->Print(); … Manager m; m.Print(); m.RaiseSalary(); }

Polymorphism Typing – Identifying the type of the object we manipulate. Binding – Associating the memory address of functions/variables. Static binding – upon compilation (like in C) Dynamic binding – upon runtime (like in Java) In C++ binding is static unless instructed otherwise. Static & Dynamic Typing/Binding

Polymorphism When activating a method through a pointer to the base class:  if the method was not defined as virtual:  A Static Binding is performed (on compilation)  The base class method will be activated  If the method was define as virtual:  A Dynamic Binding will be performed (on runtime)  The method of the appropriate class will be activated. Example: Humus_Ashkara Virtual Methods and Dynamic Binding

Polymorphism The only place in the application we checked the object type, was in the input function (object creation). Our goal in polymorphism is to avoid checking the actual type of an object by using virtual methods and operators. The virtual property in the application was used twice:  Directly activating a method (in the main) through a pointer to the base class  the appropriate method was activated.  The print method of the base class is using virtual methods and using the appropriate methods according to the object which activated the print method. This paradigm is called GENERIC ALGORITHMS!!! Note that we have used one array to store and handle objects of various types. We have actually implemented a General Container (can we do that in C?) Humus_Ashkara – A few Notes

Polymorphism General Containers support the implementation of data structures using pointers to Data* while actually storing objects that are derived from Data. Examples: Humus, MFC objects (CObArray), Graphic objects… General Containers Generic Algorithms forms a skeleton of the algorithms by calling methods which will be implemented differently by each derived classes. Example: Print, Draw Object, Calc3DVolume. Generic Algorithms

Polymorphism The Virtual property is inherited. If a methods was defined in the base class as virtual, it will be virtual in all its derived classes. However, it is recommended to include the virtual keyword also in the derived classes. If objects in the containers has a non-trivial destructor, it must be virtual (WHY?).  Rule of thumb: If virtual inheritance is used, we must implement a virtual destructor in the base (even if it will be empty). When activating a virtual method from within the c’tor or d’tor of the base class, the base class method will be activated (even in the object created/destroyed is of a derived class). WHY? Virtual Methods – Guidelines

Polymorphism There are cases (especially when we implemented a General Container), where there is no intention to actually create an object of the base class. In such a case, we wish to:  Avoid implementation of some of the base class virtual methods (which will be overridden by the derived classes anyway).  Prevent the programmer from creating objects of the base class. The solution is Pure Functions and Abstract Classes:  A pure function is a virtual function, declared in the base class only for the purpose of implementing it in the derived classes. To define a function as pure we will add =0 in the prototype.  A class with one or more pure function is automatically an abstract class  objects of that class cannot be created or passed ByVal. Example: abstract Abstract Classes

Polymorphism Dynamic binding is implemented by a virtual functions table. Each object that includes a virtual function will also include a pointer to this table (usually two bytes pointer): Calling a virtual function will thus require two memory calls: C obj;  How Does Dynamic Binding Work? class A { int a; public: virtual void f() {…} virtual void h() {…} virtual void g() {…} }; class B : public A { int b; public: virtual void g() {…} }; class C : public B { int c; public: virtual void h() {…} }; a vfptr b c A::f() B::g() C::h() C vftable

Polymorphism Try that… class A { public: virtual void f() {cout<<“A”;} void h() {cout<<“a”;} virtual void g() { f(); h(); } }; class B : public A { public: virtual void f() {cout<<“B”;} void h() {cout<<“b”;} }; class C : public B { public: virtual void f() {cout<<“C”;} void h() {cout<<“c”;} }; main() { B* p; p = new C; p->g(); p->h(); }; 1. Abc 2. Cab 3. Acb 4. Cac 5. Ccc

Polymorphism There are scenarios where we are required to check what is the real object type that we actually hold. (WHEN?) We can implement our own mechanism using a virtual function called Type(). Alternatively, the operator typeid can be used.  #include is required (not an integral part of C++)  Setting the project to work with RTTI is required  The return value is an object of type typeinfo, that supports the method name() and the operator ==  typeid also works on fundamental data types (int, float, etc.) Example: typeinfo Runtime Type Information

Polymorphism Dynamic casting is a safe method to perform casting to a specific derived class while checking if it actually points to an object of that class. There are cases where dynamic_cast has an advantage over typeid. For example: Dynamic Casting class A { }; class B : public A { void Do() {…} }; class C ; public B { }; void main() { A* pa = new C; if (typeid(*pa)==typeid(B)) ((B*)pa)->Do(); B* pb=dynamic_cast (pa); if (pb) pb->Do(); }

Questions?