ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.

Slides:



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

ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
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.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
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,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Object Oriented Programming
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Object-Oriented Programming: Inheritance and Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
ECE 264 Object-Oriented Software Development
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
CMSC 202 Polymorphism.
ECE 264 Object-Oriented Software Development
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Jeff West - Quiz Section 12
Object-Oriented Programming: Inheritance and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
COP 3330 Object-oriented Programming in C++
Final Exam Review Inheritance Template Functions and Classes
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
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.
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes

Lecture outline Announcements / reminders  Project Demonstration (Group 1- Group 6), April 25 (Thursday) (Group 7- Group 11),April 30 (Tuesday) mins presentation All group members must present  Exam 3 Thursday, May 9 3:00 PM - 6:00 PM Today  Review polymorphism / virtual functions  Abstract classes 3/20/2016 ECE 264: Lecture 19 2

Polymorphism Polymorphism: Code/operations behave differently in different contexts  One example: operator overloading  Inheritance-based polymorphism in form of virtual functions Virtual functions allow us to write generic code that works for (hopefully) many specific cases  Using pointers to objects allows dynamic binding Base class pointer can be used for derived class object ptr->function() will call version for appropriate class Also useful when  Passing function arguments by reference  Calling member function inside another member function (implicitly using this pointer) Remember, a class with virtual functions should have a virtual destructor 3/20/2016 ECE 264: Lecture 19 3

Abstract classes Virtual functions allow appropriate function calls based on object type  Sometimes base class makes sense; sometimes derived class makes sense May want ability to specify generic class  A number of classes inherit from same base class  Functionality is too general to implement in base class  Never intend to instantiate base class object 3/20/2016 ECE 264: Lecture 19 4

Abstract class example hierarchy All shapes share some characteristics  E.g. functions for drawing, moving All 2-D shapes share characteristics  E.g. point of origin in x-y plane; area function All 3-D shapes share characteristics  E.g. point of origin in x-y-z plane; volume function Each function listed above should be handled in specific manner by derived classes! Classes in red are abstract  Specify desired functionality (pure virtual functions), but have no implementation Classes in black are concrete  Provide specific implementation for pure virtual functions 3/20/2016 ECE 264: Lecture 19 5 Shape ThreeDShape RectangleCircle TwoDShape SphereCube

Abstract classes (cont.) Abstract class: class containing 1+ pure virtual function(s)  Declares general function(s) derived classes should have, but no implementation  Instantiating base class object is illegal  Pure virtual function syntax (in.h file): virtual ( ) = 0; Ex: virtual void draw() = 0; Derived classes must overload pure virtual functions  Failure to do so makes derived class abstract! You can, however, call a pure virtual function  May want default behavior for derived classes 3/20/2016 ECE 264: Lecture 19 6

Abstract class example Given the above hierarchy, assume the following pure virtual functions exist:  Shape::draw();  TwoDShape::area();  ThreeDShape::volume(); Assume classes listed in black have concrete implementations of all appropriate pure virtual functions Which lines below cause errors?  Shape s;  Shape *sPtr, *sPtr2;  sPtr = new TwoDShape;  sPtr2 = new Cube;  sPtr2->draw();  sPtr2->area();  sPtr2->volume(); 3/20/2016 ECE 264: Lecture 19 7 Shape ThreeDShape RectangleCircle TwoDShape SphereCube

Abstract class example solution Which lines below cause errors?  Shape s;  Can’t instantiate abstract class  Shape *sPtr, *sPtr2;  Pointers are OK  sPtr = new TwoDShape;  TwoDShape is abstract  sPtr2 = new Cube;  Cube inherits from Shape  sPtr2->draw();  Function declared in Shape  sPtr2->area();  Func. declared in TwoDShape  sPtr2->volume();  Func. declared in ThreeDShape 3/20/2016 ECE 264: Lecture 19 8 Shape ThreeDShape RectangleCircle TwoDShape SphereCube

Final notes Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 3/20/2016 ECE 264: Lecture 19 9