C++ Tutorial – Namespaces and anonymous namespaces

Namespaces are used in the C++ programming language to create a separate region for a group of variables, functions and classes etc. Namespaces are needed because there can be many functions, variables for classes in one program and they can conflict with the existing names of variables, functions and classes. C++ uses namespace to avoid the conflicts.

The concept can be depicted using the following diagram: More »

Posted in C++ Tutorials | 7 Comments

File IO in C++ (text and binary files) part II

In the first C++ programming tutorial on file IO we looked at the theory behind file IO.

In this second C++ programming tutorial on file IO we will look at some examples. More »

Posted in C++ Tutorials | 6 Comments

File IO in C++ (text and binary files)

This C++ programming language tutorial will be in two parts. The first (this one) will cover the theory behind IO and in the second tutorial we will look at some examples. More »

Posted in C++ Tutorials | 4 Comments

C++ Classes – Constructors and destructors

In the last C++ tutorial we looked at the basic concept of classes. In this C++ programming tutorial we take another look at classes.

Constructor and Destructor

Classes can have complicated internal structures, so object initialization and clean-up of a class is much more complicated then for any other data structures. Constructors and destructors are special member functions of classes that are used to construct and destroy class objects. The construction can be for example: initialization for objects or memory allocation. The destruction may involve de-allocation of memory or other clean-up for objects. More »

Posted in C++ Tutorials | 8 Comments

C++ Classes

In this C++ programming tutorial we take a look at classes. A class is a mechanism for creating user-defined data types. It is similar to the C language structure data type.

A class can not only hold data variables, but can also hold functions. These variables and functions are members of a class. The variables are called data members and functions are called member functions. More »

Posted in C++ Tutorials | Comments Off on C++ Classes

C++ structures, typedef and unions

Structures are used to group together different data elements (types of variables) under the same name. These data elements, known as members, can have different types and different lengths.

Take look at the syntax of a structure: More »

Posted in C++ Tutorials | 2 Comments

C++ Dynamic Memory

In this C++ programming tutorial we will take a look at dynamic memory. Until now the use of memory was determined by the size of the variable we declared. With the use of dynamic memory we can ask for a piece of memory at runtime. In C++ we can use the operators new and delete to get memory and release the memory.

new and new[] operators

With the operator new we can request a piece of memory. A data type must follow the new operator. It is also possible to request more than one element by using brackets []. The result of a new operation is a pointer to the beginning of the memory block (if the request is accepted). More »

Posted in C++ Tutorials | Comments Off on C++ Dynamic Memory

C++ pointers – reference and dereference operators

In this tutorial we take a look at pointers. For most people it will take some time to fully understand pointers.
So you have to be patient and try to make some little programs on your own. Once you master the use of pointers, you will use them everywhere. So get ready this is a long one!

Pointers basics

To better understand pointers, it sometimes helps to compare a “normal variable” with a pointer.

When a “normal variable” is declared, memory is claimed for that variable. Let’s say you declare an integer variable MYVAR. Four bytes of memory is set aside for that variable. The location in memory is known by the name MYVAR. At the machine level that location has a memory address.

A pointer differs in the way that a pointer is a variable that points to another variable. A pointer holds the memory address of that variable. That variable contains a value. Pointers are also called address variables because they contain the addresses of other variables. More »

Posted in C++ Tutorials | 14 Comments

C++ Character sequence using arrays

In this tutorial we take a look at character sequences using arrays.

We already know that the C++ Standard Library (STL) implements a powerful string class. The string class can be used to manipulate strings of characters. But it is also possible to use plain arrays of char elements to represent a string. (Strings are in fact a sequence of characters.)

We can do the following: More »

Posted in C++ Tutorials | 1 Comment

C++ arrays, arrays and loops

In this tutorial, we are going to talk about arrays.

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this: More »

Posted in C++ Tutorials | 17 Comments