C++ Vectors

C++ Vector

A vector in C++ is like a resizable array.

Both vectors and arrays are data structures used to store multiple elements of the same data type.

The difference between an array and a vector, is that the size of an array cannot be modified (you cannot add or remove elements from an array). A vector however, can grow or shrink in size as needed.

To use a vector, you have to include the <vector> header file:

// Include the vector library
#include <vector>

Continue reading C++ Vectors

C++ Data Structures and STL

Data Structures

Data structures are used to store and organize data. An array is an example of a data structure, which allows multiple elements to be stored in a single variable.

C++ includes many other data structures as well, each is used to handle data in different ways.

These are part of the C++ STL, which stands for The Standard Template Library.

Continue reading C++ Data Structures and STL

C++ Date and Time

Date and Time

The <ctime> library allows us to work with dates and times.

To use it, you must import the <ctime> header file:

Example

#include <ctime> // Import the ctime library

Continue reading C++ Date and Time

C++ Exceptions

C++ Exceptions

When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).

Continue reading C++ Exceptions

C++ Files

C++ Files

The fstream library allows us to work with files.

To use the fstream library, include both the standard <iostream> AND the <fstream> header file: Continue reading C++ Files

C++ Polymorphism

Polymorphism

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. Continue reading C++ Polymorphism

C++ Inheritance Access

Access Specifiers

You learned from the Access Specifiers chapter that there are three specifiers available in C++. Until now, we have only used public (members of a class are accessible from outside the class) and private (members can only be accessed within the class). The third specifier, protected, is similar to private, but it can also be accessed in the inherited class: Continue reading C++ Inheritance Access

C++ Multiple Inheritance

Multiple Inheritance

A class can also be derived from more than one base class, using a comma-separated list: Continue reading C++ Multiple Inheritance

C++ Multilevel Inheritance

Multilevel Inheritance

A class can also be derived from one class, which is already derived from another class.

In the following example, MyGrandChild is derived from class MyChild (which is derived from MyClass). Continue reading C++ Multilevel Inheritance

C++ Inheritance

Inheritance

In C++, it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” into two categories:

  • derived class (child) – the class that inherits from another class
  • base class (parent) – the class being inherited from

To inherit from a class, use the : symbol.

In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class (parent): Continue reading C++ Inheritance