Data Types in C++

Data types are fundamental to any programming language, and C++ provides a wide range of data types to work with. These data types define the kind of data that variables can hold and the operations that can be performed on them.

In C++, data types can be categorized into three main groups:

  1. Primitive/Built-in Data Types
  2. Derived Data Types
  3. User-Defined Data Types

Primitive/Built-in Data Types

C++ provides various primitive data types, and here are some of the commonly used ones along with their typical ranges:

  1. Integer Types:
    • int: Represents a signed integer, typically 32 bits. Range: -2,147,483,648 to 2,147,483,647.
    • short: Represents a signed short integer, typically 16 bits. Range: -32,768 to 32,767.
    • long: Represents a signed long integer, typically 32 bits. Range is the same as int.
    • long long: Represents a signed long long integer, typically 64 bits. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
    • unsigned int: Represents an unsigned integer, typically 32 bits. Range: 0 to 4,294,967,295.
    • unsigned short: Represents an unsigned short integer, typically 16 bits. Range: 0 to 65,535.
    • unsigned long: Represents an unsigned long integer, typically 32 bits. Range is the same as unsigned int.
    • unsigned long long: Represents an unsigned long long integer, typically 64 bits. Range: 0 to 18,446,744,073,709,551,615.
  2. Floating-Point Types:
    • float: Represents a single-precision floating-point number, typically 32 bits.
    • double: Represents a double-precision floating-point number, typically 64 bits.
    • long double: Represents an extended-precision floating-point number, the size may vary depending on the platform.
  3. Character Types:
    • char: Represents a single character, typically 8 bits. Range depends on the character encoding.
    • signed char: Represents a signed character, typically 8 bits. Range is the same as char.
    • unsigned char: Represents an unsigned character, typically 8 bits. Range is the same as char.
  4. Boolean Type:
    • bool: Represents a Boolean value, which can be either true or false.
  5. Void Type:
    • void: Used to indicate that a function returns no value.
  6. Size Types:
    • size_t: Represents an unsigned integer type used for storing the size of objects in memory. The size depends on the platform (e.g., 32 bits or 64 bits).
  7. Wider Character Types:
    • wchar_t: Represents wide characters, used for working with multibyte character sets. Size and range depend on the platform.
    • char16_t: Represents a 16-bit character, suitable for Unicode character encoding.
    • char32_t: Represents a 32-bit character, also used for Unicode.

Example:

Here's a C++ program that demonstrates the use of the primitive data types:

#include <iostream>
#include <limits>

int main() {
    // Integer Types
    int integerVar = 42;
    short shortVar = 12345;
    long longVar = 1234567890;
    long long longLongVar = 1234567890123456789;
    unsigned int unsignedIntVar = 4294967295; // Maximum value for unsigned int
    unsigned short unsignedShortVar = 65535; // Maximum value for unsigned short
    unsigned long unsignedLongVar = 4294967295; // Maximum value for unsigned long
    unsigned long long unsignedLongLongVar = 18446744073709551615ULL; // Maximum value for unsigned long long

    // Floating-Point Types
    float floatVar = 3.14159f;
    double doubleVar = 3.14159265359;
    long double longDoubleVar = 3.14159265358979323846L;

    // Character Types
    char charVar = 'A';
    signed char signedCharVar = -127; // Signed char can represent negative values
    unsigned char unsignedCharVar = 255; // Maximum value for unsigned char

    // Boolean Type
    bool boolVar = true;

    // Size Types
    size_t sizeVar = sizeof(int); // Represents the size in bytes

    // Wider Character Types
    wchar_t wideCharVar = L'Ω'; // Represents a wide character
    char16_t char16Var = u'Ω'; // Represents a 16-bit Unicode character
    char32_t char32Var = U'Ω'; // Represents a 32-bit Unicode character

    // Display the values and sizes of these variables
    std::cout << "Integer Types:" << std::endl;
    std::cout << "int: " << integerVar << " (Size: " << sizeof(int) << " bytes)" << std::endl;
    std::cout << "short: " << shortVar << " (Size: " << sizeof(short) << " bytes)" << std::endl;
    std::cout << "long: " << longVar << " (Size: " << sizeof(long) << " bytes)" << std::endl;
    std::cout << "long long: " << longLongVar << " (Size: " << sizeof(long long) << " bytes)" << std::endl;
    std::cout << "unsigned int: " << unsignedIntVar << " (Size: " << sizeof(unsigned int) << " bytes)" << std::endl;
    std::cout << "unsigned short: " << unsignedShortVar << " (Size: " << sizeof(unsigned short) << " bytes)" << std::endl;
    std::cout << "unsigned long: " << unsignedLongVar << " (Size: " << sizeof(unsigned long) << " bytes)" << std::endl;
    std::cout << "unsigned long long: " << unsignedLongLongVar << " (Size: " << sizeof(unsigned long long) << " bytes)" << std::endl;

    std::cout << "\nFloating-Point Types:" << std::endl;
    std::cout << "float: " << floatVar << " (Size: " << sizeof(float) << " bytes)" << std::endl;
    std::cout << "double: " << doubleVar << " (Size: " << sizeof(double) << " bytes)" << std::endl;
    std::cout << "long double: " << longDoubleVar << " (Size: " << sizeof(long double) << " bytes)" << std::endl;

    std::cout << "\nCharacter Types:" << std::endl;
    std::cout << "char: " << charVar << " (Size: " << sizeof(char) << " bytes)" << std::endl;
    std::cout << "signed char: " << signedCharVar << " (Size: " << sizeof(signed char) << " bytes)" << std::endl;
    std::cout << "unsigned char: " << unsignedCharVar << " (Size: " << sizeof(unsigned char) << " bytes)" << std::endl;

    std::cout << "\nBoolean Type:" << std::endl;
    std::cout << "bool: " << boolVar << " (Size: " << sizeof(bool) << " bytes)" << std::endl;

    std::cout << "\nSize Types:" << std::endl;
    std::cout << "size_t: " << sizeVar << " (Size: " << sizeof(size_t) << " bytes)" << std::endl;

    std::cout << "\nWider Character Types:" << std::endl;
    std::wcout << "wchar_t: " << wideCharVar << " (Size: " << sizeof(wchar_t) << " bytes)" << std::endl;
    std::wcout << "char16_t: " << char16Var << " (Size: " << sizeof(char16_t) << " bytes)" << std::endl;
    std::wcout << "char32_t: " << char32Var << " (Size: " << sizeof(char32_t) << " bytes)" << std::endl;

    return 0;
}

