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“.
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: 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: The first value at the allocated momery : 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. Below is the C++ program illustrating the use of enum: 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.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
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(){ // 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;}
The next value stored after removing the prevous value : ”
The Final value value at the same allocated memeory space : 34.34C++
// 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


