Lecture 3 Zoom Q&A


Q: Hi, should we really do “using namespace std”? I don’t recommend “using namespace std.” for two reasons. 1) If we try to read and debug large projects that include multiple libraries, we’re unable to tell from which libraries certain functions come from. 2) If we use multiple libraries that have similar syntax of namespaces and use multiple namespaces, there will be a silent compiler error and function conflicts may arise. The only reason why there’s no conflict cctype library and the standard library is because syntactically the functions in the cctype library are named differently and so the compiler knows which functions to apply “using namespace std” to.

A1: live answered


Q: There’s nothing to turn in for assignment zero right? I can’t see anything on paperless.

A1: Just fill in the Google form, nothing on Paperless.


Q: Are we supposed to read ch 4 (streams)? Slides and schedule seem to have conflicting information

A1: Reading is optional/supplementary. That said today’s reading was for chapter 5

A2: We won’t spend much time in lecture on streams, you will want to do that reading to pick up the background info


Q: Wait where is this form for assignment 0

A1: Linked to on the assignment page


Q: Is it available other quarters? or only fall?

A1: live answered


Q: how often do they meet?

A1: live answered


Q: Can we audit the course?

A1: live answered


Q: whats auditing?

A1: Following along with class, but not officially enrolling, not being graded, etc


Q: C++ STL library uses snake case for its functions, but the stanford library uses camel case for its functions. Would that be a possible way to tell which functions come from which library without referring to the namsepace?

A1: Sharp eyes! Difference in case does help you to identify which library a feature belongs to.


Q: can you compare variables of different types using == ? for example an int 4 and a double 4.0 would that return true, false, or not even work?

A1: live answered


Q: I'm confused about not a, could you re explain it?

A1: It inverts the truth value, if a were true, not a evalutes to false and vice versa


Q: does c++ have the bitwise XOR operator (like how Java has ^)?

A1: yes, although we wont use it in this class


Q: Are booleans declared true or false, or are they declared with 0 as false and anything else is true?

A1: We’ll use true/false for boolean values in this class.


Q: if I compare int a =5 and float b = 5.34 and ask Cpp which one is bigger, what result can i expect?

A1: the float will be larger


Q: why is there 2 “,«endl«” at the last line?

A1: it creates two new lines


Q: if you do more than three if statements are all the rest of them “else”?

A1: no, all of them would be evaluated unless using else if


Q: Can we still get enough experience for interview questions from CS106B without taking CS106L?

A1: yes


Q: Can a floating-point number be compared to an integer? For example, would 1.0 == 1 return as true?

A1: It would convert the integer to float and yes, would evaluate to true, but there can be cases where the converstion between types int-float produces some roundoff surprises, so there is some pitfalls when comingling types uncarefully


Q: Wait, how come you can't declare functions within the scope of another function in C++?

A1: live answered


Q: were we supposed to get a confirmation email when we submitted assignment 0?

A1: no

A2: no. as longas you hit submit you should be fine


Q: Do all functions (not just the main function) need to return an integer?

A1: No, different functions can have different return types


Q: Does C++ have access specifiers?

A1: On member functions defined with a class, yes, but not for independent functions


Q: for function declarations


Q: Do all functions need to return something? Is that why main returns an int?

A1: no, a void function doesn’t return anything

A2: main has to return int because of C++ rules (main is special)


Q: Does varType mean int, float, double etc? Or is it a variable type itself in the stanford library?

A1: varType was a placeholder in that example. Thanks for that clarification!


Q: can you have a non-decimal double?

A1: live answered


Q: why does it only round to 4 digits? Can we specify with cout or do we need to use some sort of printf?

A1: live answered


Q: why do we need cout «< endl for the printTenTimes function

A1: live answered


Q: if you want a void function to end early can you use return; ?

A1: yes

A2: Yes!


Q: Would you have to define char c before passing it thru?

A1: yes

A2: You could also pass a character constant as the argument, i.e. printTenTimes(‘D’)


Q: if you declare a function but dont define it, what does the syntax look like for defining it later?

A1: I think Chris will talk about function prototypes a little later on!


Q: Does printTenTimes(‘a’) == printTenTimes(‘b’)?

A1: live answered


Q: how is void different from just typing return 0 at the end or a int function

A1: a void function will return nothing (no value) while an int function returning 0 is actually returning a value


Q: How do you print an empty line?

A1: cout « endl;

A2: or cout « endl « endl;


Q: are these slides available on the website? i don’t see them there

A1: check out the schedule

A2: Note that Chris is working through the end of slides from lecture 2 (which is linked onto Wednesday in schedule)


Q: does the main function not have to be first?

A1: No


Q: is python pass-by-value for all function inputs?

A1: No, there is an option to pass by reference, we’ll see that soon

A2: Python is a weird hybrid called pass-by-reference-value – all things passed in to functions are references, but these referecnes are passed by value


Q: do we not need to declare the function doublevalue?

A1: The function definition can serve as its own declaration (as long as you define it in file ahead of where you use it)


Q: Will we be using function prototypes as a part of style?

A1: yes

A2: Yes, definitely! They help to better organize your code.


Q: do all reference functions return void?

A1: no


Q: If vectors are pass by value, does that mean the entire internal array is copied? For all classes in general, how would C++ determine which information would be copied and what would be kept as just a pointer?

A1: if something is passed by value then yes it is completely copied when passed over. The writer decides what is passes by val vs ref

A2: The class itself defines its behavior when passed by value (i.e. is it a deep or shallow copy). All Stanford collections do a deep copy. Since that is expensive, we generally pass collections by reference for efficiency


Q: what would happen if there was no &?

A1: It would be passed by value


Q: how does the x know to go to the my value

A1: live answered


Q: wouldn’t it have to be doubleValueWithRef when you call it?

A1: Yes, correct!


Q: could we get a quick explanation as to the difference between a pointer and a reference?

A1: We won’t talk about pointers until week 6, so hold on on that. For now, it is all references


Q: why are the function names different? main calls on doublevalue but the function is called doublevaluewithref

A1: live answered


Q: is int& x the same as int &x? If so which one is better to use?

A1: Yres, they’re the same. Neither is better, just be consistent with your usage


Q: Wouldn’t the code by doubleValueWithRef(myValue) or can you omit the WithRef

A1: live answered


Q: why don’t we pass the address of myValue?

A1: live answered


Q: can we return some values when using reference? instead of void

A1: yes, you can return values from a function that takes parameters by reference


Q: so can you omit the WithRef?

A1: live answered


Q: can &x be used later without having to write int again?

A1: Depending where you write this it can mean different things


Q: So the Ampersand is called a pointer?

A1: no, the ampersand in front of a var indicates the var’s address

A2: In the context of the way we’ll be using things for now, the ampersand in a parameter type declaration indicates that a function’s paramaeter should be passed by reference


Q: When it is beneficial to pass by value rather than reference?

A1: live answered

A2: We’ll see some of the benefits of passing by reference as we move to work with collections. Pass-by-reference is both more efficient (don’t have to copy) and lets you make direct changes to data structures


Q: Oh okay, then what exactly is a pointer?

A1: live answered

A2: Hold onto that question until week 6! That’s when pointers make their entrance in this course


Q: That’s cool! Is it sort of convert int into the “immutable” type that passes the URL refference and not the copy?

A1: live answered


Q: If you passed the WithRef function 7, would it work but just not change any variable?

A1: No, this would get a compile error (because 7 is a constant and not mutable)


Q: Why use header files as opposed to prototype functions?

A1: Sometimes you want to reuse code from a header instead of rewriting every single time

A2: Typically you use a header file when you are exporting those functions for use by other files. If you only use the functions within the same file, having the prototypes at the top is more convenient


Q: what does "#pragma once" do/mean?

A1: Prevents the file from being included more than once


Q: what is the "pragma once" that was on top of the .h file?

A1: live answered


Q: what is console.h

A1: live answered

A2: terminal output library from stanford c++ libraries


Q: Does including a header file basically load prototypes of the functions before main?

A1: live answered


Q: what’s the point of header files if we still need to implement them

A1: It separates the interface from the implementation, exports the information about the functions to other files


Q: so whats the diff between header files and interfaces?

A1: A header file IS an interface — you got this!


Q: How do we actually implement the functions that are in the header file? Is it the same syntax as normal?

A1: live answered


Q: so if we wanted to use a header file as a library in other c++ files that would be a reason not to include #pragma once, right?

A1: All header fields should have the #pragma once — it prevents issues where the compiler ends up with multiple copies of the same header wihtin one file, which is a no-no


Q: where are those functions defined?

A1: In the .cpp


Q: Does function order matter?

A1: Not if you have a prototype


Q: how does QT creator know where to find the Stanford header files? NameHash for example didn’t have a directory with header files but it still included console.h

A1: The .pro file defines where the library has been included on your computer (as defined by the CS106 project you built during Qt installation)


Q: is there downside to writing whole functions in header rather than just declaration?

A1: We generally do not put the implementation in the header, code goes in the .cpp files


Q: When would it be worth it to write inline functions as opposed to declaring them in header files?

A1: live answered


Q: is this analogous to an interface? it seems similar

A1: Yes, exactly right!


Q: Do you have to implement all functions included in the header file?

A1: You should


Q: What’s bool?

A1: A true or false value


Q: What’s the difference between pointer and reference
?

A1: live answered

A2: We’ll talk about that later in the quarter! For now, pretend pointers don’t exists. We’ll only be using references for now.


Q: Is the Assignment 1 due to next Friday?

A1: Yes, 11:59pm pacific

A2: yes


Q: How do we create a header file on Qt?

A1: Just create a file with the extension .h

A2: You can right-click on the project name and use the “Add new
” option to add a new file, with extension .h


Q: Is there a way to use header files, implement the file, and then use inheritance for a single point of control?

A1: I’m not sure what you mean by “use inheritance”, but putting commonly used functions into your own .cpp/.h and including that it multiple projects would allow you to reuse that cod.


Q: so is the benefit of using header files solely just to give us the ability to put the main function at the begining of the file and other component functions after it?

A1: live answered

A2: Yes, using header files and function prototypes allows us to easier organize the code without having to worry about the order in which things are defined.


Q: So to be clear, you declare a function in a header file, define the function below the the int main, and call the function in int main?

A1: live answered

A2: That is one way to do it. You also can put the funciton declaration before main, without explicitly using a header file.


Q: so a function doesnt have to be defined before you use it? only declared?

A1: Correct


Q: With a .h file, we always have to implement it in each file. How do we reuse the square function, which will most likely be implemented the same way wherever we use it?

A1: You would bring both files into a new project, the header file that declares it, and the .cpp that implements it


Q: what's the difference between putting func protos in the header vs just at the top of the cpp file?

A1: Both will usually work


Q: If you don’t define it how does the program know what to do

A1: It doesn’t, which causes an error


Q: In the .cpp file, we can declare the functions in any order as long as we have main first right?

A1: The general rule is that any function must be declared before it is used. So if a function is used in main, its declaration must come before main in the file.


Q: In a lot of the Stanford header files i looked at they have the function code in them? Is that also pretty common for headers?

A1: Only for templates (which have to be visible in headers due to C++ compilation semantics)


Q: Is the name “header file” reserved for files holding prototypes?

A1: Yeah, although it generally refers to any file ending in .h extension (these files mostly only have function prototypes/declarations in them)


Q: are c++ strings basically character arrays?

A1: Yes, we’ll learn about the different types of strings later


Q: Last question on declaration, what’s the general rule of thumb when to use the Header file aka more than 10+ function declarations?

A1: A better rule of thumb would be that you list declaration at the top of a file when you use those functions only in this file, but if you need to export the functionality to another file, then bust them out into a separate header file


Q: Do we need to use “#pragma once” everytime? how does it work?

A1: Yes, a header file should always have #pragma once. That is a special directive understood by the compiler


Q: So for if we pair a .h file, and a .cpp file that only implements the methods declared in the .h file, we can then use that .h file in any other .cpp file without having to define it again?

A1: Yes, that is correct.


Q: is there an advantage to using one over the other

A1: What is this referring to?

A2: they’re equivalent (assuming this is referring to .at() vs. []


Q: Why ever use the .at() function instead of grabbing the character by it’s index? Is one more efficient?

A1: style


Q: Is that why in C strings are just arrays of characters?

A1: Yes!


Q: Can you declare characters without a string? Like char c = ‘a’;

A1: yes


Q: stanfordTree[3] vs stanfordTree.at(3)

A1: basically the same


Q: Is it possible to have a single character string or is it automatically considered a char?

A1: you can have a single char string

A2: There’s a difference between a single character string and a character variable.

string s = “a”; char c = ‘a’;

^ those are different things


Q: Can a string be just one char? for ex: string example = "b"

A1: yes


Q: is the .length() function specific to the Stanford library or does it exist in the general C++ library as well?

A1: standard library


Q: Is .at a built in function?

A1: yes standard string


Q: is there a benefit to putting prototypes in the .h file vs in the cpp file? Is it just the difference between a private and public function?

A1: Yes, the header allows you to export that funtionality to other files. If you only use the functions within this file, that having prototypes in .cpp is easier to maintain.


Q: there’s no need to include an escape character for single quote and double quote?

A1: live answered

A2: If you want to include a single quote ina string you don’t need an escape. If you want to include a double quote in a string, yes you need an escape character.


Q: wouldn’t you have double single quotes because it is in both four loops?

A1: live answered


Q: is it possible to do something like enumerate() is c++?</i>

A1: No, usually we use a traditional for loop or for each loop in C++


Q: Can you go over the single quotes vs double quotes quickly again?

A1: live answered

A2: Single quote defines a character, double quote defines a string.


Q: If char ch = 'a' and you do ch++, will you get 'b'

A1: yes


Q: do we need to add “#include” in every program withstrings?</i>

A1: yes

A2: Generally, yes (sometimes you will get string on behalf of another include that also included string, but it is not a problem to include twice)


Q: Why did you do i<(int).plainText.length() instead of i<plainText.length()?

A1: live answered


Q: will we see an example of using a .h file that is fully implemented? How is the stanford .h file implemented

A1: We’ll write our own .h files when we get to implementing classes later in the quarter

A2: End of Chapter 2 in text gives a good example from library random.h and random.cpp — check it out!


Q: Could you make in size_t instead?

A1: live answered


Q: why does this only work for uppercase? how about lowercase?

A1: live answered


Q: The else statement is just to make sure that non capital letters aren

A1: live answered

A2: live answered


Q: Can you cast an int so that it becomes a char?

A1: yes

A2: yes


Q: The else statement is just to make sure that non capital letters aren't altered, right?

A1: live answered


Q: Would decrypting this be a problem because we don’t know which character was wrapped (i.e. -26)?

A1: You would do the same modular artihmetic on decrypt (wrapover)


Q: What is a control character? And what is a printing character?

A1: live answered


Q: Just a style question, but why aren’t the cctype functions in camelCase?

A1: standard C++ conventions are snake case, terse abbreviations, our stanford style is camelCase and more verbose — we think better for learners


Q: would s[i] = toupper(s[i]); do the same thing as toupper(s[i]) strings are mutable?

A1: these functions operate on a single char and it is passed by value. toupper(s[i]) would not change s[i]


Q: why does the length function go after the parameter instead of putting the parameter in the parentheses?

A1: That is the c/c++ style of getting the attribute. Python is different


Q: You said cctype is a standard library. Which standard libraries are we allowed to use?

A1: We’ll be specific about the standard libraries you need to use.


Q: Will the assignment 1 be based on lectures this week.

A1: Yes, Assignment 1 will cover content from this week’s lecture + Monday’s lecture


Q: Not sure if you guys are aware of this, but can you audit CS106M?

A1: Sorry, no


Q: Do strings and characters need to be assigned to variables before using built-in functions? Can I do reverse("string") or toupper("a") ?

A1: Yes, you can call those functions on string literals


Q: Am I allowed to ask CompSci questions unrelated to today’s lecture?

A1: We’d prefer to keep lecture questions on topic to that days lecture/ If you have questions beyond the scope of the class, I would recommend dropping by instructor OHs


Q: Why are the methods not in the form of the methods from ? Like uses form of str.at() whereas is like isalnum(x) (aka no dot).</i>

A1: The string methods are defined on the string object, which is why they’re called in the s.function() format. The functions defined in the cctype library do not belong to a certain object – they are standalone functions that take in a paramater as input and produce output


Q: I might've missed this but why did we cast plainText.length() into an int in slide 6 but not anywhere else?

A1: because it was a non-int type being compared to an int


Q: could you briefly go over what a size_t is

A1: A number that cannot hold negative values


Q: right but why didn't we cast it anywhere else?

A1: that’s the only place that we get the length I believe


Q: are there any other things besides length that require typecasting?

A1: live answered


Q: whats the difference between unsigned and size_t

A1: live answered


Q: is all (or most) c code runnable as c++. E.g can I make strings as char* etc

A1: Yes, all C code can be compiled by a C++ compiler (I believe). However, you should not use char* for strings in this class

A2: Yes, an intentional design goal of C++ was to be a superset of C (with very very few exceptions)


Q: is plaintext.length() called every single time we go through the loop? or is it just called once at the beginning and then saved? because if the length of plaintext changes in the loop then this could affect the results

A1: It’s called every time the condition is evaluated


Q: Is size_t defined as the biggest possible size?

A1: live answered


Q: The way (int) works is to make str as number, when should we do that? Still confused.

A1: live answered

A2: live answered


Q: when's the next assignment coming out?

A1: tomorrow early afternoon


Q: When we use += for strings, will a new string be created or will it modify the original string (mutated)?

A1: live answered


Q: Doesnt't adding the (int) to correct the integer type change the actual value of the integer we're dealing with? instead of actually getting the length of the string we will get its ASCII code no?

A1: live answered


Q: Does the length of a string include the null character?

A1: live answered


Q: is there auto casting?

A1: live answered


Q: So could we use cctype toupper function to make Ceaser Cipher work on any string regardless of case?

A1: yes, that function would be usefukl if you wanted to make this program case-insensitive


Q: Can functions in the "strlib.h" Stanford library be used in homework assignments without explaining what they're doing?

A1: yes


Q: Can we use unicode too, in place of ASCII?

A1: live answered


Q: just to make sure, plainText[i] is the numerical value of the character right?

A1: no it’s a char


Q: is there a page in the text about size_t I could look at?

A1: I don’t know off the top of my head, but the textbook has an index that you can check out