Share how to enum c++

huuhoangasdf123

New member
## Cách Enum C ++

Các liệt kê C ++ là một cách để xác định một tập hợp các hằng số được đặt tên.Chúng được khai báo bằng cách sử dụng từ khóa `enum` và các giá trị của các hằng số được chỉ định bên trong niềng răng.Ví dụ:

`` `C ++
enum colors {
Màu đỏ,
Màu xanh lá,
Màu xanh da trời
};
`` `

Các giá trị của các hằng số được tự động gán, bắt đầu từ 0. Vì vậy, trong ví dụ trên, `red` có giá trị 0,` Green` có giá trị 1 và `blue` có giá trị 2.

Bạn có thể truy cập các giá trị của các hằng số bằng cách sử dụng từ khóa `enum`.Ví dụ:

`` `C ++
std :: cout << "màu đỏ có giá trị" << red << std :: endl;
`` `

Bạn cũng có thể sử dụng từ khóa `enum` để lặp lại các giá trị của các hằng số.Ví dụ:

`` `C ++
for (màu sắc màu: màu sắc) {
std :: cout << "màu là" << color << std :: endl;
}
`` `

## Ví dụ

Dưới đây là một ví dụ về chương trình C ++ sử dụng một bảng liệt kê để thể hiện các ngày trong tuần:

`` `C ++
#include <Istream>

enum daysoftheweek {
Chủ nhật,
Thứ hai,
Thứ ba,
Thứ Tư,
Thứ năm,
Thứ sáu,
Thứ bảy
};

int main () {
// In các ngày trong tuần.
for (Daysoftheweek Day: Daysoftheweek) {
std :: cout << "Ngày là" << ngày << std :: endl;
}

// Yêu cầu người dùng nhập một ngày trong tuần.
std :: cout << "Nhập một ngày trong tuần:";
Int Daynumber;
STD :: CIN >> Daynumber;

// In ngày tương ứng trong tuần.
Ngày của ngày = static_cast <Dayoftheweek> (Daynumber);
std :: cout << "Ngày là" << ngày << std :: endl;

trả lại 0;
}
`` `

## hashtags

* #C ++
* #Enumerations
* #Loại dữ liệu
* #Programming
* #tutorial
=======================================
## How to enum C++

C++ enumerations are a way to define a set of named constants. They are declared using the `enum` keyword, and the values of the constants are specified inside braces. For example:

```c++
enum Colors {
Red,
Green,
Blue
};
```

The values of the constants are assigned automatically, starting from 0. So in the example above, `Red` has the value 0, `Green` has the value 1, and `Blue` has the value 2.

You can access the values of the constants using the `enum` keyword. For example:

```c++
std::cout << "The color red has the value " << Red << std::endl;
```

You can also use the `enum` keyword to iterate over the values of the constants. For example:

```c++
for (Colors color : Colors) {
std::cout << "The color is " << color << std::endl;
}
```

## Example

Here is an example of a C++ program that uses an enumeration to represent the days of the week:

```c++
#include <iostream>

enum DaysOfTheWeek {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};

int main() {
// Print the days of the week.
for (DaysOfTheWeek day : DaysOfTheWeek) {
std::cout << "The day is " << day << std::endl;
}

// Get the user to enter a day of the week.
std::cout << "Enter a day of the week: ";
int dayNumber;
std::cin >> dayNumber;

// Print the corresponding day of the week.
DaysOfTheWeek day = static_cast<DaysOfTheWeek>(dayNumber);
std::cout << "The day is " << day << std::endl;

return 0;
}
```

## Hashtags

* #C++
* #Enumerations
* #data types
* #Programming
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top