C++ video notes USE THIS ON THE TEST Flashcards

1
Q

include <iostream> what does this do</iostream>

A

This includes the standard library in C++, and allows you to print out stuff or get input from the user using “std::”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does std::cout stand for?

A

It means character out.
Ex: std::cout &laquo_space;“Hello World!”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do you have to do return 0; for the main function?

A

It has an “int” return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is cout?

A

It’s an object that belongs to the std namespace. For example, std::cout &laquo_space;“Hello World!”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to have a constant value as your variable name?

A

You do const int value1 = 0; //still have to specify the data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to print out strings and variables on the same line?

A

you would first have to declare a variable, and then use the &laquo_space;to combine the two. For example

int value1 = 20;

std::cout &laquo_space;“x = “ &laquo_space;value1;

if you want a newline, you would do

std::cout &laquo_space;“x = “ &laquo_space;value1 &laquo_space;“\n”;

OR, you can do this (which is actually preferred)

std::cout &laquo_space;“x = “ &laquo_space;value1 &laquo_space;std::endl;

so it’s the same role as \n and you also use &laquo_space;in this scenario to add a newnline after your value or whatever (ykwim)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the way to not use std:: in your code to make it look cleaner (just include the object names)

A

you would do above your main function, “using namespace std;”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the insertion operator in C++?

A

It’s “«”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the benefit with cout combined with std::cin?

A

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 &laquo_space;“Enter a value: “ &laquo_space;std::endl;
int value1;
std::cin&raquo_space; value1;

//this doesn’t print out the value, but instead stores it in the variable value1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to memorize which way the &laquo_space;goes?

A

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 &laquo_space;“Enter the value here: “ &laquo_space;std::endl;
int value1;
std::cin&raquo_space; value1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Tip: When you get a value from the user(like user input), you can’t adjust that value

A

For example:

std::cout &laquo_space;“Enter a value: “;
int value1;
std::cin&raquo_space; value1 * 30;
you cannot do this for the cin, as cin is used for storing variables, and this is a mathematical expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the main function, what is the purpose of return 0;

A

It indicates when the program is done running

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Static type checking

A

This is when the integers and parameters data types are checked, and sees whether it’s matching before the main function runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the signature of a function mean?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you avoid typing std:: before every object name?

A

Below you including the preprocessor directive, you would do (without the hash) using namespace std;

for example
#include <iostream>
using namespace std;</iostream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or false, can you define a function signature without having the actual code and declarations?

A

Yes, then later onwards you can include the signature AND the code for that function, it will still work as normal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to use your function?

A

you just type the name of the function and then put the value as the parameter(inside of the paranthesis)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does #include <string> do in c++?</string>

A

It allows you to include string variables, unlike in C where you can declare arrays as strings, like char arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

class in C++

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you create a variable for this class and define the attribute values from that class?

A

You would have to use the period, for example

BankAccount value1;
value1.balance = …;
value1.name = …;

std::cout &laquo_space;value1.name &laquo_space;“has $” &laquo_space;value1.balance &laquo_space;std::endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

It’s just good practice to include endl.

A

OKAY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Review how to implement functions into classes

A

OKAY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the six phases of C++ development environment?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to define a namespace in C++?

A

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 &laquo_space;hello::value1;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

if, else statements are what you would see normally in C, like if (string1 == string2){
return …;
}

A

ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

For loops, while loops, and do while loops are also the same as C for the most part

A

Okay

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the difference between the size and sizeof function in C++?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Review how strings aren’t good with bounds checking

A

Ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What are the three parts of cin from the std namespace?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How to store multiple values using cin in C++?

A

You would do

using namespace std;
int value1;
int value2;

cout &laquo_space;“Enter 2 values of integer here: “ &laquo_space; endl;
cin&raquo_space; value1&raquo_space; value2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How does cin reading work for multiple values?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What happens if instead of spaces that are seperated out, the user puts enter, enter and enter

A

Instead of spaces, it is actually considered a newline (not only endl or \n is considered a newline)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How to create a struct in C++ (and how is it different from a class, review how it is different ion really get that)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What if you don’t do #include <string> at the top, how can you define a string?</string>

A

You can do std::string variableName = “…”;

35
Q

Just remember, std::cout &laquo_space;this way, not the arrow going the other way

A

OK

36
Q

do {
//insert the code
} while(x < 3);

A

This loop is different compared to the other ones because no matter what, the code actually compiles at the start, and then the while statement executes. So no matter what, the do while loop will always execute at least once, not zero times

37
Q

What is the end of file marker?

A

This is when the user keeps on putting input into the code and they are allowed to do that because of this statement

while (cin&raquo_space; a) //with a representing the user input {
… code or something maybe increasing count or something
}

If the user enters EOF, it indicates end of file marker, and so the while loop stops executing

38
Q

cin&raquo_space; Userinput
cout &laquo_space;output

A

That’s a good way of remembering it

