In this video, we'll learn how to declare and initialize variables.
Then we'll head over to CodeLite and declare and initialize some variables in live code.
The syntax for a variable declaration in c++ is very simple.
First, you tell the compiler what the type of the variable is.
Then you tell the compiler what the name of the variable is.
And finally, you terminate the declaration with a semicolon.
Later in this section, I'll go over some of the basic c++ types,
but here are a few as a preview. Int is an integer or a whole number.
A double is a floating point number.
And a string is a sequence of characters like you would see in my name Frank.
Also notice that we can declare variables of non-c++ built-in types.
One of the very powerful features of c++ is object-oriented programming,
which allows us to create our own types.
So we can create variables that represent an account or a person
using the same syntax as we used for integers and doubles.
This is great since it makes our program so much more readable.
In the next, slide we'll talk about some of the rules that you must follow
when you name variables.
Remember, in c++ we must declare our variables before we use them.
So let's take a look at some of the variable naming rules.
The rules for naming c++ variables are very very simple.
C++ variable names can contain letters, numbers and underscores.
However, the first letter of the name cannot be a number,
it can be an underscore or a letter.
As discussed previously, c++ keywords are also reserved
words so you can't use those names.
You can't use int, you can't use double as your own names.
Also you cannot declare a variable name that's already been declared in the same scope.
So if you declared x to be an integer, you can't do that again.
Remember, c++ is case-sensitive.
So the name, age in uppercase is considered
different from the name, age and lowercase. Let's take a look at a few examples.
In the left column of the table, you can see some legal c++ variable names.
Since c++ is case-sensitive,
age beginning with an uppercase A is considered
a different name from age beginning with a lowercase a.
We can begin our variable names with underscores or letters.
And we can also have underscores and numbers within the variable name.
The right column has some examples of the legal variable names.
The first example is lowercase int.
This is not allowed since lowercase int is a reserved word in c++.
We cannot have special characters like the dollar sign or mathematical characters
like the plus sign.
Also notice that variable names cannot have spaces in them.
Now let's talk about cout. Cout is a little different.
Cout is a legal name because it conforms to the variable naming rules.
However, if you include iostream in your program,
then cout is already defined somewhere else. It's defined by the standard library.
So it no longer becomes a valid name
since it's been defined somewhere you cannot re-declare it yourself.
So the rules that the c++ compiler enforces for variable names
are pretty simple as you can see.
Now let's take a look at variable naming style and best practices.
The most important thing you can do is to be consistent with whatever
naming convention you choose to use.
There are many style guides and naming conventions out there.
In many cases, you'll be working for a company,
and they have a specific style and any convention that you have to use.
This has nothing to do with the c++ compiler, but it's still very important.
For example, google has a published c++ style guide
as do many other companies.
One of the first decisions you make with regard to variable naming
is whether you want to use camel case or underscores to separate words.
Both styles make the text more readable by avoiding long streams
of lowercase letters which are harder to read.
CamelCase capitalizes the first letter of every word in your name
and doesn't use underscores.
The other style is to use underscores to separate words.
Either style is fine, the important thing is to be consistent.
In this course, I'll use the underscore style.
Please use meaningful names. Don't use obscure abbreviations. For example,
instead of using the variable name m-o-e to represent the mass
of the earth, just use the real name mass of earth,
with underscores or CamelCase either way.
Sure it's a little bit more typing but your program becomes so much more readable and maintainable.
Never use variable names before initializing them.
Uninitialized variables can cause all kinds of unexpected results in c++.
I'll show you an example of this when we head over to the IDE.
Finally, it's best practice to declare your variables
as close to when you need them as possible.
In other words, if your program uses 10 variables,
don't declare all 10 of them at the top of the file and then use them later.
Instead declare them right before you use them.
There are many, many more programming best practices, and we'll discuss them throughout the course.
So now let's talk about initializing variables.
Notice the first line of code in this example, int age.
This is a variable declaration that's uninitialized.
You have no idea what the value of age is.
And that's the problem. If you try to display age
or perhaps use it in some calculation,
you'll run into problems because it was never initialized, so its value could be anything.
Uninitialized variables are a very common source of programming errors.
C++ provides three ways to initialize variables.
All three are equivalent and, they're related to the way that c++ has evolved
over the years.
The first is to use an assignment operator. That's the equal sign.
This will assign 21 to age right when it's created,
so it will be immediately initialized.
The second uses parentheses around the value, in this case 21.
This is a constructor-style initialization.
We'll talk about constructors when we talk about object-oriented programming.
The final initialization statement
is the c++11 initialization syntax.
This style of initialization is consistent across different types in c++
and has some benefits that we'll discuss later in this course.
I encourage you to use this style of initialization.
Now let's head over to CodeLite and see some of these examples in live code.
Okay. So now I'm in CodeLite.
And let's write a real simple program that calculates the area
of a room in square feet.
So the idea is we'll ask the user for the length of the room and the width of the room
and then we'll multiply those two together to calculate the square footage of the room,
the area of the room. Okay.
Now obviously, we need two variables. But before we do that,
let me show you one of the issues with uninitialized variables
as we talked about in the previous slides.
Let's create a real simple variable here called age, just like we did before.
And I'm not going to initialize it on purpose.
And what I want to do is just output that value
to cout. Let's build this,
and I'll execute it. And you can see the what's printing out here.
You can see there's age. I'm outputting age, and that's the value that's in age right now.
Not what we expected for sure.
The problem is that we never initialize the variable age.
So the value that's in that variable could be anything.
And in this case, it is anything. Okay. So
let me show you how that works.
So let's go back to our conceptual model. Here's our memory.
I've got a memory location, let's say that that's maybe location 2000 that could be anything.
And age is associated with that location.
Now in there, there are a series of zeros and ones.
That's how we store values in computers.
Whatever value happens to be in there
is the value that will be displayed on the console. Remember,
when I output age,
the compiler is going to grab whatever this value is in here
and replace it right in there.
In this case, we've got that we got that really strange, very large number.
The idea is we really want to initialize this guy
to something even zero
before we use it. So you can see here, I'm using it before I initialize it.
That's not a good idea.
Okay. So let me clear this. And
if we now initialize this to let's say 0 or 21 or whatever we'd like,
I'll use 21 here.
Now we'll build and run.
You can see now it's printing out 21.
Okay so that gives you an idea. Now also
when we did not initialize it,
the compiler did give us a warning. I'll just right click here and compile.
Notice the warning that the compiler is giving me down here,
it's saying warning age is used uninitialized in this function.
That's a clue. Don't ignore those warnings.
The compiler is telling you be careful. You're using something that you never initialized.
Okay. So that gives you an idea of what's going on with uninitialized variables.
All right. So let me erase this, and let's write our program.
So what we need here is we need to ask the user for
the length of the room and the width of the room.
We'll grab those two values from cin,
then we'll multiply them together, and we'll display the square footage of the room.
Really, really simple program. But let's get started.
So what we'll do is we'll say cout, and remember, I don't have to use standard
scope resolution operator here because I'm using namespace.
So I'll say enter,
the width of the room
and we'll assume this is in feet.
We'll put a semicolon at the end of that line to terminate it.
Now we need to read something into a variable, right. So we need to do
cin into something.
Well, we need to declare that variable. Let's declare it right up here. We could call it width.
And it's going to be an integers, let's say. Remember, we've got to say the type,
followed by the name of the variable, followed by a semicolon.
This is perfectly fine.
Width is probably not a great name. Something like room width
is better.
It provides much more information to whoever's going to come later and modify this code.
So now we've got the room width
and I'm going to enter the room width and I'm going to read it into
room width.
Right now, we've got our variable. Now you'll notice a couple of things.
One is we never initialized room width.
In this case, it's probably okay because I'm going to read something into it before I use it
but best practice is let's initialize that.
We'll set it to zero.
Now we need another variable, right. We need the length of the room. So I'll just cut and paste this
to save a little time here.
And I'm going to say enter the length of the room.
And we're going to read that into another variable called room length.
And again, we'll initialize that to zero.
In this case, zero is fine. You could initialize it to anything that makes sense in your application.
All right, so now we'll read that one into room length. I'll just change this variable name right here.
So look at what's going on here. We've initialized two variables,
room width and room length. They're both integers.
Again, notice the syntax. The type name is integer,
followed by the name of the variable. And in this case, we're initializing it.
I'm prompting the user to enter the width of the room and I'm reading that integer into room width.
Then I'm prompting the user to enter the length of the room,
and I'm reading that integer into room length. All I have left to do now is just say cout,
and I'll say the area of the room
is and we'll do the calculation here right in line.
It's going to be room width times
room length.
And we may as well say square feet.
That's it.
We'll complete this with an end line,
and I'll clean a little bit of this space up just to make it a little bit more readable.
That's it. We're returning zero at the end.
Now let's build and run this.
We get a clean compile, and it says enter the width of the room. Let's say that the width is 10 feet.
Enter the length of the room, let's say it's 5 feet. Always remember,
we know what to expect, I expect a 50.
Press enter. It says the area of the room is 50 square feet.
So we get back what we expected. Always
think about what you're going to get back before you do this. Otherwise,
you may have errors that you didn't think about
or press any key to continue. That's it. That's a
pretty straightforward solution you can see how we use variables, how we can initialize variables,
how you can read into variables.
Now a best practice would be
rather than declare these two variables up here,
what we could do is we could declare them closer to where we're using them.
So in this case, this is the first use of room width.
So i could modify this by taking this line here and I'll just cut it
and I'll paste it right in here.
And I'll do the same with room length. I'll cut it from here,
and I'll paste it right in here.
And again, I'll just clean up the code a little bit, get rid of some of this extra white space.
Now you can see what's happening. I'm prompting the user.
I'm declaring that variable. I'm reading into it.
I'm prompting the user for the length. I'm declaring the variable. I'm reading into.
So this is basically what we talked about in the previous slides.
You can see this is a really simple example.
Try it out, play around with it. There'll be a challenge exercise
at the end of this section that's going to extend this a little bit.
So that's the end of this video. This shows you how to initialize variables,
use the variables. In the next video, we'll talk a little bit about
scope and global variables.