HEAD
Unit 03: C++ Basics
This week's stuff:
At minimum, a C++ program requires one file - a .cpp source file.
Usually programs are bigger and contain multiple files, like .h header files as well as other .cpp source files.
The file that contains the program starting point is usually named main.cpp, but it doesn't have to have this name.
A C++ source file is technically just a plaintext file, which can be opened in a text editor like notepad,
though there are better code editors than notepad.
Every C++ program requires a main() function as its starting point. The most basic C++ program must have the following:
int main()
{
return 0;
}
For now you can take this code for granted but you will understand it more as the class continues. But, some basic info:
Additionally, C++ is case sensitive, so capitalization matters. Make sure you write main() in all lower case - Main() and MAIN() won't work!
;
int total_cats;
total_cats = 10;
int result = number1 * number2;
cout << "Hello!";
In order to display text to the screen with our program, we need to include a library.A library is a set of pre-written code that can be used across multiple programs. C++ contains a standard library of pre-written code we can use. To gain access to the cout (console-output) statement, we need to add this at the top of our C++ program file:
#include <iostream>
After that, we can use the cout command to display text like this:
std::cout << "Hello!";
The std:: prefix stands for "standard", as in the C++ standard library.
This command will display "Hello!" to the screen when the program is built and run.
You can also add empty lines to the program using the endl (end-line) command:
std::cout << std::endl;
And these commands can be chained together:
std::cout << "Hello!" << std::endl << "World!" << std::endl;
The << sign is known as the output stream operator.
We can leave comments in our code. Comments are meant for humans, and not the computer - the compiler just ignores these lines. But, they can be handy to help clarify what's going on in your program.
A single line comment begins with two forward slashes:
// Note: formula for area
Sometimes you need more text, so you can use a multi-line comment. These begin with /*
and end with */
like this:
/*
MY PROGRAM
This program does abcxyz stuff.
*/
We're going to be making a lot of errors during this semester and throughout our programming career! As you keep practicing, you'll get better at the diagnosis and fixing process, but bugs will always occur. I know they can be frustrating, but it's a normal part of development. Here are some tips...
We're going to be making a lot of errors during this semester and throughout our programming career! As you keep practicing, you'll get better at the diagnosis and fixing process, but bugs will always occur. I know they can be frustrating, but it's a normal part of development. Here are some tips...
Data Type | Example values |
---|---|
int | -5, 1, 45 |
float | 98.5, 7.75 |
string | "Username", "HI!" |
char | '$', 'x' |
bool | true, false |
Variable names can contain letters, numbers, and underscores - but NO SPACES!
Variable declaration: In our C++ programs, we need to tell our program about a variable before we start using it. This type of instruction is called a variable declaration. To do this, we specify the variable's DATA TYPE and give it a NAME.
string username;
int total_followers;
Programs flow from the top to the bottom (until we get to functions :) so before you use any variables, they must be declared above.
Variable assignment:
You can assign values to variables in C++ using the assignment operator, =
,
like this:
username = "MooseInvader";
total_followers = 12345;
You can also assign a value to a variable during its declaration. This is known as initializing the variable:
string username = "Texpert";
With numeric variables you may need to do arithmetic with them. The basic arithmetic operators are:
Operation | Operator | Example code |
---|---|---|
Add | + | total_posts = video_post_count + text_post_count; |
Subtract | - | total_followers = total_followers - 1; |
Multiply | * | area = width * length; |
Divide | / | price_per_pizza = 9.99 / 3; |
When writing a command statement with a math expression, the LHS (left-hand-side) of the equal sign is WHERE TO STORE THE RESULT. The RHS (right-hand-side) of the equal sign is THE MATH EXPRESSION TO BE SOLVED.
We can use the cout
command (pronounced "C-Out" for Console Output)
to display text to the screen. We can display string literals "like this"
-
which are hard-coded text we put in - or we can display variable values.
Note that to use output and input commands, we need to add #include <iostream>
at the top of our code file.
This is a library of pre-written code we can utilize.
#include <iostream>
using namespace std;
int main()
{
// endl means "end line", starts the next text on a new line.
cout << "Hello," << endl;
cout << "world!" << endl;
return 0;
}
If we want to display a variable's value, we put the variable's name in the cout command:
cout << total_cats << endl;
And when displaying variable values, it's best to add a string-literal label so that the user knows what the data they're looking at means.
cout << "Total cats I have: " << total_cats << endl;
Often we want our programs to be interactive, so that the user can interface
with it and our program can respond to the user's input. The first step to this is getting
input from the keyboard and storing that input in a variable. We do this with the
cin
("C-in", Console Input) command:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your favorite number: " << endl;
int favorite_number;
cin >> favorite_number; // Getting input and storing in favorite_number variable
cout << "Your favorite number is " << favorite_number << endl;
return 0;
}
If we want to get string text, we can use cin >>
,
but that won't capture any spaces. If you want to get information like a name, address, etc.,
you will probably want to use the getline
function instead:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your username: ";
string username;
getline( cin, username ); // Get a full line of text
return 0;
}
However, if you inter-mix cin >> ...
and getline( cin, ... )
,
then we will run into some buffer issues. If your program is skipping an input, that means you
need to add a cin.ignore();
to your program. This ONLY needs to happen when going FROM
cin >> ...
TO getline( cin, ... )
:
int pin;
string username;
cout << "Enter your PIN: ";
cin >> pin;
cin.ignore(); // Flush the input buffer
cout << "Enter your username: ";
getline( cin, username ); // Get a full line of text
In-class example code:
Archived videos and class lectures:
Unit 03: C++ Basics
This week's stuff:
At minimum, a C++ program requires one file - a .cpp source file.
Usually programs are bigger and contain multiple files, like .h header files as well as other .cpp source files.
The file that contains the program starting point is usually named main.cpp, but it doesn't have to have this name.
A C++ source file is technically just a plaintext file, which can be opened in a text editor like notepad,
though there are better code editors than notepad.
Every C++ program requires a main() function as its starting point. The most basic C++ program must have the following:
int main()
{
return 0;
}
For now you can take this code for granted but you will understand it more as the class continues. But, some basic info:
Additionally, C++ is case sensitive, so capitalization matters. Make sure you write main() in all lower case - Main() and MAIN() won't work!
;
int total_cats;
total_cats = 10;
int result = number1 * number2;
cout << "Hello!";
In order to display text to the screen with our program, we need to include a library.A library is a set of pre-written code that can be used across multiple programs. C++ contains a standard library of pre-written code we can use. To gain access to the cout (console-output) statement, we need to add this at the top of our C++ program file:
#include <iostream>
After that, we can use the cout command to display text like this:
std::cout << "Hello!";
The std:: prefix stands for "standard", as in the C++ standard library.
This command will display "Hello!" to the screen when the program is built and run.
You can also add empty lines to the program using the endl (end-line) command:
std::cout << std::endl;
And these commands can be chained together:
std::cout << "Hello!" << std::endl << "World!" << std::endl;
The << sign is known as the output stream operator.
We can leave comments in our code. Comments are meant for humans, and not the computer - the compiler just ignores these lines. But, they can be handy to help clarify what's going on in your program.
A single line comment begins with two forward slashes:
// Note: formula for area
Sometimes you need more text, so you can use a multi-line comment. These begin with /*
and end with */
like this:
/*
MY PROGRAM
This program does abcxyz stuff.
*/
We're going to be making a lot of errors during this semester and throughout our programming career! As you keep practicing, you'll get better at the diagnosis and fixing process, but bugs will always occur. I know they can be frustrating, but it's a normal part of development. Here are some tips...
We're going to be making a lot of errors during this semester and throughout our programming career! As you keep practicing, you'll get better at the diagnosis and fixing process, but bugs will always occur. I know they can be frustrating, but it's a normal part of development. Here are some tips...
Data Type | Example values |
---|---|
int | -5, 1, 45 |
float | 98.5, 7.75 |
string | "Username", "HI!" |
char | '$', 'x' |
bool | true, false |
Variable names can contain letters, numbers, and underscores - but NO SPACES!
Variable declaration: In our C++ programs, we need to tell our program about a variable before we start using it. This type of instruction is called a variable declaration. To do this, we specify the variable's DATA TYPE and give it a NAME.
string username;
int total_followers;
Programs flow from the top to the bottom (until we get to functions :) so before you use any variables, they must be declared above.
Variable assignment:
You can assign values to variables in C++ using the assignment operator, =
,
like this:
username = "MooseInvader";
total_followers = 12345;
You can also assign a value to a variable during its declaration. This is known as initializing the variable:
string username = "Texpert";
With numeric variables you may need to do arithmetic with them. The basic arithmetic operators are:
Operation | Operator | Example code |
---|---|---|
Add | + | total_posts = video_post_count + text_post_count; |
Subtract | - | total_followers = total_followers - 1; |
Multiply | * | area = width * length; |
Divide | / | price_per_pizza = 9.99 / 3; |
When writing a command statement with a math expression, the LHS (left-hand-side) of the equal sign is WHERE TO STORE THE RESULT. The RHS (right-hand-side) of the equal sign is THE MATH EXPRESSION TO BE SOLVED.
We can use the cout
command (pronounced "C-Out" for Console Output)
to display text to the screen. We can display string literals "like this"
-
which are hard-coded text we put in - or we can display variable values.
Note that to use output and input commands, we need to add #include <iostream>
at the top of our code file.
This is a library of pre-written code we can utilize.
#include <iostream>
using namespace std;
int main()
{
// endl means "end line", starts the next text on a new line.
cout << "Hello," << endl;
cout << "world!" << endl;
return 0;
}
If we want to display a variable's value, we put the variable's name in the cout command:
cout << total_cats << endl;
And when displaying variable values, it's best to add a string-literal label so that the user knows what the data they're looking at means.
cout << "Total cats I have: " << total_cats << endl;
Often we want our programs to be interactive, so that the user can interface
with it and our program can respond to the user's input. The first step to this is getting
input from the keyboard and storing that input in a variable. We do this with the
cin
("C-in", Console Input) command:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your favorite number: " << endl;
int favorite_number;
cin >> favorite_number; // Getting input and storing in favorite_number variable
cout << "Your favorite number is " << favorite_number << endl;
return 0;
}
If we want to get string text, we can use cin >>
,
but that won't capture any spaces. If you want to get information like a name, address, etc.,
you will probably want to use the getline
function instead:
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your username: ";
string username;
getline( cin, username ); // Get a full line of text
return 0;
}
However, if you inter-mix cin >> ...
and getline( cin, ... )
,
then we will run into some buffer issues. If your program is skipping an input, that means you
need to add a cin.ignore();
to your program. This ONLY needs to happen when going FROM
cin >> ...
TO getline( cin, ... )
:
int pin;
string username;
cout << "Enter your PIN: ";
cin >> pin;
cin.ignore(); // Flush the input buffer
cout << "Enter your username: ";
getline( cin, username ); // Get a full line of text
In-class example code:
Archived videos and class lectures: