Lecture 13 Zoom Q&A


Q: so A5 won't be next week?

A1: No, there is a little gap between assignments 4 and 5 for the mid-quarter diagnostic/assessment


Q: will the assignment still be released on Saturday?

A1: No, there is a little gap between assignments 4 and 5 for the mid-quarter diagnostic/assessment

A2: No, Assignment 5 won’t be released until the middle of next week.


Q: so there should always be a semicolon after the curly bracket for structs?

A1: Yes!


Q: If Vectors were created using structs, why didn’t they make it so it could accept multiple types of data so we wouldn’t have to indicate the type we’re using first?

A1: Internally a vector is stored as an array which is a homogenous sequences of elements all of same type


Q: what is diffrence betwwen class and struct

A1: live answered


Q: why is it that if I declare a variable like int i;, but don't set it to a value, then I go into the debugger, it says that i is equal to some really big number?

A1: This is a garbage value, from whatever occupied the location that i is stored in on your computer before

A2: There is no “default constructor” for primitive types, so their contents are uninitilaized. Memory location contains some leftover/arbitrary value


Q: What are some cases when we would NOT use # pragma once?

A1: A header file should alwasy have protection against multiple include. There is an older way to do it using a #ifndef style guard, this was used before compilers had the #pragma once option


Q: Why isn't the inside of the declaration indented?

A1: It doesn’t make a difference, functionality wise. If you’re more comfortable you can index the public and private lines

A2: The indentation is not significant to C++ compiler (although we do use conventions for readability)


Q: Would you ever want to have any public variables?

A1: live answered


Q: so are private variables kind of like universal variables?

A1: Private variables have scope that can be accessed from any function within the class definition, so yes they have wider scope than local variables we’ve seen before.


Q: Will we ever use a Protected Class?

A1: you won’t need them for this class

A2: No. You only need protected when using inheritance, which is not a topic in this course