39
Q

cin stream

A

Basically, any newline(like enter) or spaces between the values are considered to be one character as part of a stream

40
Q

Review std::getline(std::cin, line)

A

OK

41
Q

What is std::endl useful for?

A

It is useful for flushing the buffer as well as indicating a newline

42
Q

In addition to adding a newline, what does std::endl also guarentee?

A

It guarentees there being flushing the buffer, meaning the output (cout) will be printed immediately onto the screen

43
Q

True or false, a stream can NEVER be cleared

A

True, it is always just getting added on

44
Q

What does .get() and .put() do? (functions)

A

.get is used with cin and it takes one character from the stream every time, where .put prints out one character from the stream everytime and is used with cout

45
Q

Write a C++ program that reads a line of text from the user and outputs the line, but with the following conditions:

Replace every vowel (a, e, i, o, u, both uppercase and lowercase) with an asterisk (*).
The program should stop reading input once the user presses the Enter key (‘\n’).

A
46
Q

Do both cin stream and cout stream include newline and spaces?

A

YES

47
Q

How does a stream (cout or cin) remove all it’s characters?

A

Through the endl object

48
Q

try to review and practice streams

A

Ok

49
Q

What is a function prototype?

A

A function prototype does not include the definition of a function, but it instead includes the type that the variables have (float, double, int), the return type, and the function name

For example:

int array_sum(int[], int);

50
Q

How does the actual function definition differ from the function prototype?

A

It differs as the function definition provides the name of the parameter variables as well as the code and the return block

51
Q

How to call a function

A

You just have to put the function name and the variables:

ex if a function prototype is int functionName(int, double);

then, you can print it out because it has a return type

using namespace std;

cout &laquo_space; functionName(variable1, doubleVariable) &laquo_space;endl;

52
Q

Pass by value

A

When a value is passed inside of a function (as a parameter), it is actually a copy of that value, and it isn’t affected outside of that function

so for example,
function(value1);

let’s say that that function returns a certain value, the variabl, value1, doesn’t change

53
Q

Reference of variables

A

A reference of variable is written like this (affects an existing variable)

int i = 3;
int& j = i;

i++;
//both i gets added to and j does, so they both equal 4

j++;
//same thing happens, so it’s both 5

54
Q

“Pass by”

A

This usually refers to a value passed to a function somehow as a parameter

55
Q

Pass by Reference

A

When you pass a reference as a parameter for a function, that value gets affected and you can use it and print it out as normal. That’s why it’s different.

56
Q

Pointers of Variables

A

This is similar to C, but this is how it’s formatted:

int* ptr;
this is a pointer to an int value, and only an int value

int y = 4;
ptr = &y;

The way to dereference the pointer is

*ptr, which is equal to the value of y

57
Q

What exactly is a pointer?

A

A pointer stores the address of a variable

58
Q

How do you print out the memory address of a pointer?

A

Let’s say you declare something

int y = 4;
int* ptr;
//pointer to type int

ptr = &y;

*ptr //in this case is actually equal to 4, because you are dereferencing the pointer

std::cout &laquo_space;&ptr &laquo_space;std::endl;

59
Q

Below in the code, &i is the memory address of i, and j stores the memory address of i, *j dereferences the variable so that it’s equal to the value of the variable.
&j is actually the memory address of j

A

int main()
{
int I = 3;
int* j = &I;
cout &laquo_space;“Value of I is “ &laquo_space;i &laquo_space;endl;
cout &laquo_space;“Address of I is “ &laquo_space;&i &laquo_space;endl;
cout &laquo_space;“Value of j is “ &laquo_space;j &laquo_space;endl;
cout &laquo_space;“Address of j is “ &laquo_space;&j &laquo_space;endl;
cout &laquo_space;“Value of where j is pointing “ &laquo_space;*j &laquo_space;endl;

} in here, is the value of &i the same as j, the value of i is the same as *j, but what does &j do or mean

60
Q

what does int* mean?

A

It’s a type, and it means it’s a pointer for the int type

61
Q

We learned how to pass by value, and pass by reference, how do you pass by pointer?

A

Let’s say you have a function

void function1(int* value1){
*value1 = *value1++;
}

if we use this function in the main function, you state the address of a variable, and it also gets affected like pass by reference, and unlike pass by value.

An example of this is

int main{
int a = 2;
function1(&a);
// so in this case int* value1 = &a, meaning value1 is equal to the memory address of a, the value of a gets affected too, being equal to 3 after this function executes
}

62
Q

Review the Pros and Cons of each one (pass by value, pass by reference, pass by pointer)

A

OK

62
Q

How do you pass a function as a parameter? (I don’t really get how this works review this, might just be somethign that you memorize)

A

For example,

void function1(void(*animal)(), int count){
animal()
}

Is the animal function a void function that has no parameter

if I were to do void function1(int(*function2)(double), double), that would be a pointer to an int function with a double as a parameter, the statement

