Logo

Abstraction

Abstraction is the process of considering something independently of its associations, attributes, or concrete accompaniments. In our world (i.e. computer science) abstraction means displaying only essential information and hiding the details.

Back in the day it meant hiding details in files, one file for each "abstraction" or package. The hidden stuff was not made globally known (languages did that differently). The stuff that needed to be known was made known globally. This is not the best solution to hiding things but it just had to do. Some languages allowed declarations that could be "included" into another file.

Now we have header files that can include declarations of everything we need to know to call a function. Variables must have the correct type or be converted to the correct type in every function call and in the return value. It is easy to separate the declarations in one file (called a "header" file) from the definitions in another file (typically called the body). Only those things that need to made visible are visible.

In "C" the modifier "static" is used to hide data and functions in a file. In "C++" the class provides the perfect way to house an abstraction.

So what exactly is an abstraction? The example that most people use is: "Bob". Bob is an example of a class called "Human" or maybe "Person" or perhaps "Animal". A Human has attributes, for example: arms, legs, head, eyes, etc. Some of the attributes are hidden, for example: stomach, liver, brain. A human may be called on to perform a task, for example: Drink Water, Lift Bale, Run Away.

Some abstractions do not lend themselves to easy descriptions and are merely a collection of related functions. I don't get carried away with Object Oriented Programming paradigm but I do make sure to hide all unnecessary details from the outside world.

Header File Organization

A header file must have a few components. After they are in place the rest of the file is up to the purpose of the file. Here is the template that I use every time (This file is named IdentityInfo.h):

// Identity Info Class
// rrvt Copyright, all rights reserved
#pragma once
#include "RequiredHeaderFile.h"

The first line describe the "abstraction" in a very high level (i.e. one line if possible). The second line has any identity information, copyright data. Following the second line one could put an elaborate description of the purpose, interface and other details. I usually don't do that since I would never read it and during development it would needed to be updated and I would probably not get around to it.

The "#pragma once" line is essential so the compiler does choke on some declarations. Initially no include files lines are include in a header file. However, if a decleration in the first header file requires a second header it should be included. Then no matter where the first header file is included all the declerations it needs are included. I include any required system header files after all of my project's header files. This removes the possibilty of unintended declerations for the other header files. This avoids an order dependency in the include file declarations.

There is one other little declaration that should be mentioned somewhere. If the class and/or a function needs a reference or pointer to another object then there is a little trick that avoids conflicting inclusion of header files. Essentally a reference and pointer only needs the name and language element in the header file. Then the header file of the element is included in the body file to allow using the element. Here is an example:

// Identity Info Class
// rrvt Copyright, all rights reserved
#pragma once
class AnotherClass;
class MyClass {
AnotherClass& anotherClass;
public:
  MyClass(AnotherClass& anClass) : anotherClass(anClass) { }
    o o o

Body File

The body file contains all the hidden stuff. The header file contains the public stuff. But there is potential problem. Both files should compile to represent the same interface. There is one way to ensure that happens. Here is my solution to this (This file is named IdentityInfo.cpp):

// Identity Info Class
// rrvt Copyright, all rights reserved
#include "stdafx.h"   // pch.h is the new stdafx.h
#include "IdentityInfo.h"

The first two lines are copied out of the header file (or vice versa). Any other header documentation can be copied here (or not). The first include line must be the stdafx.h (now pch.h) by order of Visual Studio. Only one exception and that is when the option for precompiled header files is turned off. The second include line should be the header file that corresponds to the body file. The should have the same main name (i.e. the main name is the part of the name before the period). This will ensure that the the header and body file compile the same way.

Any other header files are placed after the header file that corresponds to the body. I usually place them in alphabetical order for convenience. Following that any required system header files may be placed. I place the system header files last so that there is no inadvertant declarations for the other header files. This avoids an order dependency in the include file declarations.

by the way, I've discovered that for my small programs, turning off the precompiled header file option makes the compilation less subject to the whims of the compiler when there are frequent changes in the source. I am not sure why that is the case, but the compiler seemed to get tripped up at times.