Q: For public functions, can you redefine the public function (a.func4() = function5{}

A1: No


Q: what's the difference between public variables and global variables?

A1: live answered


Q: Does ‘private’ mean users can’t use the data or that they can’t manipulate it?

A1: It means that users can’t even see the data, nor manipulate it

A2: Both. Cannot read or write


Q: 106A we were told that global variables were great structure/style. why is it the opposite here?

A1: live answered


Q: how could a user change a public variable unless they peeked under the hood of the interface theyre interacting with? they'd probably need to be programmers themselves rght?

A1: The public features of a class are listed in the .h file, thus that interface is known to the client.


Q: For the constructor with parameters, do we not have to initialize the instance variables?

A1: If we do not initialize them, they will use the default initialization (for priimitive types, this means no initialization, for classes like string, the default constructor will initialize to empty string)


Q: in python, is the implict param "self"

A1: live answered


Q: wouldn’t it be better practice to pass parameters with different names than the names of the actual instance variables?

A1: live answered


Q: Are the parameters passed into the function the explicit parameters then?

A1: live answered


Q: Confused on why we’re renaming parameters? Aren’t they already named name and balance?

A1: live answered


Q: Can you explain the. ::

A1: Yes we’ll touch on them a little later in the lecture.


Q: Are parameters in the constructor a way for users to define private variables for the class?

A1: They are used to allow the client to specific the initial state of the object being constructed


Q: Why couldn’t we just use parameters name and balance right away like we do in functions? Why do we need to declare and assign them to new variables?

A1: The parameters name and balance are being passed by the client and we are saving the name and balance as part of the object state becase we will need it later as part of managing this account object

A2: We use the parameter values to act as initial values for the member variables of our class. We need to store this information in member variables so that is is persistently available in other functions that we implement on the class.


Q: Just to be clear, do things like strings, stacks, and vectors already have built in classes where things like.add() or .erase() are public?

A1: Correct!

A2: correct


Q: In what cases do we need the friend at the beginning of the line?

A1: live answered


Q: For good style should we indent private/public?

A1: live answered


Q: Why aren’t we passing in fraction in decimal?

A1: live answered

A2: Because this is a class method, the Fraction itself is an implicit parameter (“this”)


Q: So are there private parts of the class that help define what add and erase would do behind the scenes? Or is it fully defined in the public part?

A1: It could be either, depends on how the class is implemented

A2: Yes, its definitely possible to have private functions that act as helpers to help implement public behavior. I think that we’ll see an example of this a little later on today.


Q: can you put member variables in public?

A1: live answered


Q: does default consturctor always have to have no parameters?

A1: Yes


Q: why aren't we using "this->num;" in the private section?

A1: live answered


Q: Thanks!


Q: Why do we want to keep num and denom private if the user is inputing it anyways?

A1: live answered


Q: Do we need a function that allows us to change the numerator or denominator?

A1: It is possible to add such a function ,but not clear that that this is a necessary feature to provide

A2: You could have this or just create a new fraction


Q: Why do we use the friend ostream& part?

A1: live answered

A2: We’ll talk about that a little later I believe.


Q: How does the class know what the default value is?

A1: live answered


Q: The fraction class that we just created, doesn’t know about its functionality (i.e. i’s dividing two numbers)? So, we basically create the tools in the class and then give functionality to the class in the main code?

A1: live answered


Q: is the ostream redefinition analogous to how we reconfigure object.toString() in java classes

A1: Yes


Q: Why do you need a constructor with and without parameters?

A1: live answered


Q: For overloaded functions, does the last function always dominate?

A1: No. The number/type of parameters in the call is matched to the appropriate version.


Q: why u pass in as referecnce

A1: live answered


Q: Is Chris organizing by type within public? I see he is grouping int and separting everything else.

A1: I think it’s just a coincidence because getnum and getdenom are similar functions

A2: live answered


Q: Is there any reason to set the Fraction by reference in mult() aside from speed?

A1: live answered


Q: Whats the namspace std?

A1: It is the namespace that contains all of the C++ standard library features (string, iostream, etc.)

A2: If you don’t include it you will have to use std:: in front of standard c++ types like strings


Q: Was that a vim trick :0

A1: Sure was


Q: so do we only do the colon stuff for private functions?

A1: No, it applies to both private and public functions


Q: So is ostream part of the std class?

A1: Correct

A2: yes


Q: when woud we have a different class in front of the colons

A1: When we were defining member functions that belong to that class

A2: i.e. Fraction::add is the fraction’s add function, Vector::add is the vector’s add function


Q: do you need the std:: before ostream?

A1: only if you dont use namespace std


Q: Is there an automatic way to apply the Fraction:: to everything in the class?

A1: using namespace Fraction


Q: why do we not need to specify the Friend namespace for &operator«?

A1: in C++ the friend only goes on the function declaration, not the function definiition

A2: The operator is not actually a part of the class, which is why its not preceded by the Fraction::keyword. We use the friend keyword instead ot indicate that the method should have access to variables/fnuctions from Fraction class without actually being a part of it


Q: so we use the colons to show member functions regardless of whether they are public or private, just to show that these functions are associated with this class

A1: Correct

A2: Yes


Q: * Fraction namespace


Q: If we’re using functions outside of the std class and have included it’s header file, would we need to call it as Class::function()?

A1: yes


Q: can you call the other constructor with parameters and use 1, 1 as the var as well? or is that bad style

A1: Yes, it is possible to have implemented a constructor “chain” to another constructor. That can be a good thing rather than repeat code


Q: why are we implementing the class functions in a .cpp file instead of in the header file?

A1: This is standard covention in c++ – you define the interface in the .h file and you implement the functions in the .cpp file.


Q: So if you don't have the same variable names, is "this->VARIABLE" an implicit thing?

A1: yes, you can omit this-> if there is no variable naming conflict

A2: Yes, you can refer to the member variable without the explicit this-> if the name is not already in scope


Q: but they are private variable, how can u change it

A1: live answered

A2: Private variables are accessible within functions that are defined as part of the class.


Q: Is it bad style to not put ‘this’ if the parameters do not have the same name or there are no parameters?

A1: nope, that’s totally fine.


Q: so for the default constructor with no parameters can you also use this->num?

A1: Correct. You can, but could also use the shorthand num = as well (since no naming conflict)


Q: if the parameter was named something different, would the class variable still need the this-> pointer?

A1: no, you can omit this-> if there are no variable naming conflicts, but sometimes we still include it just for clarity that we’re operating on a member variable


Q: Can the constructor take arrays? Or is this the reason why we are using this->?

A1: I don’t think the constructor can take arrays

A2: You can pass a variable that is an array as a parameter to any function (including a construtor), we will discuss arrays next week and how they are passed as paramete, etc. will come up then


Q: How do we know that we’re in the same class as the header file containing the private variables? The #include?

A1: It is because of the Fraction:: on the function definition


Q: Why no parenthese after f.num?

A1: live answered


Q: can public functions access private variables of another object that’s in the same class?

A1: Yes


Q: Why don’t we have to use this->num to refer to the num in this instance?

A1: Because there is no naming conflict in scope


Q: Why do we directly access the private field f.num instead of calling f.getNum()?

A1: live answered


Q: if we return the class var num, in the getnum function, won't it reutrn the wrong num if the fraction happens to be reducable?

A1: Assume that we always store the num/denom in reduced form, then getNum will return the properly reduced value


Q: why are we allowed to refer to the private variables for f.num

A1: Because we are still in the same class that num is defined in


Q: Are the class varibles “num” and “denom” the same all the time (num=1=donom)? as you defined in the class

A1: Each Fraction object has its own copy of num and denom


Q: so we do not need a semicolon after the curly bracket for functions defined in the class? only when we define the class itself?

A1: Correct. The semi-colon is needed on the declaration of a type, function, etc. but there is no semi-colon on the definition.


Q: so just calling reduce() is the same as saying this->reduce() ?

A1: Yup.


Q: Stylistically, which is better? Calling the function to access num or directly using the private field?

A1: calling the function to access num

A2: From the implementers perspective either is fine. From the users perspective they are not allowed to access the private field, so the only way for them to get the info is to use the function.


Q: Private variables can be accessed since we are in the same class but can they be modified as well?

A1: Within the class that they are defined


Q: for the multi function, why we pass in reference? can we not pass in as refereence?

A1: Our convention is to pass objects by reference to avoid making unnecessary copies of them

A2: Pass by reference will avoid making a copy of the data type


Q: What does reduce do?

A1: Reduces fraction to its simplest form, i.e. 128/256 reduces to 1/2


Q: so if you were in main, you can’t do f.num; ? you would have to do f.getNum(); ?

A1: exactly


Q: why isn’t the implementation and the method headers in one file?

A1: Standard c++ convention is to include the interface in the .h file and the implementation int he .cpp. That way, another programmer can read and understand the interface without having to dig through the entire implementation.


Q: is it possible to just implement a toString() function instead of doing the ostream business?

A1: Yes, but it is convenient to allow output into a stream using « directly


Q: If it's not part of the class why can you access the private variable?

A1: Because it was declared as “friend”


Q: Why can we use frac.num and frac.denom if we are not part of the Fraction class? Aren’t those variable private?

A1: live answered

A2: By labeling the function as “friend” in the method declaration, we have permission to access those variables


Q: Why do we define all of the functions here and not the header file?

A1: Standard c++ convention is to include the interface in the .h file and the implementation int he .cpp. That way, another programmer can read and understand the interface without having to dig through the entire implementation.


Q: If operator is not part of the class, how can we use frac.num instead of frac.getNum()?

A1: live answered


Q: why is it frac_gcd?

A1: It is the greatest common divisor of the numerator and denominator


Q: Is there a difference between ‘std::ostream &operator
’ and ‘std:ostream& operator
’ (location of ‘&’)?

A1: Nope, no difference

A2: No, same exact thing.


Q: What is the point of keeping a variable private if you would use friend to make it accessible? What determining factor should we use to determine whether to keep a variable private or public?

A1: friend gives limited acces to exactly what you name as friend, it does not broadly give access to anyone (as public would)


Q: Does it differ whether we call it frac_gcd or fracGcd?

A1: No. The camel case would match our convention, though :-)


