C++中的struct、union与enum
0.概述
在本文中,我们将讨论结构体、联合和枚举及其区别。
1.struct
struct是C++中可用的用户自定义数据类型。
struct用于组合不同类型的数据类型,就像数组用于组合相同类型的数据类型一样。
使用关键字 struct ”声明结构。当我们声明结构变量时,我们需要在 C 语言中编写关键字“ struct,但对于 C++来说,关键字struct不是强制性的。
struct
{
// Declaration of the struct
}
下面是演示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;
}
输出结果如下
The value is : 85
The value is : G
The value is : 989.45
说明:在上面的代码中,值被赋值给结构体employee的(G1,G2,G3)字段,最后打印出“salary”的值。
使用 typedef 的结构: typedef是一个关键字,用于为任何现有数据类型分配新名称。下面是 C++ 程序说明使用typedef来使用 struct :
// 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
在上面的代码中,关键字“ typedef ”被用在了struct之前,structure的右括号之后,写上了“ GFG ”。
现在创建结构变量而不使用关键字“ struct ”和结构名称。
一个名为“Geek”的结构实例已经创建,只需在它前面写上“ GFG ”。
2. union
union 联合是一种结构,可以在使用的内存空间是关键因素,也就是节省内存空间的情况下使用。
与结构类似,联合可以包含不同类型的数据类型。
每次从联合初始化一个新变量时,它会覆盖 C 语言中的前一个变量,但在 C++ 中,我们也不需要此关键字并使用该内存位置。
当通过函数传递的数据类型未知时,这是最有用的,使用包含所有可能数据类型的联合可以解决这个问题。
它是通过使用关键字“ union ”来声明的。
下面是说明 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 = 34;
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 : "
The Final value value at the same allocated memory space : 34.34
解释:在上面的代码中,Geek2 变量被赋值了一个整数(34)。但是由于是 char 类型,该值通过强制转换为它的 char 等价物 (")。此结果正确显示在“输出”部分中。
3. enum
enumeratation枚举,枚举是用户定义的类型,由命名的整数常量组成。
它有助于将常量分配给一组名称,使程序更易于阅读、维护和理解。
枚举是通过使用关键字“ enum ”来声明的。
枚举的默认值从0开始,依次递增1,也可以自定义其它值,然后依次递增1,或完整自定义其它值。
下面是说明枚举用法的 C++ 程序:
// C++ program to illustrate the use
// of the Enums
#include <bits/stdc++.h>
using namespace std;
// Defining an enum
enum GeeksforGeeks { Geek1=2,
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 : 2
The numerical value assigned to Geek2 : 3
The numerical value assigned to Geek3 : 4
说明:在上面的代码中,Geek1、Geek2、Geek3等命名常量在给定输出时分别赋值为2、3、4等整数。
相关文章
- C++之new和delete
- C++构造函数实例——拷贝构造,赋值
- 利用OpenCV的函数Rect()提取感兴趣区域的C++代码
- 35 C++ - 指针运算符(*、->)重载
- C++ 简单的 Tcp 实现[socket] 服务器端与客户端通信
- 使用c++filt命令还原C++编译后的函数名
- C++流的streambuf详解及TCP流的实现
- 《Visual C++ 开发从入门到精通》——2.6 数据类型
- 《C++ Templates中文版》——第2章 函数模板
- 基于汇编的 C/C++ 协程 - 切换上下文
- c++/c 获取cpp文件行号跟文件名
- 137、【贪心算法】leetcode ——406. 根据身高重建队列(多维度贪心)(C++版本)
- c++ template笔记
- C++写动态站点之HelloWorld!
- c++ struct与class的差别
- C++实操 - struct和class的区别
- C/C++ Windows API——获取磁盘信息