0% found this document useful (0 votes)
10 views

Lecture Week10-11 Abstract Classes-up2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture Week10-11 Abstract Classes-up2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CS212 – Object Oriented

Programming
Abstract Classes

Dr. Muhammad Sadiq Amin

Courtesy Dr. Khurrhum Shahzad


Outline

• Abstract Classes
– Pure Virtual Function
– Abstract Classes
Pure Virtual Function
• A pure virtual function is a virtual function
whose declaration ends with = 0 e.g.
class Data {
// ...
virtual void function() = 0;
// ...
};
OR
class Data {
// ...
virtual float calculateArea() = 0;
// ...
};

40
Pure Virtual Function
The equal sign here has nothing to do
with assignment; the value 0 is not
assigned to anything.
The =0 syntax is simply how we tell
the compiler that a virtual function will be
pure.

41
Pure Virtual Function
• How can we make it clear to someone
using our family of classes that we don’t
want anyone to instantiate objects of the
base class?
– We could just say this in the documentation, and
count on the users of the class to remember.
• How can we can do that? By placing at
least one pure virtual function in the base
class.
42
Example 8 – Abstract Class

CS212 Object Oriented Programming in


C++
Abstract Class
• The purpose of an abstract class is to provide an
appropriate base class from which other classes can
inherit.
• Abstract classes cannot be used to instantiate objects
and serves only as an interface.
• Attempting to instantiate an object of an abstract
class causes a compilation error.

CS212 Object Oriented Programming in


C++
Example 9- Abstract Class Polygon

45
Example 9- Attempting to instantiate an
object of an abstract class: Output Error
Example 9- Attempting to instantiate
an object of an abstract class: Output
Error
• Now if in main() you attempt to create objects of
class Base, the compiler will complain that you’re
trying to instantiate an object of an abstract class. It
will also tell you the name of the pure virtual
function that makes it an abstract class.

CS212 Object Oriented Programming in


C++
Example
CS212 Object Oriented
Programming
C++

in
Abstract Class
• If a subclass of an Abstract Class needs to be
instantiated, it has to implement each of the virtual
functions
– which means that it supports the interface declared by the ABC.
• Failure to override a pure virtual function in a
derived class, then attempting to instantiate
objects of that class, is a compilation error.
• If we do not override the pure virtual function in
derived class, then derived class also becomes
abstract class.
• Classes that can be used to instantiate objects
are CS212 Object Oriented Programming in
C++
Example 11- Abstract Class
Polygon

CS212 Object Oriented Programming in


C++
Example 11- Failure to override a
pure
virtual function in a derived class
Example 11- Failure to override a pure
virtual function in a derived class
• Once you’ve placed a pure virtual function in the
base class, you must override it in all the derived
classes from which you want to instantiate objects.
• If a class doesn’t override the pure virtual function, it
becomes an abstract class itself, and you can’t
instantiate objects from it (although you might from
classes derived from it).
• For consistency, you may want to make all the
virtual functions in the base class pure.

CS212 Object Oriented Programming in


C++
Example 12: Pure virtual Function
Definition
Example 12 Output

Pure Virtual definition

Implementation of
Virtual Function in
Derived class
Pure Virtual definition

CS212 Object Oriented Programming in


C++
Example 15

Base

Derv1 Derv2

CS212 Object Oriented Programming in


C++
Example 15
CS212 Object Oriented Programming in
C++
Example 15 Using Arrays- Output
Derv1
Derv2

CS212 Object Oriented Programming in


C++
Class Polygon Example 16

CS212 Object Oriented Programming in


C++
Example 16- Abstract Class
Polygon CS212 Object Oriented Programming in
C++
Example 15- Using Arrays Output
20
10

You might also like