Title: Abstract Base Classes
1Abstract Base Classes
2Objectives
- Polymorphism
- Abstract Base Classes
- Virtual Functions
3Base and Derived Pointers
vehicle
truck
car
motorcycle
- Base class pointers can point to derived class
objects - E.g., all trucks are vehicles, so a vehicle
pointer can point to a truck. - Derived class pointers cannot point to base class
objects - Not all vehicles are trucks, so a truck pointer
cannot point to a vehicle.
4Base and Derived Pointers
class truck public vehicle protected int
capacity class car public vehicle
protected int numPassengers class
bicycle public vehicle protected int
serialNum
class vehicle protected char type50
5Base and Derived Pointers 2
void main() vehicle v bicycle b
truck t new truck() v t // b v
(WRONG) car c new car() v new
bicycle() delete t delete c delete
v
6Polymorphism
- Method to allow programs to generically process
objects - Dont have to know all about derived classes in
advance - Good for modular reusable code
- Generic linked lists, a queue of different types
of people, etc.
7Object Polymorphism
- A method expecting a Car object can only accept
Car objects - A method expecting Vehicle objects can accept any
object derived from the Vehicle class - Car
- Truck
- Bus
- Theres also function polymorphism
8Polymorphism 2
- Normally, when an object or pointer to an object
calls a function, it does so statically - I.e., determined at compile time
- Another way is to allow for it to be DYNAMIC, and
determined at run-time - This allows you to treat objects generically,
provided they have the same interface - This is called polymorphism
9Virtual Functions
- How do we do this? Base classes with virtual
functions. - When a virtual function is called, the computer
determines at runtime the appropriate derived
function to execute. - The base class has the virtual functions
- New derived classes can be added that implement
their own versions of these functions - Override the base virtual functions in derived
classes!
10Virtual Functions Example
class student public person protected int
studentID public student(int id) void
print() class teacher public person
protected int deptID public
teacher(int d) void print()
class person protected char name char
phone public person(char n, char p)
virtual void print()
11Virtual Functions Example 2
void personprint() cout ltlt name ltlt ltlt
phone ltlt endl void studentprint() cout
ltlt Student ltlt endl cout ltlt ID ltlt
studentID ltlt endl cout ltlt name ltlt endl ltlt
phone ltlt endl void teacherprint() cout
ltlt Teacher ltlt endl cout ltlt Dept ltlt
deptID ltlt endl cout ltlt name ltlt endl ltlt phone
ltlt endl
12Virtual Functions Example 3
void main() int x person array new
person3 array0 new student(Joe,
555-1234, 3976) array1 new
teacher(Daisy, 555-8909, 203) array2
new person(Mandy, 555-3782) for (x 0
x lt 3 x) arrayx-gtprint()
13More Virtual Functions
- Without virtual functions, only the base class
print() function would be called - This is because all the pointers are of the base
type - WITH virtual functions, the correct print
function is called for each member of the array - array0-gtprint() calls studentprint()
- array1-gtprint() calls teacherprint()
- array2-gtprint() calls personprint()
14What does this mean?
- It means that you dont necessarily have to know
the EXACT type of the object youre dealing with - You only have to know its base type
- Base classes base class interfaces written by
someone else long ago can work with new classes
you write
15Abstract Base Classes
- Sometimes you will NEVER WANT TO instantiate an
object of the base class. - E.g., doesnt make sense to make a shape object
- Only a square object, a circle object, or some
other derived object actually makes sense - Can make the class abstract so that future
programmers cannot create a base class object
either
16Pure Virtual Functions
- How do you make a base class abstract?
- Make at least one of its member functions PURE
VIRTUAL - Functions should be pure virtual if information
is needed from the derived class for any valid
implementation - a shapes area function depends on what type of
shape it is
17Pure Virtual Functions 2
- Pure virtual functions are defined in the class
declaration by - So for the shape area function example, wed have
in the shape class - There is NO IMPLEMENTATION of pure virtual
functions in the base class, just the declaration
virtual returnType functionName(arglist) const
0
virtual float area() const 0
18Virtual Destructors
- Why should destructors be virtual if a base class
has any virtual functions? - Derived objects will likely be accessed by base
pointers - If you delete a base pointer without a virtual
destructor, it will only call the base
destructor - If virtual, however, it will call the correct
derived destructor, then the base destructor
19Example
- include ltiostream.hgt
- class base
- public
- virtual void vfunc() coutltltThis is the base
vfunc().\n -
- class derived1 public base
- public
- virtual void vfunc() coutltltThis is a derived
function\n -
- class derived2 public base
- public
- virtual void vfunc() coutltltSo is this
one.\n
20Example in Action
- int main()
- base p
- base b
- derived1 d1
- derived2 d2
-
- pb
- p-gtvfunc()
- pd1
- p-gtvfunc()
- pd2
- p-gtvfunc()
A pointer to the parent class
OUTPUT This is a base vfunc(). This is a
derived function. So is this one.
21END
22Other Examples of Polymorphism
- Queue of jobs to do
- Homework
- Cleaning
- Cooking
- Linked list of classes
- Lectures
- Labs
- Discussion sections
231. Virtual Functions
- A virtual function is a member function that is
declared within a base class and redefined by a
derived class. - Its looks just like a normal function but has the
keyword virtual preceding it. - So the virtual function defines the form of the
interface. - The derived class redefines it and implements its
operation as it relates specifically to it.
24Why Virtual Functions are Useful
- So you can just use virtual functions as you
would any other function. - However what makes them so implortant is how they
behave when accessed by a pointer. - When a base-class pointer points to a derived
object that contains a virtual function, C
decides which version of that function to call
based on the type of object pointed to. - This is all processed at run-time.
25Abstract Classes
- A class that contains at least one pure virtual
function is said to be abstract it has a
virtual function in it that has no definition at
all. - Because of this, no objects of the abstract class
can be created. - It is an incomplete type a foundation for
derived classes. - However you can still create a pointer to an
abstract class.
26Polymorphism
- An object can take many forms.
- Method overloading is one type of polymorphism.
- Object polymorphism treats specialized objects as
if they are instances of a more general type.
27Factoring to the Base Class
- Factor common characteristics of multiple
classes up into a base class. - HWall and VWall classes have similar
characteristics and methods. - Factor commonalities to a Wall class
- Override some methods to specialize the HWall and
VWall classes
28Abstract Base Classes
- The classes describe what all derived classes
have in common. - Not instantiated (causes an exception)
- Use the abstract keyword when defining abstract
base classes and methods
29Abstract Methods
- Abstract methods are not implemented.
- Derived classes must provide method
implementation.