ECE 264 Object-Oriented Software Development - PowerPoint PPT Presentation

Loading...

PPT – ECE 264 Object-Oriented Software Development PowerPoint presentation | free to download - id: 7b7452-MTk4N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

ECE 264 Object-Oriented Software Development

Description:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes Lecture outline Announcements / reminders Project ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 10
Provided by: gei97
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: ECE 264 Object-Oriented Software Development


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

2
Lecture outline
  • Announcements / reminders
  • Project Demonstration
  • (Group 1- Group 6), April 25 (Thursday)
  • (Group 7- Group 11),April 30 (Tuesday)
  • 10 -15 mins presentation
  • All group members must present
  • Exam 3
  • Thursday, May 9 300 PM - 600 PM
  • Today
  • Review polymorphism / virtual functions
  • Abstract classes

3
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-gtfunction() 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

4
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

5
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

6
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 ltreturn_typegt ltfnamegt(ltargsgt) 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

7
Abstract class example
  • Given the above hierarchy, assume the following
    pure virtual functions exist
  • Shapedraw()
  • TwoDShapearea()
  • ThreeDShapevolume()
  • 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-gtdraw()
  • sPtr2-gtarea()
  • sPtr2-gtvolume()

8
Abstract class example solution
  • Which lines below cause errors?
  • Shape s ? Cant instantiate abstract class
  • Shape sPtr, sPtr2 ? Pointers are OK
  • sPtr new TwoDShape ? TwoDShape is abstract
  • sPtr2 new Cube ? Cube inherits from Shape
  • sPtr2-gtdraw() ? Function declared in Shape
  • sPtr2-gtarea() ? Func. declared in TwoDShape
  • sPtr2-gtvolume() ? Func. declared in ThreeDShape

9
Final notes
  • Acknowledgements this lecture borrows heavily
    from lecture slides provided with the following
    texts
  • Deitel Deitel, C How to Program, 8th ed.
  • Etter Ingber, Engineering Problem Solving with
    C, 2nd ed.
About PowerShow.com