C++ User-defined Data Types


User-defined data types in C++ allow programmers to create complex data structures that better reflect the needs of their applications. By defining custom types, developers can group related data and behaviors into a single entity, improving code organization and maintainability. The main user-defined data types in C++ include structures, classes, unions, enumerations, and type definitions (typedef). Here's a detailed explanation of each:

1. Structures (struct)

Structures are used to group different data types together under a single name. They are useful for representing a record with multiple attributes.

  • Declaration:

    struct Person { std::string name; int age; };
  • Usage:

    Person person1; person1.name = "Alice"; person1.age = 30;
  • Accessing Members: You can access the members of a structure using the dot (.) operator.

    std::cout << "Name: " << person1.name << ", Age: " << person1.age << std::endl;

2. Classes

Classes are a fundamental part of C++’s object-oriented programming. They allow for encapsulation, inheritance, and polymorphism. Classes combine data (attributes) and methods (functions) that operate on the data.

  • Declaration:

    class Car { public: std::string brand; void honk() { std::cout << "Beep!" << std::endl; } };
  • Usage:

    Car myCar; myCar.brand = "Toyota"; myCar.honk(); // Calls the honk method
  • Access Specifiers: Classes use access specifiers (public, private, and protected) to control access to members:

    • public: Members are accessible from outside the class.
    • private: Members are accessible only within the class.
    • protected: Members are accessible within the class and by derived classes.

3. Unions

Unions are similar to structures but allow storing different data types in the same memory location. Only one member can hold a value at any given time.

  • Declaration:

    union Data { int intValue; float floatValue; char charValue; };
  • Usage:

    Data data; data.intValue = 10; // Using the union to store an int std::cout << "Int Value: " << data.intValue << std::endl; data.floatValue = 5.5; // Overwrites the int value std::cout << "Float Value: " << data.floatValue << std::endl;

4. Enumerations (enum)

Enumerations define a variable that can hold a set of predefined constants. They improve code readability by allowing you to use meaningful names instead of numeric values.

  • Declaration:

    enum Color { RED, GREEN, BLUE };
  • Usage:

    Color myColor = RED; if (myColor == RED) { std::cout << "The color is red." << std::endl; }

5. Type Definitions (typedef)

The typedef keyword creates an alias for an existing data type. It simplifies complex type definitions and improves code readability.

  • Example:

    typedef unsigned long ulong; // Creating an alias for unsigned long ulong population = 7800000000;
  • Usage in Structures and Classes:

    typedef struct { std::string name; int age; } Person; Person person1; person1.name = "Alice";

6. Using using for Type Definition

C++11 introduced the using keyword as a more flexible alternative to typedef.

  • Example:
    using uint = unsigned int; // Creating an alias for unsigned int uint myNumber = 10;