Q: can I do something when constructing a fraction if the denom is zero?

A1: Yes, you can do error checking

A2: Yes that might be a good time to throw an error since that would be an invalid function


Q: unrelated, but could you use backtracking to turn the gcd() into the extended euclidean algorithm?

A1: live answered


Q: If someone created a fraction 1/0 and used the decimal() function what would happen?

A1: Try it and see ! :-)

A2: Program would crash! We should probably disallow 0 as a denom in the constructor by throwing an error


Q: How would you add to the class to prevent something like that^

A1: In the constructor you could throw an error if the denominator was zero

A2: You can add error checking in the constructor to throw an error when denom is passed in as 0


Q: for the multi function, are we acutually changing the first object?

A1: Yes, we are


Q: so can private functions only be accessed by functions within the class? and the user cannot call the function directly?

A1: exactly

A2: Exactly


Q: can you show the overload of the greater than algorithm

A1: live answered


Q: why does frac need to use _gcd why is it not frac.gcd?

A1: That was just a variable name Chris chose


Q: how does gcd work? (to me, it seems like gcd(2, 2) would return 2, right?)

A1: Lots more information on the algorithm here! https://en.wikipedia.org/wiki/Euclidean_algorithm


Q: so when you declare a function as a “friend” it gives it permission to do stuff as if it were part of the class such as access private variables and functions?

A1: yes, exactly

A2: Correct


Q: can you go over the ostream implementation? Is this overloading or overriding?

A1: This is overloading the operator « (stream insertion operator) to allow you to directly output a Fraction onto an output stream


Q: If private functions can be accessed by functions with the class, why did we use friend? Was that because it was a variable rather than a function?

A1: We use friend because the restrictions are a little different than actually making it a part of the class


Q: what's the difference between defining class-specific functions as methods and defining functions that only take the class as an input? (like the string class functions stringisReal(string) for example). Are these latter functions declared under the class or are they defined separately?

A1: live answered


Q: When does the operator need an & before it? &operator< vs operator«/i>

A1: the & applies to the return type of the function


Q: why is operator< not passed by reference but operator« is?

A1: The < operator returns a bool by value. The « operator returns a std::ostream by reference


Q: does this relate to interfaces?

A1: live answered

A2: Little different, but we do call the thing defined in the .h file an interface for the class


Q: So does a function not being a part of the class just means you don’t need to use a specific object to call it?

A1: Yes, pretty much. You instead pass in information as parameters rather than relyiing on internal information that the object stores


Q: instead of a void mult function could you overload * and then be able to call *=

A1: live answered


Q: Could you scroll to the top of the page?

A1: The code is available on the website if you want to check it out for yourself.


Q: so does the operator overloading print the fraction to the outstream, and then return the outstream?

A1: yeah, pretty much


Q: What exactly does overload do?

A1: live answered


Q: So is operator implementation style just the “C++” way?

A1: This way of implementing operators is the only way to be able to use these operators on custom-defined types


Q: Is there a operator « implementation in std?

A1: live answered

A2: For certain classes yes


Q: do methods used from friend classes have to be operator overloading methods? or was this just an example

A1: no any methods can be marked friend


Q: why is operator« passed by reference while operator< is not?

A1: live answered

A2: < returns a bool while « returns an ostream that we want to modify