COP 3330 Object-oriented Programming in C++

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Day Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.
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.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance in C++ How to do it Copyright © Curt Hill
Modern Programming Tools And Techniques-I
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Polymorphism and Virtual Functions
Object Oriented Programming
Designing for Inheritance
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Learning Objectives Inheritance Virtual Function.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Polymorphism CT1513.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Advanced Inheritance Concepts
Inheritance and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
CIS 199 Final Review.
Review of Previous Lesson
Chapter 6 Polymorphism.
Lecture 10 Concepts of Programming Languages
Chapter 11 Class Inheritance
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
C++ Polymorphism Reference and pointer implicit type casting
Lecture 6: Polymorphism
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.
Computer Science II for Majors
Presentation transcript:

COP 3330 Object-oriented Programming in C++ Polymorphism Spring 2019

A Motivational Example At FSU, we may have many sub-categories of Students Undergrads, graduates, online, part-time, …… Thus, with many different types of grade reports for different type of students It would be great to store one aray of students, and print out ALL grade reports with a loop Student list[42000]; for (int j = 0; j < size; j++) list[j].gradeReport();

Problem The items in the array are base-class Student objects Base class objects do not know about subtypes (Grads, Undergrads, or others) Everything in an array needs to be the same type and size The gradeReport() function being called is the Student class version However, we want to call the Grad version for graduate students and Undergrad version for undergraduate students What about creating separate arrays for different student types? Not realistic if there are many subtypes

A Base Class Pointer Property We can get around the first problem through a special property of inheritance Normally, a pointer can only point to one type However, there is a special rule for inheritance A pointer to a base class type can be pointed at an object derived from the base class Similarly, a base class reference variable can refer to an object derived from that base class Examples Student s; Grad g; Undergrad u; Student *sp1, *sp2, *sp3; sp1 = &s; // pointing at Student object sp2 = &g; // pointing at Grad object sp3 = &u; // pointing at Undergrad object

Heterogeneous List A single array of pointers to a base class Example These pointers can point to ANY objects derived from that base Thus, we have created a list of various types of objects Without breaking the array rules! All pointers have the same type—a pointer to the base class All pointers have the same size Example Student *list[42000]; Student s; Grad g; Undergrad u; list[0] = &g; list[1] = &u; list[2] = new Grad; list[3] = &s; …

Virtual Functions The heterogenous list solves the first problem. How about the second problem? Which version of gradeReport() will be run? for (j = 0; j < size; j++) list[j]->gradeReport(); This still calls the Student version of gradeReport due to a concept known as binding list[j].gradeReport is bound to the Student version at compile time Compiler cannot guess the version of objects at runtime If we want to run the correct version at runtime, we need to achieve late (dynamic) binding

Virtual Functions The keyword virtual will do the trick To override a base class function when it is called through a pointer, declare the function to be virtual class Student { public: virtual void gradeReport(); }; Now when gradeReport()is called through a base-class pointer, the program will run the appropriate version (late binding)

Example Student s; Grad g; Undergrad u; Student *sp1, *sp2, *sp3; sp1 = &s; // pointing at Student object sp2 = &g; // pointing at Grad object sp3 = &u; // pointing at Undergrad object sp1->gradeReport(); // runs Student’s version sp2->gradeReport(); // runs Grad’s version sp3->gradeReport(); // runs Undergrad’s version

Example With the heterogeneous list, and the use of virtual functions, we can put all Students in one list, and print all grade reports: for (int i = 0; i < size; i++) list[i]->GradeReport();

Pure Virtual Function Suppose you do not want to do anything for the Student’s version of gradeReport() You can omit the definition virtual void gradeReport()=0; A virtual function without a definition is a pure virtual function has no function definition in .cpp file, only declaration in .h file For instance, class Student might not have enough information for the full grade reports -- we need to know what kind of student (i.e. which subtype)

Abstract Class Any class that has at least one pure virtual function is an abstract class An abstract class cannot be instantiated Abstract classes are generally used as base classes They are intended to be a place to declare data and functions common to classes derived from them Abstract class can still be used to build pointers, to take advantage of virtual functions Example abstract class Shape Shape s; // illegal Shape *sptr; // legal

virtual void printCheck()=0 Code Review: Employee Employee virtual void printCheck()=0 Temporary void printCheck() Permanent void printCheck() = 0 Hourly Salaried

Questions