📜  C++中的结构,联合和枚举

📅  最后修改于: 2021-05-30 03:16:35             🧑  作者: Mango

在本文中,我们将讨论结构,联合和枚举及其差异。

  • 该结构是C++中可用的用户定义数据类型。
  • 结构用于组合不同类型的数据类型,就像数组用于组合相同类型的数据类型一样。
  • 通过使用关键字“ struct ”来声明结构。当我们声明结构的变量时,我们需要用C语言编写关键字“ struct” ,但对于C++而言,关键字不是强制性的

句法:

struct 
{
   // Declaration of the struct
}

下面是C++程序来演示struct的用法:

C++
// C++ program to demonstrate the
// making of structure
#include 
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;
}


C++
// C++ program to demonstrate the use
// of struct using typedef
#include 
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;
}


C++
// C++ program to illustrate the use
// of the unions
#include 
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;
}


C++
// C++ program to illustrate the use
// of the Enums
 
#include 
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;
}


输出:
The value is : 85
The value is : G
The value is : 989.45

说明:在以上代码中,已将值分配给结构employee的(G1,G2,G3)字段,最后打印了“工资”的值。

使用typedef的结构 typedef是用于为任何现有数据类型分配新名称的关键字。下面是C++程序,说明了使用typedef使用struct的情况:

C++

// C++ program to demonstrate the use
// of struct using typedef
#include 
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;
}
输出:
The value is : 85
The value is : G
The value is : 989.45

解释:

  • 在上面的代码中,关键字“ typedef ”在struct之前和结构的括号“ GFG”之后使用。
  • 现在创建结构变量,而无需使用关键字“ struct ”和结构名称。
  • 通过在结构实例前面编写“ GFG ”,可以创建一个名为“ Geek”的结构实例。

联盟:联合是一个类型的结构,可用于其中的存储器使用量是一个关键因素。

  • 与结构相似,联合可以包含不同类型的数据类型。
  • 每次从联合中初始化一个新变量时,它都会用C语言覆盖前一个变量,但是在C++中,我们也不需要此关键字并使用该内存位置。
  • 当通过函数传递的数据类型未知时,这非常有用,使用包含所有可能数据类型的联合可以解决此问题。
  • 通过使用关键字“ union ”进行声明。

下面是说明联合实现的C++程序:

C++

// C++ program to illustrate the use
// of the unions
#include 
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;
}
输出:

说明:在上面的代码中,当初始化“ Geek1”变量时,它显示值,然后“ Geek 2”被覆盖并且不再存在,因为Geek2是char类型,这就是为什么它不显示任何值。

枚举枚举是用户定义的类型,由命名的整数常量组成。

  • 它有助于为一组名称分配常量,以使程序更易于阅读,维护和理解。
  • 通过使用关键字“ enum ”来声明一个Enumeration。

下面是说明枚举用法的C++程序:

C++

// C++ program to illustrate the use
// of the Enums
 
#include 
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;
}
输出:
The numerical value assigned to Geek1 : 0
The numerical value assigned to Geek2 : 1
The numerical value assigned to Geek3 : 2

说明:在上面的代码中,在给出输出的同时,诸如Geek1,Geek2和Geek3之类的已命名常量分别分配了整数值,例如0、1、2。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”