CMSC202 Computer Science II for Majors

Similar documents
CIS 190: C/C++ Programming. Lecture 11 Polymorphism

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Chapter 10 Introduction to Classes

Lecture 18 Tao Wang 1

Data Structures and Other Objects Using C++

QUIZ. How could we disable the automatic creation of copyconstructors

Java Object Oriented Design. CSC207 Fall 2014

CGS 2405 Advanced Programming with C++ Course Justification

QUIZ. How could we disable the automatic creation of copyconstructors

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance and Polymorphism

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Polymorphism Part 1 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

ECE 122. Engineering Problem Solving with Java

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS250 Final Review Questions

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Data Abstraction. Hwansoo Han

CS-202 Introduction to Object Oriented Programming

Inheritance -- Introduction

G52CPP C++ Programming Lecture 13

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

C++ Important Questions with Answers

C++ Inheritance and Encapsulation

VIRTUAL FUNCTIONS Chapter 10

Inheritance, and Polymorphism.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Polymorphism. Agenda

Object Oriented Programming with c++ Question Bank

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

COP 3330 Final Exam Review

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Instantiation of Template class

Object-Oriented Concept

Framework Fundamentals

Polymorphism. Zimmer CSCI 330

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

G Programming Languages - Fall 2012

CLASSES AND OBJECTS IN JAVA

Object-Oriented Programming

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Seminar on Simulation of Telecommunication Sistems. Introduction to object-oriented programming: C++

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Inheritance (with C++)

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes

Object Oriented Programming. Solved MCQs - Part 2

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

PROGRAMMING LANGUAGE 2

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Introduction to OO Concepts

Java Fundamentals (II)

Objectives. After completing this topic, the students will: Understand of the concept of polymorphism Know on How to implement 2 types of polymorphism

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance and object compatibility

CS250 Final Review Questions

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Lecture 10: building large projects, beginning C++, C++ and structs

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Chapter 14 Abstract Classes and Interfaces

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Object Oriented Design

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Basics of Object Oriented Programming. Visit for more.

Polymorphism. Arizona State University 1

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

What is Inheritance?

JAVA OBJECT-ORIENTED PROGRAMMING

Object Oriented Software Design II

Object Oriented Software Design II

More C++ : Vectors, Classes, Inheritance, Templates

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Building custom components IAT351

Transcription:

CMSC202 Computer Science II for Majors Lecture 14 Polymorphism Dr. Katherine Gibson

Last Class We Covered Miscellaneous topics: Friends Destructors Freeing memory in a structure Copy Constructors Assignment Operators 2

Any Questions from Last Time?

Today s Objectives To review inheritance To learn about overriding To begin to understand polymorphism Limitations of Inheritance Virtual Functions Abstract Classes & Function Types Virtual Function Tables Virtual Destructors/Constructors 4

Review of Inheritance

Review of Inheritance Specialization through sub classes Child class has direct access to Parent member functions and variables that are:??? 6

Review of Inheritance Specialization through sub classes Child class has direct access to Parent member functions and variables that are: Public Protected 7

Review of Inheritance Specialization through sub classes Child class has direct access to Parent member functions and variables that are: Public Protected Parent class has direct access to:??????? in the child class 8

Review of Inheritance Specialization through sub classes Child class has direct access to Parent member functions and variables that are: Public Protected Parent class has direct access to: Nothing in the child class 9

What is Inherited Parent Class public members protected members private variables private functions copy constructor assignment operator constructor destructor 10

What is Inherited Child Class Parent Class child class members (functions & variables) public members protected members private variables private functions copy constructor assignment operator constructor destructor 11

Overriding

Specialization Child classes are meant to be more specialized than parent classes Adding new member functions Adding new member variables Child classes can also specialize by overriding parent class member functions Child class uses exact same function signature 13

Overloading vs Overriding Overloading Use the same function name, but with different parameters for each overloaded implementation Overriding Use the same function name and parameters, but with a different implementation Child class method hides parent class method Only possible by using inheritance 14

Overriding Examples For these examples, the Vehicle class now contains these public functions: void Upgrade(); void PrintSpecs(); void Move(double distance); Car class inherits all of these public functions That means it can therefore override them 15

Basic Overriding Example Car class overrides Upgrade() void Car::Upgrade() { } // entirely new Car-only code When Upgrade() is called on a object of type Car, what happens? The Car::Upgrade() function is invoked 16

Overriding (and Calling) Example Car class overrides and calls PrintSpecs() void Car::PrintSpecs() { } Vehicle::PrintSpecs(); // additional Car-only code Can explicitly call a parent s original function by using the scope resolution operator 17

Attempted Overloading Example Car class attempts to overload the function Move(double distance) with new parameters void Car::Move(double distance, { } double avgspeed) // new overloaded Car-only code But this does something we weren t expecting! 18

