C++ Data Types and Structures

Chapter 10: Simple Data Types - Built-In and User-Defined

Overview of Data Types

  • Data types in C++ categorize data into types, allowing for structured programming.
  • C++ supports simple data types (built-in and user-defined) and structured data types.

Core Topics

  • External and Internal Representations of Data: How data is viewed by the compiler versus how it's stored in memory.
  • Integral and Floating Point Data Types: Different numeric types and their characteristics.
  • Combined Assignment Operators: Simplifying arithmetic operations in variable assignments.
  • Enumeration Types: Creating new data types using enumerations by listing potential values.
  • User-Written Header Files: Including custom header files for data type definitions.

C++ Simple Data Types

  • Integral Types: These include:
    • char (1 byte): Represents characters. Range: -128 to 127.
    • short (2 bytes): Range: -32,768 to 32,767.
    • int (typically 2 or 4 bytes): Range: -32,768 to 32,767.
    • long (4 bytes): Range: -2,147,483,648 to 2,147,483,647.
    • bool: Can be true or false.
  • Floating Point Types:
    • float (4 bytes): Range: 3.4E-38 to 3.4E+38.
    • double (8 bytes): Range: 1.7E-308 to 1.7E+308.
    • long double (10 bytes): Extended range for high-precision requirements.

Key Points on Sizes and Ranges

  • Size in bytes may be machine-dependent.
  • Use the sizeof operator to determine the size of data types on the specific machine.
  • C++ guarantees a basic size hierarchy.

Operators and Expressions

  • Combined Assignment Operators:
    • age += 3; equivalent to age = age + 3;
    • Similar syntax for subtraction (-=), multiplication (*=), and division (/=) operations.

Enumeration Types

  • Enumeration allows for defining a new data type with a set of named constants:
  enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
  • Declaring Enums: Create enumerated types in a clear and organized manner.
  • Can be used in switch statements and comparisons using relational operators (e.g., <, >).

Structs and User-Defined Data Types

  • A structured data type groups related data items together, allowing for access using a single name.
  • Defining a Struct:
  struct AnimalType {
      long id;
      string name;
      ...
  };
  • Access struct members using the dot operator (.).

Example Structs and Unions

  • Struct Usage:
    • Access members via thisAnimal.name, thisAnimal.age++.
  • Using Unions: Multiple data members but only one can be used at a time:
  union WeightType {
      long wtInOunces;
      int wtInPounds;
      float wtInTons;
  };

Pointers and References

  • Pointer Variables: Hold memory addresses of variables.
  • Use * to dereference a pointer and get its value. For example:
  int* ptr = &x;
  *ptr = 5;
  • Reference Variables: Automatically manage memory addresses without needing explicit dereferencing:
  int& intRef = gamma;

Hierarchical Structures

  • Struct members can be another struct type, enabling nesting and more complex data structures.
  • Example:
  struct MachineRec {
      int idNumber;
      StatisticsType history;
  };

Conclusion

  • Understanding simple and custom data types in C++ is crucial for efficient programming. Proper usage of structures, enums, and pointers enhances code clarity and organization, while ensuring effective data management.