C++ video notes 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>