Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Copyright  Hannu Laine C++-programming Part 8 Hannu Laine.
Inheritance (2).
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include.
Functions Prototypes, parameter passing, return values, activation frams.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
1 CSC241: Object Oriented Programming Lecture No 21.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Revision.
Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Inheritance zThe mechanism by which one class can inherit the properties of another. zIt allows a hierarchy of classes to be built, moving from the most.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Operator Overloading Fundamentals
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
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.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
The History of Computer Programming Languages Introductory Programming Visual Basic.
Objective: Students will be able to: Declare and use variables Input integers.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
CSCI-383 Object-Oriented Programming & Design Lecture 21.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Derivation of the 2D Rotation Matrix Changing View from Global to Local X Y X’ Y’  P Y Sin  X Cos  X’ = X Cos  + Y Sin  Y Cos  X Sin  Y’ = Y Cos.
NESTED CLASS. Apa itu nested class ? Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined.
Object-Oriented Programming (OOP) Lecture No. 24.
Fundamentals of structural programming
Inheritance: hiding methods
Inheritance II CMSC 202.
ASSIGNMENT NO.-2.
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
Ambiguity Resolution in Inheritance
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Polymorphism CT1513.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Object-Oriented Programming (OOP) Lecture No. 25
Dynamic Memory A whole heap of fun….
Jeff West - Quiz Section 12
Midterm Thursday, in class.
Pointers Lecture 1 Thu, Jan 15, 2004.
Object-Oriented Programming (OOP) Lecture No. 22
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Introduction to Programming
Similarities and Differences
The Stack.
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:

Review of Inheritance

2 Several Levels of Inheritance Base Class B Derived class D Derived class D1

3 Type Conversions Base Class B Derived class D Derived class D1 D d; //is also of type B D1 d1; //is also of type D, B B b; //is not of type D or D1

4 Assignment b = d Base Class B Derived class D Derived class D1 D d; //is also of type B D1 d1; //is also of type D, B

5 Assignment d = b Base Class B Derived class D Derived class D1 D d; //is also of type B D1 d1; //is also of type D, B B b; //is not of type D

6 Function calls Base Class B Derived class D Derived class D1 D d; //is also of type B void print() {Base::print();} void print() {cout << “B”}

7 Function calls Base Class B Derived class D Derived class D1 D d; //is also of type B D1 d1; //is also of type D, B B b; //is not of type D void print() {Base::print();} void print() {D::print();}

8 Use of Virtual (Dynamic Binding)  To enable programming with pointers to different object types  The compiler generates code to: inspect the type of the object the pointer points to at run- time and then call the appropriate function D * D1

9 Use of Virtual (Dynamic Binding) D *ptr = new D1(); ptr  f(); //dynamic binding – calls f from D1 For dynamic binding to occur for function f: - must use pointers or references - f must exist in D - f must be declared virtual in D

10 Use of Virtual (Dynamic Binding) D d; d.f(); //static binding – calls f from D For dynamic binding to occur for function f: - must use pointers or references - f must exist in D - f must be declared virtual in D

11 Use of Virtual (Dynamic Binding) D *ptr = new D1(); ptr  print(); //dynamic binding – calls print from D1 For dynamic binding to occur for function print: - must use pointers or references - print must exist in D (could be implemented or pure virtual) - print must be declared virtual in D

12 Use of Virtual (Dynamic Binding) D *ptr = new D1(); ptr  print(); //dynamic binding – calls print from D1 Make sure you go through the steps: 1. Find type of ptr (D* here) 2. Look in that class (D here) for the function (print) 3. If print - is not there (neither declared in D nor inherited from B)  compiler error ! - is there and is virtual (either declared virtual explicitly or inherited from B)  dynamic binding (print from D1) - is there and not virtual  static binding (print from D)

13 Inheritance with Virtual Functions Base Class B virtual void print(){cout <<“B”} Derived class D Derived class D1 not overriden void print(){cout <<“D1”;} which print() is called? D *ptr = new D1; ptr->print(); “D1” Dynamic binding

14 Inheritance with Virtual Functions Base Class B virtual void print(){cout <<“B”} Derived class D Derived class D1 not overriden which one is it then ? D *ptr = new D1; ptr->print(); “B” the one closest in the hierarchy not overriden Dynamic binding occurs but print is not overriden in D1, it is inherited as is from B

15 Inheritance with Virtual Functions Base Class B void print(){cout <<“B”} Derived class D Derived class D1 void print() {cout << “D”} overridden which one is it? D *ptr = new D1; ptr->print(); -- “D” the one closest in the hierarchy Dynamic binding occurs but print is not overridden in D1

16 Virtual Destructors ! Base Class B ~B(); Derived class D Derived class D1 ~D(); which destructor is called ? D *ptr = new D1; Delete ptr; ~D1();

17 Virtual Destructors ! Base Class B ~B(); Derived class D Derived class D1 ~D(); which destructor is called ? D *ptr = new D1; Delete ptr; //~D and ~B called ~D1(); Static binding occurs, because ~D is not virtual

18 Virtual Destructors ! Base Class B ~B(); Derived class D Derived class D1 virtual ~D(); which destructor is called ? D *ptr = new D1; Delete ptr; //~D1, ~D and ~B ~D1(); Dynamic binding occurs, because ~D is virtual

19 Virtual Destructors ! Base Class B virtual ~B(); Derived class D Derived class D1 ~D(); which destructor is called ? D *ptr = new D1; Delete ptr; //~D1, ~D and ~B ~D1();