Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes."— Presentation transcript:

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 3:00 PM - 6:00 PM Today  Review polymorphism / virtual functions  Abstract classes 3/20/2016 ECE 264: Lecture 19 2

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->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

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 3/20/2016 ECE 264: Lecture 19 4

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 3/20/2016 ECE 264: Lecture 19 5 Shape ThreeDShape RectangleCircle TwoDShape SphereCube

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 ( ) = 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

7 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

8 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

9 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


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

Similar presentations


Ads by Google