function2(2.03); could be used within that function

63
Q

What is set precision?

A

include <iostream></iostream>

Set precision is instead of getting from the library of #include <iostream>, it's from iomanip. It is done by implementing std::setprecision(# of integers) (with std indicating the namespace in C++)</iostream>

An example code is:

#include <iomanip></iomanip>

int main(){
double value1 = 3.1415;
std::cout &laquo_space;value1 &laquo_space;setprecision(3) &laquo_space;std::endl;
} this code will print out 3.14

63
Q

What is the breakdown of this code?

A

In this code below, when the CPU approaches foo(i), the CPU makes sure it has the memory address of the next line’s code, and also stores the int value of i into the stack. “#include <iostream>
using namespace std;</iostream>

void foo(int x) {
x++;
cout &laquo_space;“Value of x is “ &laquo_space;x &laquo_space;endl;
}

int main() {
int i = 3;
foo(i);
cout &laquo_space;“Value of i is “ &laquo_space;i &laquo_space;endl;
return 0;
}”

63
Q

Function Overload

A

If functions have the same name, if they have different type of parameters or more or less parameters than the others, they can still be distinguishable when you fall them. However, return type has no affect on this.

64
Q

What are default arguments?

A

Instead of just leaving parameter variables, or arguments blank in a function definition (for example, int function1(int value2){
value2++;
return value2;
})
you can actually define these values, so instead I can say function1(value2 = 1){
value2++;
return value2;
}
you can also override these values by putting actual values into the parenthesis

65
Q

What if you want to combine both the required values and the optional values?

A

You would have to put the required ones and then optional

Ex: boxVolume(int value1, int value2, int value3 = 1){

}

66
Q

What are the sizes of doubles and floats?

A

A float is 4 bytes and double is 8 bytes. Therefore, doubles are usually longer than floats

67
Q

What are templates?

A

Templates can either be defined with template <typename> or template <class>. They allow for any type of value (int, double, float) to be inputted as a parameter</class></typename>

68
Q

What is the difference between template <typename> and template <class>?</class></typename>

A

There’s literally no difference, you can use either one

69
Q

What is an example of how a template can be used in C++, and what is an example of that function being called?

A

include <iostream></iostream>

template <typename></typename>

T compare(T value1, T value2){
if (value1 > value2){
return value1;
}
else{
return value2;
}
}

int main() {
compare(1, 2);
compare(1.1, 2.2);
int value3 = compare(1.1111111,2.2222222);
std::cout &laquo_space;compare(1.1, 2.2) &laquo_space;std::endl;

return 0; }
70
Q

How can you also have two different types of template types?

A

include <iostream></iostream>

using namespace std;

template <typename T, typename U>

T function1(T value1, U value2){
return value1 + value2;
}

int main() {
cout &laquo_space;function1(3.5, 3) &laquo_space;endl;
return 0;
} if you do the opposite, it rounds down, so rather than being 6.5, it is equal to 6

71
Q

True or false, you can use template types that are already defined in order to create variables of that type

A

True, you can do this:

T function1(T value1){
T value2 = value1;
return value2;
}

72
Q

Template type casting

A

int main()
{
cout &laquo_space;sum<double, double>(3.5, 1) &laquo_space;endl;
return 0;
} this code manually sets both values (double and double) to be 3.5 and 1.0

73
Q

Pass by reference is an address sign, whereas pass by pointer is a * sign, both as parameters

A

OK DUMBASS

74
Q

Templates Vs. Overloading

A

Use templates when anything can work with different types(for the function), but make different functions and overload it if you want to do different things that aren’t compatible with all data types

75
Q

What are the different sizes of the data types?

A

This information is on page 8 of slides

76
Q

How is a function prototype different from a function signature?

A

A function prototype does not contain the parameter variables, but rather contains just the data types, whereas a function signature has the actual parameter variables, and below it is the actual definition

77
Q

What does std::getline do?

A

It stores a string name as a whole(including spaces). There is a difference between this and std::cin&raquo_space;. Look at the code below:

“#include <iostream>
#include <string>
using namespace std;</string></iostream>

int main(){
cout &laquo_space;“What is the value that you want to enter?” &laquo_space;endl;
string value a;
cin&raquo_space; a;
return 0;

}” If the user enters “Hello Hi”, only “Hello” will be stored in the a variable, and “Hi” will be ignored.

However, with this code:
“ int main(){
cout &laquo_space;“What is a string value you want to enter” &laquo_space;endl;
string a;
getline(cin, a);
return 0;
} will continue storing string variables until the user presses enter, indicating endl

78
Q

What is the format for getline?

A

It’s std::getline(std::cin, variableName);

79
Q

The stream for cin and cout just includes what is being put in to the variable(input) and the output(includes integers, decimals, endline, like \n (also indicates enter), and spaces) and I think endl flushes the stream

A

Ok??

80
Q
A