C++ Data Types

In C++, there are different ways to store and use information, just like there are different types of containers to store different things.

  1. Basic data types: These include int (integer), float (floating-point number), double (double-precision floating-point number), char (character), and bool (boolean).
int age = 25;
float pi = 3.14;
double bigPi = 3.14159265358979323846;
char letter = 'A';
bool isTrue = true;

in C++ there are several other data types besides int, float, char, and bool. Here are a few examples:

  • double: used to store decimal numbers with a higher precision than float.
  • long: used to store whole numbers with a larger range than int.
  • short: used to store whole numbers with a smaller range than int.
  • unsigned: used to store whole numbers without negative values.
  • string: used to store a sequence of characters, often used to store text.
double pi = 3.14159265358979323846;
long population = 7_843_000_000;
short age = 25;
unsigned int score = 100;
string name = "John Doe";

In this example, we create variables of different types, with different values. “pi” is of type double and it’s used to store a decimal number, “population” is of type long and it’s used to store a large whole number, “age” is of short and it’s used to store a small whole number, “score” is of type unsigned int and it’s used to store a positive whole number, and “name” is of type string and it’s used to store a sequence of characters.

2. Enumerated data types: These are user-defined data types that consist of a set of named values.

enum days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
days today = Monday;

3. Pointers: A pointer is a variable that stores the memory address of another variable.

int x = 5;
int *ptr = &x;
  1. Arrays: An array is a container that holds a fixed number of values of the same type.
int arr[5] = {1, 2, 3, 4, 5};
  1. Strings: C++ has a string class that can be used to store and manipulate strings of characters.
#include <string>
std::string name = "John";
  1. Structures: A structure is a user-defined data type that can hold a collection of related data elements of different types.
struct Person {
    std::string name;
    int age;
};
  1. Classes: A class is a blueprint for an object, and it defines the properties and methods that an object of that class will have.
class Dog {
    public:
        void bark() {
            std::cout << "Woof!\n";
        }
};
  1. Templates: A template is a powerful feature of C++ that allows you to create generic functions and classes that can work with a variety of data types.
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}
  1. Complex data types: These include the complex and valarray classes, which are used to manipulate complex numbers and arrays of numbers, respectively.
#include <complex>
std::complex<double> c1 (1.0, 2.0);
std::complex<float> c2 (3.0, 4.0);
  1. User-defined data types : These include user-defined classes and structures, which allow you to define your own data types and methods for working with them.
  2. C++ Standard Template Library (STL) data types: STL provides a collection of classes and functions that can be used to work with data structures such as lists, stacks, queues, sets, and maps.
#include <vector>
std::vector<int> v;

C++11 and C++14 introduced new data types like auto and decltype, which can be used to automatically deduce the type of a variable or expression.

auto x = 5;
decltype(x) y = 10;

C++17 introduced new data types like optional and variant, which are used to represent values that may or may not be present and to hold a value of one of several types, respectively.

#include <optional>
std::optional<int> x;
x = 5;

This is all for now!

Peace!!