Abstraction is the concept of focusing on the key elements of an item rather than the unimportant (i.e. pulling out [abstracting] the key features), whilst also hiding the way it's implemented.


For example, we all know a motorbike is made up of many objects, but essentially they all have two wheels, a frame, an engine, handle bars and a seat - this is abstracting the main aspects of a motorbike. We don't particularly care how it works internally, as long as it does. We can then refine any particular instance of an object according to the specifics required, i.e. by way of defining sub-classes from super classes that add specific details required for the sub-class object.


Similarly, we can apply this concept to programming to create generic Abstract Data Types (or Abstract Base Classes) and call upon its methods for a specific object according to the (data) type of object.


This allows focus on the "What" not the "How".


The classic example is that of a polygon. We know that it is a shape that has sides and an area. We can declare some abstract information of the shape such as its height and width, and then further refine the shape according to the sub-class to define its area:

#include <iostream>
#include <string>
using namespace std;

class Shape{
	protected :
		int height, width;
		void set(int, int) ;
} ;

class Rectangle : public Shape {
		int area() {
			return (height * width) ;
} ;

class Triangle : public Shape {
	int area() {
		return (height * width) / 2 ;
} ;

void Shape::set(int x, int y){
	height = x ;
	width = y ;

int main() {

	Rectangle myRect ;
	myRect.set(4,5) ;
	cout << "Rectangle has an area of: " << myRect.area() << endl ;

	Triangle myTri ;
	myTri.set(4,5) ;

	cout << "Triangle has an area of: " << myTri.area() << endl ;

	return 0;

Compile & Run:

Rectangle has an area of: 20
Triangle has an area of: 10

Leave a Reply