image
Enumerated Types

Contents

Introduction
1
Course Overview
PREVIEW2m 14s
Inheritance and Polymorphism
2
Inheritance (Part 1)
PREVIEW11m 57s
Conclusion

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 49m
Students
50
Ratings
5/5
starstarstarstarstar
Description

This course is designed to enhance your object-oriented programming skills by focusing on two concepts: inheritance and polymorphism. We'll cover the key concepts and then put them to practice with a couple of demo projects towards the end of the course.

Learning Objectives

  • Learn about base classes and derived classes and how they are related
  • Understand how different base classes can be used to control how derived classes inherit data and behaviors from their base classes
  • Understand the fundamentals of polymorphism
  • Learn about enumerated types in C++

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

 

Transcript

In the last lecture, we discussed and explored the syntax involved with polymorphism. In this lecture, we take a brief detour to the topic of enums or enumerated types. Enums are a little bit like classes and that they are programmer defined types. However, they have special restrictions on them. An enum can be used to represent any of the finite list of named constants. This can be very handy for values where we need a relatively small list of values such as directions; north, south, east west, 2D directions; up, down, left, right days of the week; Monday, Tuesday, Wednesday etc. and more. An enumerated variable can only take on the value of one of the possible constants that the enumerated type represents. It will help to see an example. Let's create a project called EnumFun. So, create a new project, empty project EnumFun, and hit 'Create'. And of course for this one we just need a main file, so right click 'Add' 'New item' and we've got main.cpp, and for this one we'll just use iostream and create our little skeleton here for main. And for our enumerated type, we have enum Direction {UP, DOWN, LEFT, RIGHT}; there we go. Now, since they are named constants essentially, I'm setting this up so that they're all capitalized, but you don't really have to, you can make them uppercase, sentence case, camel case, whatever you'd like really. Now, let's create a variable of this type. So, now let's see what happens if I say cout<< myDirection << endl; Let's do this first. So, it prints out a zero, now that's interesting. So, what happens if I put DOWN instead of UP? Print that out, now it's printing out a one. These named constants get an integer value 0, 1, 2, 3. That's why it's doing what it's doing. Now, if we continue here, we can actually use an if statement, if else. So, if (myDirection == UP), maybe we want to print out up, else if (myDirection == DOWN), maybe we want to print out down, else if(myDirection) == LEFT), we can print out left, scroll up just a little bit, if (myDirection == RIGHT), we can print out right; because these aren't strings, these are integer values and their constants. Now we're getting a little bit of an underline here, it's not an error. It's telling us that there is now a version of enums called scoped enums where you put enum class so they won't be directly convertible or coercible to integer types, but for our purposes this enum type works perfectly fine and that's actually the more common type that's used anyway. So, let's run it, debug, start without debugging and now it's saying up, and that indeed is the type that we have, now value rather, that's the value we have, now let's change it to LEFT and see what that does, and indeed it prints out a two, because that's it's 0, 1, 2, that's it's integer value and then prints out LEFT. Alright, pretty cool. We can use the enumerated types for comparisons, because each of the values of the enum again, are named integer constants. Now, when we print out the direction, you can see the integer, since UP occurs first, again its ordinal value is usually what we call it. It's not really an index because it's not an array, it's not a collection of values in the same way that an array is. So, we call it an ordinal value and the ordinal value of UP is zero, DOWN is one, LEFT is two and then RIGHT is three. So, that would be the value that's printed. So, as you can probably tell like any named constants, enums can be very useful. They are especially important for situations where there is a need to maintain a few related named constants as we have seen with the direction example. And of course before moving on, I have a quick challenge for you. If we picture our direction enum being used as for example a 2D computer game or something of that sort, the player character can move in any of the given directions. However, the player might also want to stand still, doesn't have to constantly move, right? So, I want you to extend the enum with the value standing. Make sure to add the if else to the chain to test it out, to make sure that it works also. So, pause the video come back when you're done or if you need some help. How is that little challenge? Were you able to accomplish the task? I hope so, let's do it together. So, I'm going to change this here so that the enum now has the value standing 0, 1, 2, 3, 4. So, it has five possible values and this one will have the value four, STANDING. So, we should get a print out of four and we can also add to our else if chain, if they're standing we can put standing still or something of that sort. Excellent, let's run it. Debug, start without debugging. There we go. We have four and then it says standing still. Excellent. So, it looks like our new value works great. So, you can see it's pretty easy to modify and update our enumerated types. In the next lecture, you'll work on the first project for this section. I'll see you there.

 

About the Author
Students
1386
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics