Skip to content

Commit b1da7a2

Browse files
committed
Add C++ concepts enum, enum class
1 parent 2b3d273 commit b1da7a2

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// enums https://www.learncpp.com/cpp-tutorial/enumerated-types/
2+
3+
#include <iostream>
4+
5+
int main()
6+
{
7+
enum Color
8+
{
9+
color_red,
10+
color_blue
11+
};
12+
13+
enum Fruit
14+
{
15+
fruit_apple,
16+
fruit_banana
17+
};
18+
19+
Color color { color_blue };
20+
Fruit fruit { fruit_banana };
21+
22+
if (color == fruit) {
23+
std::cout << "color and fruit are equal\n";
24+
} else {
25+
std::cout << "color and fruit are not equal\n";
26+
}
27+
28+
return 0;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// enum classes https://www.learncpp.com/cpp-tutorial/enum-classes/
2+
3+
#include <iostream>
4+
5+
#include <iostream>
6+
7+
int main()
8+
{
9+
enum class Color
10+
{
11+
color_red,
12+
color_blue
13+
};
14+
15+
enum class Fruit
16+
{
17+
fruit_apple,
18+
fruit_banana
19+
};
20+
21+
Color color { Color::color_blue };
22+
Fruit fruit { Fruit::fruit_banana };
23+
24+
if (color == fruit) {
25+
std::cout << "color and fruit are equal\n";
26+
} else {
27+
std::cout << "color and fruit are not equal\n";
28+
}
29+
30+
return 0;
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// enum classes https://www.learncpp.com/cpp-tutorial/enum-classes/
2+
3+
#include <iostream>
4+
5+
#include <iostream>
6+
7+
int main()
8+
{
9+
enum class Color
10+
{
11+
color_red,
12+
color_blue
13+
};
14+
15+
Color color { Color::color_blue };
16+
17+
std::cout << static_cast<int>(color) << "\n";
18+
19+
return 0;
20+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://www.learncpp.com/cpp-tutorial/random-number-generation/
2+
3+
#include <iostream>
4+
5+
unsigned int PRNG()
6+
{
7+
static unsigned int seed { 5323 };
8+
seed = 8253729 * seed + 2396403;
9+
return seed % 32768;
10+
}
11+
12+
int main()
13+
{
14+
for (int i {1}; i <= 100; ++i) {
15+
std::cout << PRNG() << "\t";
16+
17+
if (i % 5 == 0) {
18+
std::cout << "\n";
19+
}
20+
}
21+
22+
return 0;
23+
}

C++-Concepts/RandomNumbers/Srand.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://www.learncpp.com/cpp-tutorial/random-number-generation/
2+
3+
#include <iostream>
4+
#include <cstdlib>
5+
6+
int main()
7+
{
8+
std::srand(5232);
9+
std::rand();
10+
11+
for (int i {1}; i <= 100; ++i) {
12+
std::cout << std::rand() << "\t";
13+
14+
if (i % 5 == 0) {
15+
std::cout << "\n";
16+
}
17+
}
18+
19+
return 0;
20+
}

C++-Concepts/RandomNumbers/Srand2.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// https://www.learncpp.com/cpp-tutorial/random-number-generation/
2+
3+
#include <iostream>
4+
#include <cstdlib>
5+
#include <ctime>
6+
7+
8+
int getRandomNumber(int min, int max)
9+
{
10+
static constexpr double fraction {1.0 / (RAND_MAX + 1.0)};
11+
return min + static_cast<int>((max - min + 1) * (std::rand() * fraction));
12+
}
13+
14+
int main()
15+
{
16+
std::srand(static_cast<unsigned int>(std::time(nullptr)));
17+
std::rand();
18+
19+
for (int i {1}; i <= 100; ++i) {
20+
std::cout << std::rand() << "\t";
21+
22+
if (i % 5 == 0) {
23+
std::cout << "\n";
24+
}
25+
}
26+
27+
std::cout << "====================================\n";
28+
29+
for (int i {1}; i <= 100; ++i) {
30+
std::cout << getRandomNumber(0, 100) << "\t";
31+
32+
if (i % 5 == 0) {
33+
std::cout << "\n";
34+
}
35+
}
36+
37+
return 0;
38+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Short sample program to demonstrate C++ language basics
3636
* move semantics
3737
* move constructor
3838
* move assignment
39+
* enums & enum class
40+
* Random numbers
3941

4042

4143
# [Operating System Concepts](Operating-System-Concepts/)

0 commit comments

Comments
 (0)