Lecture 2: C++ Basics Flashcards
To use functions and objects in libraries, we must
Include them in our code and by doing this with an include directive at the top of our file.
Ex:
#include <iostream></iostream>
A namespace is
A collection of names (such as cin and cout)
An object named cout exists in the std (or standard) namespace. It might also exist in other namespaces, we instruct C++ to use the
Cout from the std namespace. Example of:
using namespace std;
When runs a program, executions always begins at the
Main function
The return 0 at the end of our main function tells to
End the program here
Ex:
cout «_space;“Hello World”;
return 0;
Use a comment to write something in the program that the compiler will
Ignore
We can also place a comment on the same line as a statement. This is referred to as an
Incline comment
To create a multi-line comment, we can also use the following technique:
/Print “Hellow World” to the screen
These lines are ignored by the compiler
End of comment /
A variable can hold data of
A specific type and for now, we will only use variables that hold numbers
When we declare a variable, we tells to set aside a location in
Main memory large enough to hold a value of the given type
The declaration statement tells to:
1) Set aside a location in memory large enough to hold an integer
2) Given that memory location the name
Can place a value in or retrieve a value from a variable’s memory location by using its
Name
This code declares three variables. When they are created, they are each assigned a different
Memory address
The addresses assigned might be different each time the program runs. It depends on
What memory is free at the moment
Only need to use a variable’s name to access its
Memory address
The name of a variable is also called an
Identifier
Each identifier’s name must be unique in its
Scope (more on scope later) and the program only have one scope, so all identifier must be unique
C++ is a
Case sensitive language and this means the difference between uppercase and lowercase letters matters
A keyword or reserved word is
A special type of identifier
Ex: int, double, void, bool, for, while
Naming conventions - if a variable name is one word, it is written in lowercase
Ex: int number;
If a variable name has more than one word, it is written in
Camelcase
Ex: int numberOfBars
Since an identifier cannot have spaces, all words must be combined into
One word
Assignment statement
Ex: totalWeight = 5;
An assignment statement uses the “ = “ operator, also known as the
Assignment operator
Unlike in math, “ = “ does not assert that its left and right operands are
Equal, rather it takes the value on the right and assigns it to a variable on the left