CIS 190: C/C++ Programming. Polymorphism



Similar documents
Advanced Data Structures

Constructor, Destructor, Accessibility and Virtual Functions

1. Polymorphism in C++...2

Glossary of Object Oriented Terms

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

C++FA 5.1 PRACTICE MID-TERM EXAM

Compile-time type versus run-time type. Consider the parameter to this function:

13 Classes & Objects with Constructors/Destructors

El Dorado Union High School District Educational Services

Java Interview Questions and Answers

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Introduction to Programming Block Tutorial C/C++

C++ INTERVIEW QUESTIONS

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Description of Class Mutation Mutation Operators for Java

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

CEC225 COURSE COMPACT

History OOP languages Year Language 1967 Simula Smalltalk

Chapter 13 - Inheritance

An Internet Course in Software Development with C++ for Engineering Students

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

An Incomplete C++ Primer. University of Wyoming MA 5310

The C Programming Language course syllabus associate level

Java (12 Weeks) Introduction to Java Programming Language

OpenCL Static C++ Kernel Language Extension

PHP Object Oriented Classes and objects

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Class 16: Function Parameters and Polymorphism

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Data Structures Using C++ 2E. Chapter 5 Linked Lists

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Implementation Aspects of OO-Languages

The Separation of Interface and Implementation in C++

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

BCS2B02: OOP Concepts and Data Structures Using C++

C++ Programming Language

How To Teach C++ Data Structure Programming

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Basics of C++ and object orientation in OpenFOAM

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

Introduction to Object-Oriented Programming

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

C++ Overloading, Constructors, Assignment operator

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration


language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Part 3: GridWorld Classes and Interfaces

CISC 181 Project 3 Designing Classes for Bank Accounts

Curriculum Map. Discipline: Computer Science Course: C++

Facebook Twitter YouTube Google Plus Website

Agile Software Development

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Object-Oriented Programming

Object Oriented Software Design II

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CS 101 Computer Programming and Utilization

16 Collection Classes

Java Application Developer Certificate Program Competencies

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

CS193j, Stanford Handout #10 OOP 3

Object-Oriented Programming: Polymorphism

Symbol Tables. Introduction

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Borland Delphi 6 Product Certification. Study Guide. Version 1.0 Copyright 2001 Borland Software Corporation. All Rights Reserved.

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Extending Classes with Services

Lecture 1 Introduction to Android

Variable Base Interface

Part VI. Object-relational Data Models

3 Representation in C++.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Programmation 2. Introduction à la programmation Java

MODULE 17 POLYMORPHISM

Subject Name: Object Oriented Programming in C++ Subject Code:

VB.NET - CLASSES & OBJECTS

Java EE Web Development Course Program

Transcription:

CIS 190: C/C++ Programming Polymorphism

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

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

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

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

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

What is Polymorphism? ability to manipulate objects in a type-independent way already done to an extent via overloading can take it further using subtyping, AKA inclusion polymorphism

Using Polymorphism only possible by using pointers to objects a pointer of a parent class type can point to an object of any child class type Vehicle *vehicptr = &mycar; this is valid because mycar is-a Vehicle

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

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*/};

Car Example: Car Rental implement a catalog of cars available for rental how could we do this (using vectors)?

Car Example: Car Rental implement a catalog of cars available for rental two options: separate vector for each type of Car (SUV, Van, etc.) have to add a new vector if we add new type must have separate variables for each vector single vector of Car pointers no changes necessary if we add new type

Car Example: Car* vector vector <Car*> rentallist; vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

Polymorphism Limitations parent classes do not inherit from child classes not even public member variables and functions vehicptr->printspecs(); will call Vehicle s PrintSpecs() function, not Car s vehicptr->drive(); will not work; Drive() is a function only of the Car class, and vehicptr can t access it

Virtual Functions can grant access to child methods by using virtual functions to do this, declare the function in the parent class with the keyword virtual can also use virtual keyword in child class, but not required

Virtual Function Example class Vehicle{ virtual void Drive(); /* rest of vehicle class */ } class Car: public Vehicle { void Drive(); /* rest of car class */ }

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

Function Types Pure Virtual virtual void Drive() = 0; denoted with an = 0 at end of declaration this makes the class an abstract class child classes must have an implementation of the pure virtual function cannot declare objects of abstract class types

Function Types Virtual virtual void Drive(); parent class must have an implementation child classes may override if they choose to if not overridden, parent class definition used

Function Types Non-Virtual void Drive(); parent class should have an implementation child class cannot override function parent class definition always used should be used only for functions that won t be changed by child classes

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

Behind the Scenes assume our Drive() function is pure virtual how does the compiler know which child class s version of the function to call? vector of Car* objects SUV SUV Jeep Van Jeep Sedan Sedan SUV

Virtual Tables lookup tables of functions employed when we use polymorphism virtual tables are created for: classes with virtual functions child classes derived from those classes handled by compiler behind the scenes

Virtual Table Pointer compiler adds a hidden variable that points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan SUV

Virtual Table Pointer compiler adds a hidden variable that points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan SUV * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr

Virtual Table Pointer compiler adds a hidden variable that points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan SUV * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table

Virtual Table Pointer compiler adds a hidden variable that points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan SUV * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive();

Virtual Table Pointer compiler adds a hidden variable that points to the appropriate virtual table of functions SUV SUV Jeep Van Jeep Sedan Sedan SUV * vptr * vptr * vptr * vptr * vptr * vptr * vptr * vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table * to SUV::Drive(); * to Jeep::Drive(); * to Van::Drive(); * to Sedan::Drive();

Outline Review of Inheritance Polymorphism Car Example Virtual Functions Virtual Function Types Virtual Table Pointers Virtual Constructors/Destructors

Virtual Destructors Vehicle *vehicptr = new Car; delete vehicptr; non-virtual destructors will only invoke the base class s destructor for any class with virtual functions, you must declare a virtual destructor as well

not a thing... why? Virtual Constructors

Virtual Constructors not a thing... why? we use polymorphism and virtual functions to manipulate objects without knowing type or having complete information about the object when we construct an object, we have complete information there s no reason to have a virtual constructor

Project Alphas due next Monday (April 14th) doesn t: have to be working a complete project in a folder named <your_team_name>