How to Use C++ Inheritance and Abstract Class with Code Examples

by Koscica Dusko on July 31, 2014

If you are working on any object oriented programming languages (For example, C++), the following are some of the important concepts that you should understand during your learning phase.

Out of the following concepts, in this article, you’ll learn the basics of Inheritance and Abstract class using a sample C++ code snippet and an explanation that goes along with it.

Before we begin, you should understand the relationship between CChild and CParent.

In the basics, a CChild class inherits from the CParent some methods and data. A class, as we know, is made of data and methods. Those methods and data are members of that class.

If we adopt this paradigm, it becomes more logical and way more natural for human thinking. This approach enable us to have a class, that could be instanced to create the object. The object will have its attributes to describe it and methods to do something, preferably reasonable, with that data.

Sometimes, people see hierarchy also and separate it as different term. The CChild is at the lower level of hierarchy than CParent.

At the the top of inheritance chain, there should be one abstract class. You should not try to instantiate abstract class, but you could have the pointer of that abstract class type.

It becomes complicated to design good OOP solutions, and some people use UML as way to cope with some tough tasks, in order to develop good solution that is maintainable, as well as practically applicable. The problem, is how to present your solutions to people that work with you as well.

If you look at a geometrical figure, it could be basic for the square. But geometrical figure could also be basic for a circle. Then you could have: triangles, pentagon, etc. You should broaden your previous work and continue developing additional possibilities.

Let us have an example of square and geometrical figure. In this, geometrical figure will be base class for the derived one. In this case, square is derived from base class which is figure.

You could have three types of inheritance in C++:

  1. Public
  2. Protected
  3. Private

We also spoke about C++ single and multiple inheritance in a previous article.

Public inheritance is the most common. The way to write that would be:

class CParenent

And from that, you create the child as shown below:

class CChild : [public|protected|private] CParent

It is possible to have more than one class as parent class as shown below.

class CChild: public CParent1, public CParent2

This is the case where you have two classes as the parent classes, both are basis to create the class CChild and this time, the both of them are using public inheritance.

It would be possible to have more parents than two and each of those parent classes could be used like base class in: public, private or protected way as well.

In some languages, there is no way you could have two parents, because there could be situations which would create ambiguities that would have no way of determine to which class data belongs.

In C++, you resolve this problem using virtual inheritance.

Also, there are virtual methods and destructors. The virtual methods and destructors are there to solve the problems when you use pointers to parent class to access the child objects.

If you don’t use virtual methods, you would not be able to reach the expected method. Virtual destructor is important when you create objects with operator new, or if you prefer to say in dynamic manner.

We need an abstract class, which is a basis for other classes that get developed from the abstract class.

The class that has a method which has no body is called the abstract one. You could just think of this kind of method in the class, as the method, that has no purpose, other than to crate the address space for a member of class being reached indirectly over the pointer, that is of type on higher hierarchy. This is needed when you have pointer that is of CParent type and it is used to access the method from CChild class.

The appropriate way to write this is shown below:

virtual returnDataType SomeMethod(inputDataTypes)=0;

You could not instantiate the abstract class, and it takes just one method with no body to become an abstract class.

Problem Definition

Create the class CFigure with completely virtual methods as the base for class CSquare. We will create squares and pointer of CFigure type to access the methods of a CSquare. The object CFigure will never be created, but you will use pointer of CFigure and instance it with some arguments.

C++ Code for the Problem

The following C++ code is the answer to the above problem definition:

#include <iostream>

using namespace std;

class CFigure
double dSide;
	CFigure(double a){dSide=a;};
	virtual       ~CFigure(){};
	virtual double Surface(void)const=0;
	virtual double Circumference(void)const=0;
	virtual double Side(void)const=0;

class CSquare: public CFigure
	       CSquare(double a):CFigure(a){};
	double Surface(void)const{ return dSide*dSide;}
	double Circumference(void)const{ return 4.0*dSide;}
	double Side(void)const{return dSide;}

CSquare Square_1(1);


