Section 3 Flashcards
Create an array with no content and 4 items that can go in it
string cars[4];
for-each Loop through a string array
// Create an array of strings
string cars[5] = {“Volvo”, “BMW”, “Ford”, “Mazda”, “Tesla”};
// Loop through strings
for (string car : cars) {
cout «_space;car «_space;“\n”;
}
Do you have to add a number for how many items are in your array?
No
Get the byte size of an array
int myNumbers[5] = {10, 20, 30, 40, 50};
cout «_space;sizeof(myNumbers);
How do you get the number of items in an array?
Take the bytes size and divide by the size of the first element
int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(myNumbers[0]);
cout «_space;getArrayLength;
for loop through an array the proper way
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(myNumbers[0]); i++) {
cout «_space;myNumbers[i] «_space;“\n”;
}
or
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout «_space;i «_space;“\n”;
}
What is a multidimensional array?
Create one and print the last letter of the first array
Multiple arrays in one
specify the amount of arrays in your main array, followed by the elements they have in them
string letters[2][4] = {
{ “A”, “B”, “C”, “D” },
{ “E”, “F”, “G”, “H” }
};
cout «_space;letters[0][3]
What are structs?
Structures
Group several related variables into one place. Each variable in a struct is a member
can contain multiple data types
Create a struct with two different data type variables with no value. place the struct in a variable named “myStructure”
Next, give them a value
Next print them
// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = “Hello World!”;
// Print members of myStructure
cout «_space;myStructure.myNum «_space;“\n”;
cout «_space;myStructure.myString «_space;“\n”;
Create a structure that contains three variables:
brand
model
year
Create two variables that contain the structure.
Assign values to all variables
print everything
struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here
// Put data into the first structure
myCar1.brand = “BMW”;
myCar1.model = “X5”;
myCar1.year = 1999;
// Put data into the second structure
myCar2.brand = “Ford”;
myCar2.model = “Mustang”;
myCar2.year = 1969;
// Print the structure members
cout «_space;myCar1.brand «_space;” “ «_space;myCar1.model «_space;” “ «_space;myCar1.year «_space;“\n”;
cout «_space;myCar2.brand «_space;” “ «_space;myCar2.model «_space;” “ «_space;myCar2.year «_space;“\n”;
Create a struct with the name mydataType
Declare a variable that uses the struct by it’s datatype
// Declare a structure named “car”
struct car {
string brand;
string model;
int year;
};
int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = “BMW”;
myCar1.model = “X5”;
myCar1.year = 1999;
// Create another car structure and store it in myCar2;
car myCar2;
myCar2.brand = “Ford”;
myCar2.model = “Mustang”;
myCar2.year = 1969;
// Print the structure members
cout «_space;myCar1.brand «_space;” “ «_space;myCar1.model «_space;” “ «_space;myCar1.year «_space;“\n”;
cout «_space;myCar2.brand «_space;” “ «_space;myCar2.model «_space;” “ «_space;myCar2.year «_space;“\n”;
return 0;
}
What is an enum?
Group of constants, sounds like a tuple
Create an enum list and then access it
include <iostream></iostream>
using namespace std;
enum Level {
LOW,
MEDIUM,
HIGH
};
int main() {
enum Level myVar = MEDIUM;
cout «_space;myVar;
return 0;
}
Change the values of LOW MEDIUM and HIGH in an enum
enum Level {
LOW = 25,
MEDIUM = 50,
HIGH = 75
};
int main() {
enum Level myVar = MEDIUM;
cout «_space;myVar;
return 0;
}
What is a reference variable?
Create one
It references another variable
string food = “Pizza”;
string &meal = food;
cout «_space;food «_space;“\n”; // Outputs Pizza
cout «_space;meal «_space;“\n”; // Outputs Pizza
What is a memory address?
Create a variable and get it’s memory address
Location where variable is stored on the computer.
string food = “Pizza”;
cout «_space;&food; // Outputs 0x6dfed4
What is a pointer?
Create one
Variable that stores it’s memory address as its value.
You create one with the “*’ operator.
string food = “Pizza”; // A food variable of type string
string* ptr = &food; // A pointer variable, with the name ptr, that stores the address of food
// Output the value of food (Pizza)
cout «_space;food «_space;“\n”;
// Output the memory address of food (0x6dfed4)
cout «_space;&food «_space;“\n”;
// Output the memory address of food with the pointer (0x6dfed4)
cout «_space;ptr «_space;“\n”;
What are three ways of declaring a pointer?
string* mystring; // Preferred
string *mystring;
string * mystring;
use the DEREFERENCE OPERATOR to get the value of a variable from the pointer
string food = “Pizza”; // Variable declaration
string* ptr = &food; // Pointer declaration
// Reference: Output the memory address of food with the pointer (0x6dfed4)
cout «_space;ptr «_space;“\n”;
// Dereference: Output the value of food with the pointer (Pizza)
cout «_space;*ptr «_space;“\n”;
*
When used in declaration (string* ptr), it creates a pointer variable.
When not used in declaration, it act as a dereference operator.
What happens if you change a pointer value using a dereference operator?
It will change the original variable’s value as well
string food = “Pizza”;
string* ptr = &food;
// Output the value of food (Pizza)
cout «_space;food «_space;“\n”;
// Output the memory address of food (0x6dfed4)
cout «_space;&food «_space;“\n”;
// Access the memory address of food and output its value (Pizza)
cout «_space;*ptr «_space;“\n”;
// Change the value of the pointer
*ptr = “Hamburger”;
// Output the new value of the pointer (Hamburger)
cout «_space;*ptr «_space;“\n”;
// Output the new value of the food variable (Hamburger)
cout «_space;food «_space;“\n”;
Declare a function and then call it
void myFunction() {
cout «_space;“I just got executed!”;
}
int main() {
myFunction(); // call the function
return 0;
}
void - this means the function doesn’t return a value
body - inside of the function
What are the two sections of a function called?
Declaration - return type, name of function and parameters
Definition - body
Can you put a function below the main function?
Can you declare a function and give it a definition later?
No
Yes
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
cout «_space;“I just got executed!”;
}
Create a function that takes a name as an argument (give it a parameter)
Have that function add a last name and then print to screen
void myFunction(string fname) {
cout «_space;fname «_space;” Refsnes\n”;
}
int main() {
myFunction(“Liam”);
myFunction(“Jenny”);
myFunction(“Anja”);
return 0;
}
Give a function a default parameter
void myFunction(string country = “Norway”) {
cout «_space;country «_space;“\n”;
}
int main() {
myFunction(“Sweden”);
myFunction(“India”);
myFunction();
myFunction(“USA”);
return 0;
}