02 - Variables and Data Types
02 - Variables and Data Types
Lecture 2
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
#include <iostream>
4
Hello World
- Every C++ program must have a function called main().
- A function is basically a sequence of instructions for the
computer to execute.
- This main() function houses all of our instructions for our
program. This is where we will be writing our code.
int main() {
// Statements
}
5
Hello World
- This code uses a method known as cout (pronounced “see out”)
to send the text “Hello World!” to the terminal for output.
return 0;
6
Code → Save → Compile → Execute
- C++ is a compiled language. That means that to get a C++
program to run, you must first translate it from a human-
readable form to something a machine can “understand.” That
translation is done by a program called a compiler.
- When you program in C++, you mainly go through 4 phases
during development:
• Code — writing the program
• Save — saving the program
• Compile — compiling via the terminal
• Execute — executing via the terminal
- And repeat (debug the errors if needed).
7
Variables
- A variable is a name that represents a particular piece of the
computer’s memory that has been set aside for storing,
retrieving, and using data.
• Declaring a variable
• Declaring multiple variables
• Constant variables
8
Creating a Variable
- In order to create a variable, three pieces of information need to
be present:
• Data type: The type of data the variable will store.
• Name: The unique name of the variable. Names cannot start with
a number and cannot be a C++ keyword.
• Value: The value the variable will store. This value must match the
type of the variable.
9
Declaring a Variable
- Declaration means to create a variable and assign a value to it.
This is also called initialization.
- The common expression is “initialize name to value”:
type name = value;
- Style tip: Variable names should be all lowercase, with
underscores between words.
- For example, student_id or result.
10
Defining a Variable
- Definition means to create a variable without assigning a value to
it:
type name;
11
Assigning a Variable
- To assign or change the value of an existing variable, state the
name of the variable followed by the assignment operator (=)
and the new value:
name = new_value;
- Note: changing the value of an existing variable will overwrite the
old value.
- Let’s look at some examples of variable declaration and
assignment in C++:
// Define a variable called letter
char letter;
// Declare a variable called x
int x = 100;
12
Declaring Multiple Variables
- Multiple variables of the same type can be declared in a single
statement using a comma-separated list. For example:
int a = 1, int b = 2;
13
Declaring Multiple Variables
- The second mistake is trying to declare different types of
variables in the same statement:
// Correct syntax
int a = 1;
double b = 2.2;
14
Constant Variables
- Constant variables are variables with values that cannot be
changed after initialization.
- The purpose of a constant variable is to protect its value from
being accidentally altered elsewhere in the program.
- Constant variables are declared with the const keyword. For
example:
15
Constant Variables
- Constant variables must be initialized when they are declared.
Declaring a constant variable without providing a value will
cause an error:
16
Data Types
- A variable in C++ must be assigned a specific type. Type is
important because it tells the compiler how to interpret the
content of a variable.
- There are 5 basic data types that every C++ programmer should
know about:
Type Usage Examples
- Byte = 8 bit
- Kilo byte = 1024 byte
- Mega byte= 1024 kilobyte
- Gigabyte= 1024 megabyte
- Terabyte = 1024 gigabyte
18
double
- double stores floating-point numbers with decimals.
- A double variable requires 8 bytes of memory space and is
sufficient for storing up to 15 decimal digits.
double pi = 3.1415;
double height = 1.75;
19
Type Conversion
- It is possible to convert int into double and vice versa.
- This is called a type conversion or a type casting.
- The notation (type) value means “convert value to type“. So for
example:
// Converting a double to an int
double a = 3.5;
int b = (int) a; // b is now 3
21
string
- std::string stores a sequence of characters surrounded by double
quotes " ".
#include <string>
22
String Methods
- std::string comes with a lot of useful methods.
- Here are a few of them:
- The + operator can be used to combine strings together. This is
called concatenation:
23
String Methods
- The [] operator can be used to access the character at the
specified position in the string:
24
String Methods
- The length() function can be used to get the length of a string:
25
bool
- bool stores true or false boolean values. A bool variable requires
1 byte of memory space.
26
Example
- Calculate the Area of a Rectangle: create a program to
calculate the area of a rectangle (by multiplying the length and
width).
27
Example
- Calculate the Area of a Rectangle: create a program to
calculate the area of a rectangle (by multiplying the length and
width).
// Create integer variables
int length = 4;
int width = 6;
int area;