Section One Flashcards
What do you need to write C++
A text editor like notepad
A compiler like GCC to translate the code into a language that the computer will understand
What is an IDE
Integrated Development Environment
What compiler are you using
GCC from mysys2.org
What is a header file library?
Show an example of one.
include <iostream></iostream>
Allows us to work with input and output objects.
Make your program able to use names for objects and variables from the standard library
include <iostream></iostream>
using namespace std;
What function is always required in your code? What happens to the code inside of it?
int main() {
}
Code inside curly brackets is always executed.
What is an insertion operator?
Use it with an object to use hello world
int main() {
cout «_space;“Hello World!”;
return 0;
}
«_space;- insertion object
What does every statement end with?
;
How do you end the main function?
return 0
What is a computer program?
List of instructions to be executed by a computer.
What are the instructions that you give your computer called?
statements
Add the numbers 3 + 3
include <iostream></iostream>
using namespace std;
int main() {
cout «_space;3 + 3;
return 0;
}
How do you insert a new line the popular way?
include <iostream></iostream>
\n - new line character that is an escape sequence
You can also do this
using namespace std;
int main() {
cout «_space;“Hello World!” «_space;“\n”;
cout «_space;“I am learning C++”;
return 0;
}
Other than \n, what is another way to create a blank line?
include <iostream></iostream>
using namespace std;
int main() {
cout «_space;“Hello World!” «_space;endl;
cout «_space;“I am learning C++”;
return 0;
}
How would you:
tab
insert a backslash
insert a double quote
\t
"
EXAMPLE
#include <iostream>
using namespace std;</iostream>
int main() {
cout «_space;“They call him "Johnny".”;
return 0;
}
How do you make a comment?
include <iostream></iostream>
using namespace std;
int main() {
// This is a comment
cout «_space;“Hello World!”;
return 0;
}
OR
cout «_space;“Hello World!”; // This is a comment
Create a multiline comment
include <iostream></iostream>
using namespace std;
int main() {
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout «_space;“Hello World!”;
return 0;
}
What are the 5 variable types
int
double - float
char - single character
string
bool
Create a variable named myNum and give it the value of 15
Print to screen
include <iostream></iostream>
using namespace std;
int main() {
int myNum = 15;
cout «_space;myNum;
return 0;
}
OR ASSIGN THE VALUE LATER
int myNum;
myNum = 15;
cout «_space;myNum;
Create a variable and use it in a string to output the statement:
‘I am 34 years old’
include <iostream></iostream>
using namespace std;
int main() {
int myAge = 35;
cout «_space;“I am “ «_space;myAge «_space;” years old.”;
return 0;
}
Declare several integer variables
int x = 5, y = 6, z = 50;
Create 3 integers and give them all the same value
include <iostream></iostream>
using namespace std;
int main() {
int x, y, z;
x = y = z = 50;
cout «_space;x + y + z;
return 0;
}
What are identifiers?
Variables must be identified with unique names
So basically variable names
What do variables have to start with and what can they contain?
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
What is a constant?
Make one
Read-only variable. It can’t be changed.
They HAVE TO contain a variable
const int myNum = 15;
Put user input into a variable and print it
You’ll need to use an extraction operator
int x;
cout «_space;“Type a number: “; // Type a number and press enter
cin»_space; x; // Get user input from the keyboard
cout «_space;“Your number is: “ «_space;x; // Display the input value
Using user input, create a calculator that takes two numbers and adds them together
int x, y;
int sum;
cout «_space;“Type a number: “;
cin»_space; x;
cout «_space;“Type another number: “;
cin»_space; y;
sum = x + y;
cout «_space;“Sum is: “ «_space;sum;
What are the sizes of the below data types:
bool
char
int
double
bool - 1 byte
char - 1 byte
int - 2 or 4 bytes
double 8 bytes
Difference between a float and a double
The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.
Code a boolean variable
bool isCodingFun = true;
bool isFishTasty = false;
cout «_space;isCodingFun; // Outputs 1 (true)
cout «_space;isFishTasty; // Outputs 0 (false)
Should you put a single character in single or double quotes?
single
Do you need to include a certain library to use strings?
// Include the string library
#include <string></string>
// Create a string variable
string greeting = “Hello”;
// Output string value
cout «_space;greeting;
Using variables
create a program that takes
amount = 50
cost per item = 9.99
total cost = multiply here
currency = $ - Just make the dollar sign in the variable to use in your program.
include <iostream></iostream>
using namespace std;
int main() {
// Create variables of different data types
int items = 50;
double cost_per_item = 9.99;
double total_cost = items * cost_per_item;
char currency = ‘$’;
// Print variables
cout «_space;“Number of items: “ «_space;items «_space;“\n”;
cout «_space;“Cost per item: “ «_space;cost_per_item «_space;”” «_space;currency «_space;“\n”;
cout «_space;“Total cost = “ «_space;total_cost «_space;”” «_space;currency «_space;“\n”;
return 0;
}
How do you increase a number by one?
int x = 10;
++x;