⧼⧼  Previous Page
⧼  Table of Contents  ⧽
Next Page  ⧽⧽

Variables and Primitive Data-Types

Computer Programming C++ / Getting Started

As you should already know, almost all programs need to use varaibles. In the following video, we will cover some of the primitive data types in C++. But, what exactly is a primitive type? Basicaly they are the types of data that we can use to compose other types. For example: a button is a data type that you've probably used in the past. A button is built (composed) from other types. It probably has text (characters), a width and height and a position on the screen (integers), etc.

Sometimes these types of variables are called "built in" types, because they are built into the language. Though I personally don't like this term, because languages these days come with many data types that shouldn't be considered "primitive."

Printing variables to the console is pretty easy, but it's slightly different than what you're probably used to, so let's take a look:

Constant Variables

Occasionally, you'll want to create a variable that doesn't change. In other words, a constant. Like in C#, the const keyword can be used to make a variable immutable.

If you haven't heard the term before, just think of immutable as programmer speak for unchangeable. In contrast to mutable which can be changed.

One thing worth mentioning about const, is that it can come before or after the data-type as shown here:

I generally chose to put const before the data type, like the first example above, though you are free to use which ever one you choose.

Casting

Casting a variable simply means turning the value into a different data type. Let's take a look at how it's done.

Review Questions

The following are examples of questions similar to what you can expect to see on the next exam.

Hover over a question to show the answer.
  1. What are some examples of primitive types? int, float, double, bool, char, etc.
  2. Where is the const keyword placed in a variable declaration? Before or after the data-type.
  3. What does immutable mean? Cannot be changed.
  4. What does it mean to cast a variable? Turn the value into another data-type.
  5. How would you store a float variable called "f" into an int called "i"? i = (int)f;
⧼⧼  Previous Page
⧼  Table of Contents  ⧽
Next Page  ⧽⧽
© Ryan Appel