Learn about Abstract Classes and Virtual Functions in C++

0
1546

C++Abstract-Classes-and-Virtual

In the previous article, we were  introduced to Inheritance. At the end of that article, I gave you a hint about Abstract Classes. I told you that an Abstract Class is a class that you can’t instantiate (create objects from), but are a sort of classes that serve as a base to derive other child classes from. For a class to be made abstract, it should have at least one pure virtual function.  Our focus today will be on Abstract Classes and Pure Virtual Functions. Happy reading.

Pure Virtual Functions
A pure virtual function is declared inside a base class, and “must” be implemented (overridden) by any non-abstract derived class.

Consider the following class definition:

This is an abstract class.

Having the keyword virtual in the function declaration makes the function behave() like a virtual function. The trailing  = 0 part makes it pure virtual, and causes the class to be abstract.

Assume you don’t believe me, and want to make sure class A can never be instantiated (as I claim). OK, so simple; all you need to do is to try to create objects of class A:

If you compile the above code, you will get an error like the following:
1

The message shows clearly that you can’t declare variable (object) objecta to be of the type (class) A, as it is an abstract class.

Okay, let’s inherit that abstract class by a concrete (non-abstract) one:

Now, we need a main function to try this derived class:

Executing this code should give the following output:
2

Good job!

Public and Private Inheritance
One thing that we have passed by without clarification is the role of access specifier in the derived class definition line:

The word public here specifies that public member functions in the base class A are accessible to objects of class B. On the other hand, if we say:

This will prevent objects of class B from accessing public member functions in class A.

Inheriting from Multiple Classes
Unlike some other languages (Java) that don’t support multiple inheritance, C++ o allows a child class to inherit from more than one parent class.

Consider for example the circle class. A circle has a center (point) and at the same time is considered as a shape that has a perimeter and an area.

This means a circle actually inherits features from both point and shape classes. Let’s see how we can achieve this in C++ code.

How will this program execute? Let’s see:
3

Wonderful.

Aggregation (Composition)
Why to inherit a class (or more), if you could go ahead and use it (them) directly?!

In other words, class B inherits class A to make use of all features in class A. Couldn’t we do it by defining an object of class A inside class B?

Yes, we could. And this presents a simple alternative to multiple inheritance.
Consider the following code:

Instead of inheriting ClassA and ClassB, the class ClassC has defined and used instances of both classes. When executed, the above program produces the following output:
4

Perfect!

Summary

  • An abstract class is a class that you can’t instantiate. It serves as a base that derived classes can inherit.
  • A class is considered abstract if it contains one or more pure virtual functions.
  • A class can inherit from multiple classes the same way it inherits from one class.
  • In Aggregation (composition), for a class B to utilize features in another class A, class B uses an object of class A, instead of inheriting class A.

In the next article, we will be talking about Pointers; the enigma of C++. An important part of the C++. See you there.

LEAVE A REPLY

Please enter your comment!
Please enter your name here