Open In App

Structures, Unions and Enumerations in C++

In this article, we will discuss structures, unions, and enumerations and their differences.

Syntax:



struct 
{
   // Declaration of the struct
}

Below is the C++ program to demonstrate the use of struct:




// 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++ 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:

Unions: A union is a type of structure that can be used where the amount of memory used is a key factor. 

Below is the C++ program illustrating the implementation of union:




// 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.

Below is the C++ program illustrating the use of enum:




// 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.


Article Tags :