Skip to content
Related Articles

Related Articles

Structures, Unions and Enumerations in C++
  • Difficulty Level : Hard
  • Last Updated : 03 May, 2021

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 keywordstruct“. When we declare a variable of the structure we need to write the keywordstruct 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 structure
struct GFG {
    int G1;
    char G2;
    float G3;
};
 
// Driver Code
int 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;
}
Output:
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 typedef
typedef struct GeekForGeeks {
 
    int G1;
    char G2;
    float G3;
 
} GFG;
 
// Driver Code
int 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;
}
Output:
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 Union
union GFG {
    int Geek1;
    char Geek2;
    float Geek3;
};
 
// Driver Code
int main()
{
    // Intializing 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 momery : "
         << G1.Geek1 << endl;
 
    cout << "The next value stored "
         << "after removing the "
         << "prevous value : "
         << G2.Geek2 << endl;
 
    cout << "The Final value value "
         << "at the same allocated "
         << "memeory space : "
         << G3.Geek3 << endl;
    return 0;
}
Output:

The first value at the allocated momery : 34
The next value stored after removing the prevous value : ”
The Final value value at the same allocated memeory space : 34.34

Explanation: In the above code, when the “Geek1” variable is initialized, it displays the value, then “Geek 2” is overwritten and no longer exists as Geek2 is of char type that’s why it doesn’t display any value.

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 enum
enum GeeksforGeeks { Geek1,
                     Geek2,
                     Geek3 };
 
GeeksforGeeks G1 = Geek1;
GeeksforGeeks G2 = Geek2;
GeeksforGeeks G3 = Geek3;
 
// Driver Code
int 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;
}
Output:
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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :