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

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)

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

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

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;

21
Q

It’s just good practice to include endl.

A

OKAY

22
Q

Review how to implement functions into classes

A

OKAY

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

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;
}

25
Q

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

A

ok

26
Q

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

A

Okay

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

28
Q

Review how strings aren’t good with bounds checking

A

Ok

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

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;

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

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)

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

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
A