C++ video notes USE THIS ON THE TEST Flashcards
include <iostream> what does this do</iostream>
This includes the standard library in C++, and allows you to print out stuff or get input from the user using “std::”
What does std::cout stand for?
It means character out.
Ex: std::cout «_space;“Hello World!”;
Why do you have to do return 0; for the main function?
It has an “int” return type
What is cout?
It’s an object that belongs to the std namespace. For example, std::cout «_space;“Hello World!”;
How to have a constant value as your variable name?
You do const int value1 = 0; //still have to specify the data type
How to print out strings and variables on the same line?
you would first have to declare a variable, and then use the «_space;to combine the two. For example
int value1 = 20;
std::cout «_space;“x = “ «_space;value1;
if you want a newline, you would do
std::cout «_space;“x = “ «_space;value1 «_space;“\n”;
OR, you can do this (which is actually preferred)
std::cout «_space;“x = “ «_space;value1 «_space;std::endl;
so it’s the same role as \n and you also use «_space;in this scenario to add a newnline after your value or whatever (ykwim)
What is the way to not use std:: in your code to make it look cleaner (just include the object names)
you would do above your main function, “using namespace std;”
What is the insertion operator in C++?
It’s “«”
What is the benefit with cout combined with std::cin?
With cout, you can also get input from the user by printing out a statement and then declaring a value for the user to input. Then, with std::cin, you can get that user input, and store it in the variable
example:
std::cout «_space;“Enter a value: “ «_space;std::endl;
int value1;
std::cin»_space; value1;
//this doesn’t print out the value, but instead stores it in the variable value1
How to memorize which way the «_space;goes?
For cout, it is going inward because you are getting user info, whereas for cin, it is going the other way because you are storing that information in a variable
Ex:
std::cout «_space;“Enter the value here: “ «_space;std::endl;
int value1;
std::cin»_space; value1;
Tip: When you get a value from the user(like user input), you can’t adjust that value
For example:
std::cout «_space;“Enter a value: “;
int value1;
std::cin»_space; value1 * 30;
you cannot do this for the cin, as cin is used for storing variables, and this is a mathematical expression
In the main function, what is the purpose of return 0;
It indicates when the program is done running
Static type checking
This is when the integers and parameters data types are checked, and sees whether it’s matching before the main function runs.
What does the signature of a function mean?
It is the line before the code of a functions which is surrounded by brackets, and includes the return type, the parameters, and the name of the function
For example,
void function_name (int value, double value2){
// insert all the code here, newline is the other direction btw
}
How do you avoid typing std:: before every object name?
Below you including the preprocessor directive, you would do (without the hash) using namespace std;
for example
#include <iostream>
using namespace std;</iostream>
True or false, can you define a function signature without having the actual code and declarations?
Yes, then later onwards you can include the signature AND the code for that function, it will still work as normal
How to use your function?
you just type the name of the function and then put the value as the parameter(inside of the paranthesis)
What does #include <string> do in c++?</string>
It allows you to include string variables, unlike in C where you can declare arrays as strings, like char arrays
class in C++
This contains attributes of that specific class name and you can create objects of that class
For example,
class BankAccount{
int balance;
string name;
};
These attributes are key for what defines a bank account
How do you create a variable for this class and define the attribute values from that class?
You would have to use the period, for example
BankAccount value1;
value1.balance = …;
value1.name = …;
std::cout «_space;value1.name «_space;“has $” «_space;value1.balance «_space;std::endl;
It’s just good practice to include endl.
OKAY
Review how to implement functions into classes
OKAY
What are the six phases of C++ development environment?
The first phase is edit, which is basically writing the code and putting it into an editor in proper format
The second phase is preprocessor, this is what is run at first, and is executed automatically before compiling
Ex: #include <iostream>
The third phase is the compile phase, and this is when the actual code starts to be put into code that the machine can understand</iostream>
The fourth phase is the link phase, which is used to link what you have as the preprocessor directive (ex: #include <iostream>) and also the functions and objects from that namespace in order for your code to work properly</iostream>
The fifth phase is when the computer uses RAM(it takes stuff from the hard drive and uses it’s computer memory to finally execute the code)
Review the fifth and sixth phase
How to define a namespace in C++?
A namespace allows you to have the same variable names across different name namespaces to avoid confusion, as you can just do different_namespace …
an example of a namespace is std, as you can see, we use std by including two colons after std
When we create a namespace, we can put namespace::attribute or whatever variable or function or object is found within that namespace
Declaration of namespace:
namespace hello{
int value1;
}
int main() {
std::count «_space;hello::value1;
return 0;
}
if, else statements are what you would see normally in C, like if (string1 == string2){
return …;
}
ok
For loops, while loops, and do while loops are also the same as C for the most part
Okay
What is the difference between the size and sizeof function in C++?
value.sizeof(int) or whatever data type(float, double, string, long) gets the size of the data type and puts it in an integer value, whereas .size() which is next to a variable name of a string gets the length of the string
Review how strings aren’t good with bounds checking
Ok
What are the three parts of cin from the std namespace?
Reading the value:
The user types in the input and hits enter to end the input
Transmit:
Enter indicates your machine to make your input into a buffer
Store:
Inspects the variable type and stores it into the variable
How to store multiple values using cin in C++?
You would do
using namespace std;
int value1;
int value2;
cout «_space;“Enter 2 values of integer here: “ «_space; endl;
cin»_space; value1»_space; value2;
How does cin reading work for multiple values?
It basically notices whether there is a space between the two values, and in that place where there are spaces, it is called a skip delimeter and moves on to the next value or the next data type
Also, newline is also considered a delimeter
What happens if instead of spaces that are seperated out, the user puts enter, enter and enter
Instead of spaces, it is actually considered a newline (not only endl or \n is considered a newline)
How to create a struct in C++ (and how is it different from a class, review how it is different ion really get that)
you create a struct by doing this:
struct Name{
int value;
string value1;
};
and then you can create a struct variable by doing this
Name variable;
variable.value;
variable.value1;
and you can set those attributes to actual values, also struct automatically have public attributes unlike classes