Precedence Overriding takes precedence over overloading Instead of overloading the Move() function, the compiler assumes we are trying to override it Declaring Car::Move(2 parameters) Overrides Vehicle::Move(1 parameter) We no longer have access to the original Move() function from the Vehicle class 19

Overloading in Child Class To overload, we must have both original and overloaded functions in child class void Car::Move(double distance); void Car::Move(double distance, double avgspeed); The original one parameter function can then explicitly call the parent function 20

Limitations of Inheritance

Car Example Car SUV Sedan Van Jeep class SUV: public Car {/*etc*/}; class Sedan: public Car {/*etc*/}; class Van: public Car {/*etc*/}; class Jeep: public Car {/*etc*/}; 22

Car Rental Example We want to implement a catalog of different types of cars available for rental How could we do this? Multiple vectors, one for each type (boo!) Combine all the child classes into one giant class with info for every kind of car (yuck! don t do this!) We can accomplish this with a single vector Using polymorphism 23

What is Polymorphism? Ability to manipulate objects in a type-independent way Already done to an extent via overriding Child class overrides a parent class function Can take it further using subtyping, AKA inclusion polymorphism 24

Using Polymorphism A pointer of a parent class type can point to an object of a child class type Vehicle *vehicleptr = &mycar; Why is this valid? Because mycar is-a Vehicle 25

Polymorphism: Car Rental vector <Car*> rentallist; vector of Car* objects 26

Polymorphism: Car Rental vector <Car*> rentallist; vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV Can populate the vector with any of Car s child classes 27

Limitations of Polymorphism Parent classes do not inherit from child classes What about public member variables and functions? 28

Limitations of Polymorphism Parent classes do not inherit from child classes Not even public member variables and functions Vehicle *vehicleptr = &mycar; Which version of PrintSpecs() does this call? vehicleptr->printspecs(); Vehicle::PrintSpecs() 29

Limitations of Polymorphism Parent classes do not inherit from child classes Not even public member variables and functions Vehicle *vehicleptr = &mycar; Will this work? vehicleptr->repaintcar(); NO! RepaintCar() is a function of the Car child class, not the Vehicle class 30

Virtual Functions

Virtual Functions Can grant access to child methods by using virtual functions Virtual functions are how C++ implements late binding Used when the child class implementation is unknown or variable at parent class creation time 32

Late Binding Simply put, binding is determined at run time As opposed to at compile time In the context of polymorphism, you re saying I don t know for sure how this function is going to be implemented, so wait until it s used and then get the implementation from the object instance. 33

Using Virtual Functions Declare the function in the parent class with the keyword virtual in front virtual void Drive(); Only use virtual with the prototype // don't do this virtual void Vehicle::Drive(); 34

Using Virtual Functions The corresponding child class function does not require the virtual keyword Should still include it, for clarity s sake Makes it obvious the function is virtual, even without looking at the parent class // inside the Car class virtual void Drive(); 35

Abstract Classes & Function Types 36

Function Types Virtual virtual void Drive(); Parent class must have an implementation Even if it s trivial or empty Child classes may override if they choose to If not overridden, parent class definition used 37

Function Types Pure Virtual virtual void Drive() = 0; Denote pure virtual by the = 0 at the end The parent class has no implementation of this function Child classes must have an implementation Parent class is now an abstract class 38

Abstract Classes An abstract class is one that contains a function that is pure virtual Cannot declare abstract class objects Why? They have functions whose behavior is not defined! This means abstract classes can only be used as base classes 39

Overview of Polymorphism Assume we have Vehicle *vehicleptr = &mycar; And this method call: vehicleptr->drive(); prototype Vehicle class Car class void Drive() virtual void Drive() virtual void Drive() = 0 Can implement function Can create Vehicle Can implement function Can create Vehicle Cannot implement function Cannot create Vehicle Can implement function Can create Car Calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive 40

Overview of Polymorphism Assume we have Vehicle *vehicleptr = &mycar; And this method call: vehicleptr->drive(); prototype Vehicle class Car class void Drive() virtual void Drive() virtual void Drive() = 0 Can implement function Can create Vehicle Can implement function Can create Vehicle Cannot implement function Cannot create Vehicle Can implement function Can create Car Calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive 41

Overview of Polymorphism Assume we have Vehicle *vehicleptr = &mycar; And this method call: vehicleptr->drive(); prototype Vehicle class Car class void Drive() virtual This is a void pure virtual function, Drive() and Vehicle is now an abstract class virtual void Drive() = 0 Can implement function Can create Vehicle Can implement function Can create Vehicle Cannot implement function Cannot create Vehicle Can implement function Can create Car Calls Vehicle::Drive If no Car::Drive implementation, calls Vehicle::Drive Can implement function Can create Car Calls Car::Drive Must implement function Can create Car Calls Car::Drive 42

Announcements Project 3 is out get started now! Due Thursday, March 31st Exam 2 is in 1.5 weeks Will focus heavily on: Classes Inheritance Linked Lists Dynamic Memory 43