
Object- Oriented Programming Iuliana Bocicor Polymorphism Object-Oriented Programming Static and dynamic binding Virtual Iuliana Bocicor methods [email protected] Upcasting and downcasting Babes-Bolyai University Abstract classes 2020 1 / 41 Overview Object- Oriented Programming Iuliana Bocicor 1 Polymorphism Polymorphism Static and 2 Static and dynamic binding dynamic binding Virtual 3 methods Virtual methods Upcasting and downcasting 4 Abstract Upcasting and downcasting classes 5 Abstract classes 2 / 41 Primary OOP features Object- Oriented Programming Abstraction: separating an object's specification from its Iuliana Bocicor implementation. Polymorphism Static and Encapsulation: grouping related data and functions to- dynamic binding gether as objects and defining an interface to those objects. Virtual methods Upcasting and Inheritance: allowing code to be reused between related downcasting types. Abstract classes Polymorphism: allowing an object to be one of several types, and determining at runtime how to "process" it, based on its type. 3 / 41 PolymorphismI Object- Oriented Programming Iuliana Definitions Bocicor Polymorphism Polymorphism is the property of an entity to react differ- Static and ently depending on its type. dynamic binding Virtual Polymorphism is the property that allows different entities methods Upcasting and to behave in different ways to the same action. downcasting Abstract classes Polymorphism allows different objects to respond in differ- ent ways to the same message. Polymorphism - Greek meaning: "having multiple forms". 4 / 41 PolymorphismII Object- Oriented Programming Usually, polymorphism occurs in situations when one has Iuliana classes related by inheritance. Bocicor Polymorphism A call to a member function will cause the execution of a Static and dynamic different code, depending on the type of object that invokes binding the function. Virtual methods Upcasting and downcasting The code to be executed is determined dynamically, at run Abstract time. classes The decision is based on the actual object. 5 / 41 Polymorphism III Object- Oriented Programming Iuliana Bocicor Polymorphism Static and dynamic binding Virtual methods Upcasting and downcasting Abstract classes Figure source: https://www.youtube.comwatchv=ng98qapa4Sw 6 / 41 PolymorphismIV Object- Oriented Programming Iuliana Bocicor ? In the code below, why are we allowed to write: Polymorphism Animal* a2 = &p1; ? Static and dynamic binding Animal a1f"black" , 20g; Virtual Penguin p1f"black and white" , 7 , "Magellanic" g ; methods Animal ∗ a2 = &p1 ; Upcasting and downcasting cout<<a2−>toString(); //which toString function? Abstract The one from the class Animal, or the one classes from the class Penguin? 7 / 41 Static binding Object- Oriented Programming Iuliana Bocicor The choice of which function to call is made at compile time. Polymorphism Static and dynamic The object a2 is declared as a pointer to Animal ) at binding compile-time it is decided that the function Animal::toString() Virtual methods will be called. Upcasting and downcasting Abstract Static binding is also called early binding. classes 8 / 41 Dynamic bindingI Object- Oriented Programming In the presented case, the behaviour we expect is a call to Iuliana Bocicor the function Penguin::toString(). Polymorphism Static and At runtime, the actual type of the object is determined. dynamic binding Virtual methods The decision of using Animal::toString() or Penguin::toString() Upcasting and should be taken only after determining the actual type of downcasting the object ) at runtime. Abstract classes This is dynamic binding: take the correct decision of which function body to execute, according to the actual type of the object. 9 / 41 Dynamic bindingII Object- Oriented Programming Iuliana Dynamic binding is also called late binding. Bocicor Polymorphism When a message is sent to an object, the code being called Static and dynamic in not determined until runtime. binding Virtual methods Dynamic binding only works with non-value types: ref- Upcasting and erences and pointers. downcasting Abstract classes In C++, dynamic binding is achieved using virtual func- tions. 10 / 41 Virtual methodsI Object- Oriented Programming Iuliana Declaration Bocicor virtual function signature Polymorphism Static and dynamic binding If a function is declared virtual in a base class and then Virtual overriden in a derived class ) dynamic binding is enabled. methods Upcasting and downcasting The actual function that is called depends on the content Abstract classes of the pointer (or reference). The function becomes polymorphic by being designed vir- tual. 11 / 41 Virtual methodsII Object- Oriented Programming The function in the derived class that is overriding the func- Iuliana tion in the base class can use the override specifier to ensure Bocicor that the function is overriding a virtual function from the Polymorphism base class. Static and dynamic binding override is an identifier with a special meaning when used Virtual methods after member function declarators and otherwise, it is not Upcasting and a reserved keyword. downcasting Abstract classes DEMO Polymorphic function toString (Animal - Penguin, Dog) (Lec- ture6 demo virtual functions). 12 / 41 C++ mechanismI Object- Oriented Programming Iuliana In memory, for an object with no virtual functions, only its Bocicor own data is stored. Polymorphism Static and dynamic Member functions or pointers to them are not stored in the binding object. They are stored in a code memory section, and are Virtual methods known to the compiler. Upcasting and downcasting Abstract When a member function is called, the pointer to the cur- classes rent object (this) is passed as an invisible parameter so the functions know on which object to operate on when they are called. 13 / 41 C++ mechanismII Object- Oriented Programming Things are different when virtual functions come into play. Iuliana Bocicor In the case of a derived class with virtual functions, the com- Polymorphism piler creates a table of function addresses called the virtual Static and dynamic table - a static array set up by the compiler at compile time. binding Virtual methods Every class that uses virtual functions (or is derived from Upcasting and a class that uses virtual functions) will have its own virtual downcasting table. Abstract classes Each entry in the virtual table is a function pointer that points to the most derived function accessible by the class. 14 / 41 C++ mechanism III Object- Oriented Programming Iuliana A pointer to the virtual table (vptr) is added to the base Bocicor class - and inherited by the derived classes. Polymorphism Static and dynamic When a class object is created, the pointer to the virtual binding table is set to point to the virtual table for that class. Virtual methods Upcasting and downcasting When a call is made through a pointer or reference, the Abstract compiler generates code that dereferences the pointer to classes the object`s virtual table and makes an indirect call using the address of a member function stored in the table. 15 / 41 C++ mechanismIV Object- Oriented Programming Iuliana Bocicor Polymorphism Static and dynamic binding Virtual methods Upcasting and downcasting Abstract classes 16 / 41 C++ mechanismV Object- Oriented Programming Iuliana Bocicor Animal ∗ p = new Penguin f "black and white" , 7 , "Magellanic" g ; Polymorphism Static and dynamic binding Virtual methods Upcasting and downcasting Abstract classes 17 / 41 C++ mechanismVI Object- Oriented Programming The virtual function mechanism works only with pointers Iuliana and references, but not with value-types (objects). Bocicor Polymorphism Calling a virtual function is slower than calling a non-virtual Static and function: dynamic binding 1 Use the vptr to access the correct virtual table. Virtual 2 Find the correct function to call in the virtual table. methods 3 Call the function. Upcasting and downcasting Abstract Declare functions as virtual only if necessary. classes DEMO Virtual functions. (Lecture6 demo virtual functions). 18 / 41 Virtual constructors? Object- Oriented Programming Iuliana Bocicor Constructors cannot be virtual. Polymorphism Static and dynamic When creating an object, one must know exactly what type binding of object one is creating. Virtual methods Upcasting and downcasting Usually, the virtual table pointer is initialized in the con- Abstract structor. classes 19 / 41 Virtual destructorsI Object- Oriented Programming Iuliana Bocicor A derived class object contains data from both the base class and Polymorphism the derived class. Static and dynamic binding The destructor's responsibility is to dealocate resources (mem- Virtual ory). methods Upcasting and downcasting In the case of a derived class object, it is essential that both the Abstract classes destructor of the base class and the destructor of the derived class are called. 20 / 41 Virtual destructorsII Object- Oriented Programming Iuliana The correct destructor must be invoked based on the actual type Bocicor of the object, not the type of the pointer holding the reference Polymorphism to the object. Static and dynamic binding Therefore, the destructor must have a polymorphic behaviour ) Virtual the base class destructor must be virtual. methods Upcasting and downcasting Abstract classes DEMO Virtual destructor. (Lecture6 demo virtual functions). 21 / 41 Upcasting and downcastingI Object- Oriented Programming Upcasting Iuliana Bocicor Casting an object/reference/pointer of a derived class to an Polymorphism
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages41 Page
-
File Size-