Learn How To Use The Built-In Types That C++ Gives You And Then Create Your Own
When I was first learning to program, I was in college studying electronics. This was in the late 1980’s in Chicago. I later used my skills to interview for a programming job in the early 1990’s in Singapore and failed miserably. I still laugh a bit when I think about it. In order to understand why I failed, you need a little background. Just enough to picture the state of computers and programming at the time.
Nobody I knew had cell phones. And if you did have a cell phone, it was heavy and bulky and the only thing you could do with it was make poor quality phone calls at expensive rates by the minute. There was no internet like we know it today. And personal computers were still a bit rare. This was before 64 bit processors and even 32 bit processors. You were lucky to have a 16 bit computer. Most of my work was done with an 8 bit microprocessor called the 8085 by Intel.
The microprocessor controls everything in a computer. And an 8 bit processor means it has the ability to work with 8 bits at a time. A bit is just a single binary value which can be either zero or one. Whenever the processor needs to read or write data to memory, it reads or writes 8 bits all at once. Each bit is separate and the bit value is carried to and from the processor with its own wire.
Imagine for a moment that you represent a bit. You can carry a flag to mean that you have the value one. And if you have no flag, then you’re a zero. You live in a house with seven of your friends who also carry flags. Here’s the thing, you all leave the house together and come home together. The house has 8 chairs in each room and you all move from room to room together. When I say together, I mean that you all enter and leave a room at the same time. If there’s only one door to a room, then you can’t all fit through at the same time. There needs to be 8 doors. When you leave the house and come back, you also use 8 doors and 8 sidewalks. You all walk side by side down the street when you go to the store and come back. If you walk faster than the others, then you need to wait for them before continuing.
An 8 bit processor is just the same. It has 8 connections for each bit and the data bits move along these connections at the same time. Some are zeros and some are ones.
When you have this type of computer, there’s a lot less variety in what you can do with your variable types and you’ll probably use simple byte values a lot. A byte is just another name for 8 bits. Okay, technically, some computers might have more than 8 bits in a byte. For our purposes, we’ll treat a byte as a collection of 8 bits.
This is what I knew and was most familiar with. I could program an 8085 to work with these bytes and calculate things. When I went for an interview and was asked to solve a simple programming problem by writing the code on paper, what do you think I did?
The interviewer said that I could use any language that I wanted. Whatever made me feel comfortable. All I had to do was write some code to solve the problem. I don’t remember anymore what the problem was. I do remember filling the paper with 8085 instructions. I wrote code to keep track of numbers after reading them from memory and used those numbers to solve the problem. I had loops and methods, and global variables and stack variables. It was all included in hand crafted 8085 assembly language.
After completing the program, I looked it over and felt good about it. It had been a couple years since college and I was glad to have remembered so much. The interviewer was not impressed and thought I must be joking with him. Nobody solves a programming interview question with 8085 assembly! Except me. And I was lucky I wasn’t thrown out.
My next interview went better. You see, I did take one semester of C programming in college. But I had forgotten almost all of that. So I bought a book on the C language and read the whole thing in a weekend. What was I learning about? A big part was all the different data types that the C language allows you to use. And C++ follows the same basic ideas. You’ll need to know what types are available for you to use and when and how to use them. Unless you want to take a chance like I did. I can say for certain that you won’t go far when people think you’re making fun of them.
There’s quite a few data types to understand. Each is different and usually has multiple variations such as size. It’s not unlimited or anything. Just too many to understand in one class. You can learn to make use of them all in six classes. Even then, there are finer points that will be explained in later classes.
The first class explains three basic types, char, bool, and int. Together, these are known as the integral data types. Once you understand these types, you’ll be able to create variables to hold text, keep track of whether things are true or false or open or closed or up or down, etc., and count things and add and subtract and multiply and divide whole numbers.
Let’s say that you want to build a game. With an understanding of these data types, you’ll be able to keep track of the health of your hero, know whether the hero is still alive or not, and display text to the player to ask what move to make next.
It’s a lot better to use these types instead of treating everything as a byte like when I first learned how to code. Especially when you consider the other types.
The second class explains binary floating point types, float, double, and long double. These are really just different sizes of the same concept and allow you to easily represent fractional values so you’re not limited to whole numbers anymore.
There’s another different concept called decimal floating point that also comes in different sizes. You’ll understand the differences between all of these especially between binary and decimal types. The only problem is that decimal floating point types aren’t supported in all languages and computer hardware.
Taking the same game example, with this understanding, you’ll be able to keep track of the exact location of the hero in your game world even when the hero is moved just a tiny bit to the left or right.
Once you understand both the integral types from the first class and the floating point types from the second class, you’ll have learned all the arithmetic types. These are the number types. The other classes then begin to explain types that go beyond just numbers.
The third class adds the types void and nullptr_t so that you’ll understand all of the fundamental types. Then it goes on to explain the built-in types, pointer, reference, and array.
With void, you’ll be able to declare methods that don’t return any type or declare pointers to nothing in particular. It’s a strange type because you can’t really use it by itself. I put it in the third class so that you can learn about void along with pointers.
The pointers and references will help you solve problems that would otherwise be too difficult or run too slowly without them. Many people talk about how difficult pointers are to understand and use properly and use this as a reason to avoid C++. After this class, you’ll see that they’re not too hard and actually help you quite a lot. A nullptr_t is the actual type of nullptr and lets you define pointers that don’t point anywhere.
And the array type finishes the built-in types and gives you the ability to work with many things together. In the game example, this would let your hero hold many items in a backpack and allow your code to keep track of them all.
After these three classes, you’ll have an understanding of all the types that the C++ language gives you. These are the types that are built into the language. But you can do even more when you create your own types.
The fourth, fifth, and sixth classes explain how to do just that. You’ll learn about the types enum, struct, and union in the fourth class, function types in the fifth class, and about class types in the sixth class. These are the types you’ll create yourself and are called user-defined types.
I’ll have to start being careful from this point to not confuse the class type with the lecture class. I normally don’t refer to the live lectures as lectures. I like to call them classes. This can be confusing with the class type.
Anyway, with an enum, you’ll be able to give names to concepts or ideas or states that you can choose from later. You can use an enum to tell if your hero is walking, running, sleeping, riding, or anything else that makes sense to your application. There’s no way that any language can have these ideas builtin which is why you have to define them.
What if you want to bundle a group of data together into one higher level idea? Let’s take the hero’s stats as an example. You don’t always want to keep referring to the health, age, strength, speed, etc. as separate values do you? It would be better to group all these together into a single idea that you can call player stats. A struct lets you do that.
Sometimes you’ll want to bundle data together like in a struct but you know that you only really need one of them at a time. It would be a waste of memory to reserve space for a bunch of things when only one will be valid at any given time. A union lets you express this idea.
A program can’t consist of just data types alone. It needs to actually do something. All the code instructions you write go inside of functions. Sometimes I call them methods. You’ve already seen the main method. Every C++ program has to have a method called main. But you can create your own methods too and use them to make sense of your code. This way, not everything has to be written inside of main and you can name your methods so the name describes the action taking place inside. This helps make your code easier to read, understand, and maintain. You need two things in order to use a function. You need to declare the function and then at some point, either together with the declaration or in another place, you need to define the function by providing the function body containing the code. The declaration sets up the type of the function.
Technically, a struct and a class have identical capability. Classes were introduced into object-oriented programming languages like C++ to let you add behavior to your types. This is really powerful because it lets you create types that are more than just data values and start to behave the way you would expect. You can add behavior that lets you write code that’s natural and easy to read. This will introduce you to class methods and let you do things such as add the ability to your hero so you can tell it to start walking. Walking then becomes just as much a part of the hero as the ability to add two numbers together is for integers. You do this by writing methods that exist inside your classes or that have special permission to work with your classes.