Derived Data Types

Derived Data Types are those that are created by combining primitive or built-in datatypes. Thera are three types of derived-defined data types in C++:

  1. Array: A collection of elements of the same data type, organized in a contiguous memory block.
  2. Pointer: A pointer is a variable that holds the memory address of another variable. Pointers are used for dynamic memory allocation and manipulation.
  3. Reference: A reference is an alias to an existing variable. It provides an alternative name for a variable, often used in function parameters to pass variables by reference.

Example:

Here's a C++ program that demonstrates the use of the derived data types:

#include <iostream>

int main() {
    // Arrays
    int array[5] = {1, 2, 3, 4, 5};

    std::cout << "Array Example:" << std::endl;
    for (int i = 0; i < 5; i++) {
        std::cout << "array[" << i << "] = " << array[i] << std::endl;
    }

    // Pointers
    int x = 10;
    int* pointer = &x;

    std::cout << "\nPointer Example:" << std::endl;
    std::cout << "x = " << x << std::endl;
    std::cout << "pointer points to: " << *pointer << std::endl;

    // References
    int y = 20;
    int& reference = y;

    std::cout << "\nReference Example:" << std::endl;
    std::cout << "y = " << y << std::endl;
    std::cout << "reference refers to: " << reference << std::endl;

    return 0;
}

User-Defined Data Types

The User-defined data types are defined by the user themselves. The different User-defined data types are:

  1. Structures: A user-defined data type that groups related variables under a single name.
  2. Classes: An extension of structures that not only groups data but also includes functions that operate on that data (a key feature of object-oriented programming).
  3. Union: In C++, a union is a user-defined data type that allows you to store different types of data in the same memory location. Unlike structures, which store each data member at a separate memory location, unions store all members at the same memory location, meaning that only one member can be active or in use at any given time. Unions are often used when you want to store different types of data, but you don't need to access them all simultaneously.
  4. Enumeration: An enumeration, also known as an enum, is a user-defined data type in C++ that consists of a set of named integral constants. It allows you to create a symbolic representation of a set of related values, making your code more readable and self-explanatory. Enumerations are often used when you have a predefined set of values that you want to use as options, flags, or categories in your program.
  5. Typedef: In C++, the typedef keyword is used to create user-defined data type aliases. It allows you to create a new name (an alias) for an existing data type, making your code more readable and more expressive. typedef is often used to simplify complex or lengthy data type names, especially when working with complex data structures or to enhance portability.

Example:

Here's a C++ program that demonstrates the use of the user-defined data types:

#include <iostream>

// Structures
struct Student {
    std::string name;
    int age;
};

// Classes
class Circle {
private:
    double radius;

public:
    Circle(double r) : radius(r) {}

    double getArea() {
        return 3.14159265359 * radius * radius;
    }
};

// Union
union UnionExample {
    int intValue;
    float floatValue;
    char charValue;
};

// Enumeration
enum Color {
    RED,
    GREEN,
    BLUE
};

// Typedef
typedef int Age;

int main() {
    // Structures
    Student student1;
    student1.name = "Alice";
    student1.age = 20;

    // Classes
    Circle circle(5.0);
    std::cout << "Circle Area: " << circle.getArea() << std::endl;

    // Union
    UnionExample u;
    u.intValue = 42;
    std::cout << "Union Example (int): " << u.intValue << std::endl;
    u.floatValue = 3.14;
    std::cout << "Union Example (float): " << u.floatValue << std::endl;

    // Enumeration
    Color favoriteColor = BLUE;
    std::cout << "Favorite Color: ";
    if (favoriteColor == RED) {
        std::cout << "Red" << std::endl;
    } else if (favoriteColor == BLUE) {
        std::cout << "Blue" << std::endl;
    } else if (favoriteColor == GREEN) {
        std::cout << "Green" << std::endl;
    }

    // Typedef
    Age personAge = 30;
    std::cout << "Age: " << personAge << std::endl;

    return 0;
}