Tag Archives: C++

VARS, DECLARING CONSTANTS

#37 Using Variables, Declaring Constants

Variables are tools which help programmers temporarily store data for a certain amount of time. Constants are tools which help programmers to define artifacts that are not allowed to change or make changes.

To understand variables we have to first take a look at memory addressing in brief. All computers contain a MCU and a certain amount of temporary memory called RAM In addition may devices also contains a permanent storage memory called ROM. The MCU executes the program and in doing so it utilizes the RAM to fetch binary code to be executed as well as the data associated with it.

RAM uses addresses which allow us to point to blocks of memory stored within itself.

When programming in C++ we define variables to store those variables. Below are two examples:

VariableType VariableName;
e.g int number;

VariableType VariableName = InitialValue;
e.g int number = 0;

The variable type attribute tells the compiler the nature of data the variable can store.

Below is an example of declaring and initializing multiple variables of a type.

int first = 0, second = 1, result = 16;

Data stored in variables is stored in RAM. This data will be lost once the application terminates unless the program is told to save the data to a storage medium such as a hard disk.

If variables are declared outside the scope of the function function() instead of with in it, these variables will be considered global variables.

Variable types:
bool
char
unsigned short int
short int
unsigned long int
long int
unsigned long long
long long
int(16 bit)
int(32 bit)
unsigned int(16 bit)
unsigned int(32 bit)
float
double

C++ allows us to substitute variable types to something that the programmer may find a bit more convenient . The keyword typedef can be used for this functionality. Below is an example where the programmer wants to call an unsigned int a descriptive STRICTLY_POSITIVE_INTEGER.

typedef unsigned int STRICTLY_POSITIVE_INTEGER;
STRICTLY_POSITIVE_INTEGER exampleNumber = 6570;

Constants are like variables in C++ except their value can’t be changed. Constants also occupy space in memory just like variables however this space cannot be overwritten.

Constants in C++ can be:

  • Literal constants.
  • Declared constants using the const keyword.
  • Constant expressions using the constexpr keyword.
  • Enumerated constants using the enum keyword.
  • Defined constants that are not recommended and deprecated.

Enumerations comprise a set of constants called enumerators. This is used when you need a type of variable whose values are restricted to a certain set defined by the programmer..

Below is an example of using Enumerations:

Defining constants using #define
#define pi 3.14
#define is a preprocessor macro. So once defined all mentions of pi henceforth will be replaced by 3.14 This is a text replacement.

ANATOMY OF A C++ PROGRAM

#36 ANATOMY OF A C++ PROGRAM

C++ programs are organised into classes comprising of member functions and member variables. A simple HelloWorld.cpp like below can be classified into two parts: the preprocessor directives which start with # and the main program that starts with int main()

A preprocessor is a tool that runs before the actual compilation starts. Preprocessor directives are commands to the preprocessor that always start with the # symbol. A custom header created by the programmer will be encapsulated by quotes "..." instead of brackets <...> which are typically used when including standard headers.

Custom: #include "...relative path to file\file.h"

Typical: #include <file.h>

After the preprocessor directive(s) is the body of the program characterized by the function main(). It’s a standard convention that function main() is declared with an int preceding it. int is the return value type.

Functions in C++ need to return a value unless specifically specified otherwise. In main() an int is always returned since it is indeed a function. This is very useful since it provides the ability to query on the returned value of the application. A typical success return value is 0 and in the event of an error -1 is returned.

Namespaces are names given to parts of code in order to reduce naming conflicts. By invoking std::cout, you are telling the compiler to use that one unique cout that is available in the std namespace.

C++ has two comment styles:


// indicates the start of a line and is valid to the end of that line.


/* followed by */ indicates the contained text is a comment.

Functions enable you to divide the content of your application into functional units that can be invoked in a sequence of your choosing. In a C++ console application main() is the starting function of your application.

Example of using functions in C++:

GETTING STARTED C++

#34 GETTING STARTED C++

Download visual studio 2019 from here. Once you have installed it be certain to install the C++ components. Once completed start visual studio and select the C++ console application option. Double click on the source files folder and then write your code.

Select The C++ Components

Here is a first C++ application example I have written. All it does is print the text Hello World and displayed some simple arithmetic in the cmd console.

Useful links can be found here

A SHORT HISTORY OF C++

#33 A SHORT HISTORY OF C++

C++ was developed by Bjarne Stroustrup in 1979 it was designed to be an object-oriented language. C++ implements features such as inheritance, abstraction, polymorphism and encapsulation.

Bjarne Stroustrup

C++ is an intermediate-level programming language. It allows high-level and low-level programming. Although there’s a whole bunch of newer languages, C++ is still relevant where accurate control of application’s resource consumption and performance is needed. Before 1998 C++ was being accepted and adopted on so many different platforms that many interoperability problems and porting issues surfaced. Therefore in 1998 the first standard version of C++ was ratified by the ISO Committee in ISO/IEC 14882:1998