Open In App

Structures, Unions and Enumerations in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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()
{
    // Initializing Union
    union GFG G1;
 
    G1.Geek1 = 34;
 
    // Printing values
    cout << "The first value at "
         << "the allocated memory : " << G1.Geek1 << endl;
 
    G1.Geek2 = 'G';
 
    cout << "The next value stored "
         << "after removing the "
         << "previous value : " << G1.Geek2 << endl;
 
    G1.Geek3 = 34.34;
 
    cout << "The Final value value "
         << "at the same allocated "
         << "memory space : " << G1.Geek3 << endl;
    return 0;
}


Output

The first value at the allocated memory : 34
The next value stored after removing the previous value : G
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 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.



Last Updated : 04 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads