Lecture 4/10: Lecture 3 Q&A
Lecture 3 Questions and Answers (Exported from Zoom Q&A log)
Q: Nice mandelbrot set!
A1: thank you! Youâll likely make one of your own later this quarter!
Q: is this valid in C++?:
a = â9â int_a = int(a)</i>
A1: Not sure what you have on second line, the underscore is a space?
A2: It is allowable to convert from integer to character and back, they are in the same type âfamilyâ
Q: trying to define a varible so something like
num = int(a)
A1: The type always goes on the far left and then the name of the variable, e.g.
int a = 45;
Q: do we actually type âis trueâ and âis falseâ or is this implied by using âelseâ?
A1: adding an explicit comparison to true/false is consider redundant, you can just say if (cond) not something like if (cond == true)
Q: In Python, we could often leave out an else statement by having a return both inside the if statement and outside, for instance, since a result would return either way. Can this work in C++, too?
A1: Yes. The else is optional
Q: is the else {} outside the if's curly braces?
A1: Yes, the âelseâ has a set of curly braces of its own. These are the sibling to the braces around the âthenâ part
Q: is there a syntactial reason why variables have the first word lowercase and the second uppercase for example firstNum
A1: live answered
Q: For this case, would it be possible for both x and y to be equal to 5?
A1: Yes, that is possible i(although in this code fragement y is 7 not 5)
Q: for boolean operators, what is the default order of operations? like does not go before and/or?
A1: order of precedence in not, then and, then or. You can alwyays use explicit parenthesis to be sure that you are getting the order you intend
Q: What about constants in C++?
A1: still all caps!
Q: (In terms of the naming convention)
A1:
Q: do we always have to use using namespace std;?
A1: By convention, we will do this, it allows us to use the features defined in the standard namespace by referring to them by their short name, withotut the full namesapce qualifier, e.g. string instead of std::string
Q: also is int main() the same as public static void in java
A1: yep, good eye!
Q: Where does the 0 in main get returned to? And what happnes to it>
A1: live answered
Q: What is a double?
A1: A type that can hold decimal value (with fractional component)
Q: Why do you have to return an int in main()? Why not void main()?
A1: live answered
Q: why would you return 0?
A1: live answered
Q: what should the returnType be if the function doesnât return anything?
A1: void
Q: must you always return 0 in main
A1: live answered
Q: Do you still have to return 0 in main in updated versions of C++?
A1: live answered
Q: The outcome of main() is not int. Why we declare mainType as int?
A1: live answered
Q: what is the meaning of namespace std;
A1: By convention, all our programs start with the statement using namespace::std; This allows the program to use the features defined in the standard namespace by referring to them by their short name, withotut the full namesapce qualifier, e.g. string instead of std::string
Q: Is there a reason why you chose to return 0 in the above function and return 1 in this function?
A1: returning 0 from main means âall goodâ, whereas retunring 1 means âsomething went wrongâ
Q: when testing code, is there a way to simply run a new function outside of main. eg could we run factorial(5) to debug?
A1: As a general case, no, a C program always starts from main.
Q: whats the difference between using return and cout «»
A1: cout is printing the result, i.e. equivalent to calling python print() function
Q: so are all operator= statements valid? e.g %=, *= along with += and -=?
A1: Yes
Q: and do we always need return?
A1: chris is answering this now!
Q: Is main always an int?
A1: live answered
Q: Do you always have to define the output of a fucntion before the name of the function as is in âint factorialâ
Or can we just write the funtion name and then have it return a string or an int or another type all togther?
A1: Always need to declare first (Chris is talking about this right now!)
Q: couldnt you set the main variable type to be a string and have the return be something like 'good'?
A1: The main() function is special and has to return integer type always. Other functions donât have that restriction
Q: What if our purpose of the main function was to return a sentence (string), how do we handle that?
A1: You wonât be able to do this with main() because it is special and always has to return an integer type rseult. Other functions can return strings or other type
Q: What's the difference between:
[returnType functionNameâŠ] and [int functionNameâŠ]?
A1: int is just a specific value of returnType that we can use.
Q: for the for loop inside factorial, does the first iteration not include nâ?
A1: The advance step (third piece of for loop) executes at the bottom of each loop iteration,
Q: do you have to declare the function before actually writing the code in it?
A1: Yes
Q: are there âpublicâ v âprivateâ specifications for functions in C++ like there are in Java?
A1: Yes, but visibility is only a feature of certain kinds of functions (member functions they are called)
Q: what is the benefit of using void functions?
A1: live answered
Q: I opened up Qt Creator to see whether I could try typing along with Chris. Will there be an introduction to what the various project types are, or how to set up and execute projects from scratch?
A1: There is a blank project linked to course home page â grab it!
Q: basically when do you want to return a type and when do you want to return a void
A1: live answered
Q: wait so just to be clear the void function will not output c?
A1: Agreed. To print something, there needs to be an explicit use of cout « value
Q: What is the double data type - how is it different from and int/float
A1: It is a higher-precision variant of float.
Q: for the function printTenTimes
A1: live answered
Q: Whatâs the point of calling a function if you donât want to return a value?
A1: live answered
Q: for the printTenTimes function what does the cout outside the for loop do?
A1: It prints the final newline after the all iterations of the loop have executed.
Q: but couldnt you set the value produced by a void function anyway
A1: A void function does not produce a value, there is nothing you receive when calling it
Q: why would you ever declare the function name without writing the code within?
A1: It serves as description of the function that introduces it to other clients (so they know how many, what type of parameters, etc)
Q: when you say you can define functions earlier than when you write the code for them what does that mean?
A1: When you write the declaration you are indicating the âintentionâ for the function â how many arguments and what type and what it returns. This serves to introduce the function to other clients. Later you follow through to implement the function to make good on your intention
Q: got it thanks
A1: live answered
Q: so you could declare a function using double functionName, char functionName, etc?
A1: A declaration includes the function return type, the function name, and then the type and order of arguments in parentheses
Q: Is there a way for us to have access to the Q&A after lecture?
A1: yes, weâre workin on exporting it somewhere on the course website
Q: is this like a lambda function?
A1: Not exactly⊠That maybe is a question to bring to helper hours, cause itâs kind of subtle to explain
Q: When you say cout << myValue why/how does the computer know to print 5 rather than literally âmyValueâ
A1: If we put the character myValue within double-quotes, it would understand that we want it to literally print those exact letters. Without double quotes, the name of a variable evaluates to the value of that variable
Q: Why do the main functions in these two examples have no return?
A1: the function named main() has int return type in all of these examples
Q: Why would you pass by value rather than just use a global variable?
A1: Global variables are frowned upon as poor style. It is considered better to be explicit about what information is being transferred into/out of a function call, for which parameters are the right mechanism
Q: Whatâs the reason for doing this? Is it just better style?
A1: I lost track of what this question is in reference to, sorry. Can you provide more context?
Q: by doubleValue do we mean doubleValueWithRef?
A1: No, there are two different functions and they behave differently. Review the code in slides to see what is different, one is pass by value the other by reference
Q: stylistically, any preference for passing by reference placement of &? For example âvoid funct(int& a);â or âvoid funct(int &a);â
A1: Be consistent, but either is fine. I tend to attach to the type myself
Q: why didn't the main function return 0 in that last example?
A1: oops! oversight :-) Sharp eyes, thank you!
Q: In the doublevalue example why does the main() have no âreturn 0â?
A1: Oversight. oops! Sharp eyes, thank you
Q: Shouldnât the function âdoubleValue(myValue)â in main be âdoubleValueWithRef(myValue)â?
A1: fixed, thank you!
Q: but so if i defined int example = 15, then called doubleValueRef(example), that would work?
A1: Yes, this works, but not the constant number (as you noted)
Q: i just cannot put 15 directly
A1: Right
Q: so if you did "x = 5; doubleValueWithRef(x);" then x will be 10?
A1: Yes!
Q: great thanks
A1:
Q: is main function the same as if name == âmain in python?
A1: Yes, effectively
Q: wait but if i can define functions in the main why would i use header files in the first place mate?
A1: You can also write functions in a separate file than the one where main is
Q: so header files are mainly to declare functions we have not written yet?
A1: yes, as well as to âadvertiseâ the functions to other clients
Q: how different is #pragma once is different from
#ifndef HEADER #def HEADER .. .. .. #endif</i>
A1: #pragma once is the modern way to do double include guard
Q: when does the first pset go out?
A1: Tomorrow (Saturday)
Q: Sorry, I may have missed if you said it, but does a string have an escape character at the end automatically?
A1: Escape? Do you mean newline or something else? C (not C++) has a convention of ending a string with a null (0) character, but C++ strings internally handle that
A2: sorry, i meant null character like C. you answered it. thanks!
Q: Are there empty strings? But not empty characters, right?
A1: Effectively yes (I am being a little cagey cause there is a zero character to be pedantic about it)
Q: is the function âatâ in the string library?
A1: yes
Q: What was the .at for?
A1: s.at(3) is mostly same thing as s[3]. but whereas at does bounds-checking for safety the square bracktes does not do that error checking (for efficiency reasons)
Q: sorry, i meant null character like C. you answered it. thanks!
A1:
Q: Does .length have to be defined or does the library take care of that?
A1: .length is defined as part of the standard string type
Q: is .length interchangeable with.size in c++?
A1: In some situations, but not all
Q: is count « endl just separating the outputs therre
A1: Yes
Q: is stanfordTree.length() equal to 13 or 12?
A1: live answered
Q: to get âfor(char c: stanford_tree)â to work, is there an iterator defined in strings header file??
A1: Yes
Q: Are we going to pick up on this lecture on Monday or should we watch the string video before Mondayâs class?
A1: live answered
Q: Do you call all functions on strings with that syntax? Why wouldnât it be length(stanford tree)
A1: live answered
Q: Does the cout«endl; at the end of each for loop just create that extra vertical space when printing the function?
A1: live answered
Q: is there a way to find the index of a character in a string?
A1: Look at find function for string type
Q: does slicing in strings work the same as in python?
A1: live answered
Q: how the null character is handled in c++ strings as we always null terminate in c
A1: live answered
Q: my kind of happy hour!
Q: Will you guys be switching off lecturing each week ?
Q: thank you guys so much!
Q: Can you please go over references and the use of references again?
Q: Do you need to define variable type when calling index numbers?
A1: live answered
Q: could you explain where the 0 in main get returned to again? and why you have to do this? do you have tor return 0 in void functions?
Q: :( too bad
Q: May you please explain (what using namespace std;) does?
Q: going back to the void function, so void functions can print but canât return?
Q: does the cout« endl; after each for loop create that extra vertical space when printing on the screen?
Q: Will we go over strings again next week?
Q: Can you post examples files on the website so can play with them.
Q: is there a difference between âint *aâ vs âint &aâ while pass by reference ??
Q: So does the int functionName(int& x) basically pre create a variable that gets passed into the code that follows? So you don't have to write it again insided the function?
Q: Is it considered bad style to use std::string?
Q: what language is used as the industry standard?
Q: Thank you
Q: Thank you!