CFigure* ptrFigure = new CSquare(2);

delete ptrFigure;


Code Analysis

First class is: CFigure.

It is an example of an abstract class. This means that you will not be able to create the objects from this one, but you will have chance to create pointers off this class, and because it is the base class for class CSquare or any other class in exercise 1, it will be possible to connect that pointer of CFigure type, to any object that is inherited from CFigure.

This class has few virtual methods to overcome the problems that will arise from the fact that we are using pointers to indirectly access the members of the class.

The dSide is protected data and this means it will be visible in all inherited classes beside that class where it gets mentioned first time.

There is a virtual destructor as well, it is necessary to have this destructor when we dill with pointers and create the class with the new operator.

I have not used arrays in this case but I could also show how to use them in case we need the array of some objects, or some other data structure. But the vectors are way more appropriate choice to organize more objects, in case you have a situation that requires handling larger numbers of objects.

Class CSquare is publicly inherited from class CFigure, and it will implement those methods that have no body in its base class. They are not hard to understand, that is the reason why I will not go into the details of how they work.

In the main program, we have first object Square_1 with argument 1. This means that we are instancing one object with argument 1.

After that we calculate: Surface and Circumference of the given Square.

The calculated values are presented on the monitor to the programmer.

If you need to figure out the value of side you could call method with adequate name. In this program I have not use this method, but it would be recommended for a reader to use this one as well. It would be an additional exercise for you.

Next thing, we have done is bit different, we have created pointer of base class and connect it with CSquare that has argument equal to 2.

Because child class has more data than parent class it will be possible to lose some data that is not necessary, but other way could be bit tough. You could use conversions but this is not a recommended practice in this case.

Then we indirectly access the methods of an object and present them to the user.
There is just one more thing left to be done. Now you could use operator delete on pointer ptrFigure to clear the memory that you don’t need any more.

Additional Exercises

1.Create the following class:

  • Line segment
  • Triangle
  • Circle

2.Create abstract class CAnimal. This class will be base class for: CDog and CCat. If you like more, you could create the Zoo, as well.

3. Create a class CResistor as the basis for classes:

  • Tree strip resistor
  • Four strip resistor
  • Five strip resistor
  • Six strip resistor

4. Create the class CPoint that will be used as the base class for classes: CLine, CTraingle and CPoliline.

5. Create the class CUser that will serve as the base for:

  • Banking account class
  • EMail account class
  • Library member
  • Club member

6. Create class CVehice that will be base class for different classes of vehicles.

7. You are presented with a big cup K, of certain volume that is natural number. There are also two more cups: A and B, which have a volume that is also natural number. The volumes, of two cups that are used to fill bigger cup K are: A, and B. Volume of B is smaller than A, and volume of A is smaller than volume of cup K. Your task is to:

  • Figure out, is it possible to fill cup K with two cups: A and B without any spilling of the liquid and just enough liquid to reach a rim of a cup K.
  • You are provided with enough liquid to fill cups A or B in order to fill bigger cup K.
  • Find in how many ways it is possible to fill big cup K with small cups: A and B.
  • Find the solution that will prefer cup A, where cup A is bigger than cup B.
  • Find the solution that will prefer cup B, where cup B is smaller than cup A.
  • Solve the problem if you have tree cups.
  • Solve the problem if you have n cups.

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 4 comments… read them below or add one }

1 MPT September 27, 2014 at 4:32 am

Ok, now I understand how to avoid casts that are not needed!

2 Pratik October 14, 2014 at 9:48 pm

Hi Ramesh, there is a Typo in this article
You could have tree(three) types of inheritance in C++:


3 Ramesh Natarajan October 20, 2014 at 2:56 pm

@Pratik, Thanks for pointing it out. It is fixed.

4 DuskoKoscica October 25, 2014 at 1:47 am

@THX to Pratik for finding it, and that way improve the article.

@THX to Ramesh for fixing it, so that error is not there. Now, I feel like saying a joke but I will restrain my self from say it.

Leave a Comment

Previous post:

Next post: