Structures, Unions and Enumerations in C++

18 Apr, 2022
In this article, we will discuss structures, unions, and enumerations and their differences.
The structure is a user-defined data type that is available in C++.
Structures are used to combine different types of data types, just like an array is used to combine the same type of data types.
A structure is declared by using the keyword “struct“. When we declare a variable of the structure we need to write the keyword “struct in C language but for C++ the keyword is not mandatory
Syntax:
struct 
{
   // Declaration of the struct
}
Below is the C++ program to demonstrate the use of struct:
C++
// C++ program to demonstrate the// making of structure#include <bits/stdc++.h>using namespace std; // Define structurestruct GFG {    int G1;    char G2;    float G3;}; // Driver Codeint main(){    // Declaring a Structure    struct GFG Geek;    Geek.G1 = 85;    Geek.G2 = 'G';    Geek.G3 = 989.45;    cout << "The value is : "         << Geek.G1 << endl;    cout << "The value is : "         << Geek.G2 << endl;    cout << "The value is : "         << Geek.G3 << endl;     return 0;}
The value is : 85
The value is : G
The value is : 989.45

Explanation: In the above code, that values are assigned to (G1, G2, G3) fields of the structure employee and at the end, the value of “salary” is printed.
Structure using typedef: typedef is a keyword that is used to assign a new name to any existing data-type. Below is the C++ program illustrating use of struct using typedef:
C++
// C++ program to demonstrate the use// of struct using typedef#include <bits/stdc++.h>using namespace std; // Declaration of typedeftypedef struct GeekForGeeks {     int G1;    char G2;    float G3; } GFG; // Driver Codeint main(){    GFG Geek;    Geek.G1 = 85;    Geek.G2 = 'G';    Geek.G3 = 989.45;     cout << "The value is : "         << Geek.G1 << endl;     cout << "The value is : "         << Geek.G2 << endl;     cout << "The value is : "         << Geek.G3 << endl;     return 0;}
The value is : 85
The value is : G
The value is : 989.45

Explanation:
In the above code, the keyword “typedef” is used before struct and after the closing bracket of structure, “GFG” is written.
Now create structure variables without using the keyword “struct” and the name of the struct.
A structure instance has been created named “Geek” by just writing “GFG” before it.
Unions: A union is a type of structure that can be used where the amount of memory used is a key factor. 
Similarly to the structure, the union can contain different types of data types.
Each time a new variable is initialized from the union it overwrites the previous in C language but in C++ we also don’t need this keyword and uses that memory location.
This is most useful when the type of data being passed through functions is unknown, using a union which contains all possible data types can remedy this problem.
It is declared by using the keyword “union“.
Below is the C++ program illustrating the implementation of union:
C++
// C++ program to illustrate the use// of the unions#include <iostream>using namespace std; // Defining a Unionunion GFG {    int Geek1;    char Geek2;    float Geek3;}; // Driver Codeint main(){    // Initializing Union    union GFG G1, G2, G3;     G1.Geek1 = 34;    G2.Geek2 = 34;    G3.Geek3 = 34.34;     // Printing values    cout << "The first value at "         << "the allocated memory : "         << G1.Geek1 << endl;     cout << "The next value stored "         << "after removing the "         << "previous value : "         << G2.Geek2 << endl;     cout << "The Final value value "         << "at the same allocated "         << "memory space : "         << G3.Geek3 << endl;    return 0;}
The first value at the allocated memory : 34
The next value stored after removing the previous value : "
The Final value value at the same allocated memory space : 34.34

Explanation: In the above code, Geek2 variable is assigned an integer (34). But by being of char type, the value is transformed through coercion into its char equivalent (“). This result is correctly displayed in the Output section.
Enums: Enums are user-defined types that consist of named integral constants.
It helps to assign constants to a set of names to make the program easier to read, maintain and understand.
An Enumeration is declared by using the keyword “enum“.
Below is the C++ program illustrating the use of enum:
C++
// C++ program to illustrate the use// of the Enums #include <bits/stdc++.h>using namespace std; // Defining  an enumenum GeeksforGeeks { Geek1,                     Geek2,                     Geek3 }; GeeksforGeeks G1 = Geek1;GeeksforGeeks G2 = Geek2;GeeksforGeeks G3 = Geek3; // Driver Codeint main(){    cout << "The numerical value "         << "assigned to Geek1 : "         << G1 << endl;     cout << "The numerical value "         << "assigned to Geek2 : "         << G2 << endl;     cout << "The numerical value "         << "assigned to Geek3 : "         << G3 << endl;     return 0;}
The numerical value assigned to Geek1 : 0
The numerical value assigned to Geek2 : 1
The numerical value assigned to Geek3 : 2

Explanation: In the above code, the named constants like Geek1, Geek2, and Geek3 have assigned integral values such as 0, 1, 2 respectively while the output is given.
eshitadutta
abhishek0719kadiyan
RishabhPrabhu
C-Struct-Union-Enum
cpp-struct
Structure & Union
C++
C++ Programs
cpp-struct
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Friend class and function in C++
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ program for hashing with chaining