Lecture 5/4: Lecture 13 Q&A
Lecture 13 Questions and Answers (Exported from Zoom Q&A log)
Q: mine is running extrmely slow. Same animation but at 1/15th of the speed. Is the code provided the same as Chris’ code?
A1: Did you take the pause out?
Q: No, i havent touched anything
A1: He just said that the pause is needed for 1 ball, but once you have 10 it is too slow
Q: ahh, thank you
A1:
Q: what would struct be equivalent to in Java?
A1: The class is the closest analog. (A C++ struct is almost same thing as a class)
Q: What exactly is “object oriented” programming
A1: It means that you organize your code around data +behavior (what he is talking about right now!)
Q: Java virtual machine is written in C++…so it is all C++ under the hood anyways :)
A1: And underneath that it’s all assembly… Turtles all the way down!
Q: If structs and classes are so similar, what are the advantages of each, if any?
A1: The distinctions are very slight (struct fields default to pubiic, class to private, classes allow inheritance, structs, do not)
Q: i wonder how hard it is to write assembly…I am guessing super hard :)
A1: Take CS107 and all will be revealed!
Q: Are these by any chance the same examples that were given for the 106A Java class lecture? I feel like I am having deja vu
A1: I think it is rule that every introductory lecture on classes uses BankAccount as the example :-)
Q: What exactly is the difference between private and public?
A1: A field that is private can only be accessed internally to the class implementation (i.e. in functions defined within classname.cpp) a public one is accessible to clients
Q: is the “user” someone coding or someone using the code
A1: live answered
Q: what does he mean by client vs user?
A1: live answered
A2: live answered
Q: What’s the difference between an object and a class?
A1: A class is a type, an object is a variable of that type. So int is a type, and int num creates a variable called num that is an instance of that type. Same for classes, you define Ball class, then create Ball objects when you want an instance of that type
Q: So in your example Julie, num would be the object and int would be the class?
A1: yes!
Q: So is a struct defined in the main program and the class defined separately in a diff file?
A1: well, you could define the struct in a separate file also. The distinction bertween struct and class is very slight — mostly just concerns what is default public vs private
Q: What is the different between a class and main?
A1: main is the function where the program execution starts. A class is defining a package of data + operations
Q: so the client can't even see the value of a private variable?
A1: Correct. Client cannot read nor write a private variable
Q: Since they are so similar how do you know whether to use a struct or class? Can you usually use them interchangeably?
A1: Largely interchangable, but by convention more full-featured things are written as classes and the basic record types are simple structs
Q: How is it possible for classes to have types when they house more than one function and variables
A1: The class type is a heterogenous aggregate of all its features, i.e. contains all the fields and functions as aprt of the class
Q: what is the :: syntax?
A1: It is the “scope” operator. It indicates that you are writing the getBalance function that is defined within the scope of the BankAccount class (to differentiate from other similarly named functions)
Q: So instead of using the :: syntax, you could define the method inside the brackets when you define the class?
A1: in the header file, inside class declaration Fraction, everything you declare inside those curly braces is within the Fraction class scope
Q: When and where can you use what is defined in private?
A1: only within the class implementation, i.e. within funtions that are defined as part of the class
Q: can you use const in normal function calls, or just when creating classes?
A1: const is a type modifier, can be applied to any type (parameter, return type, local variable, etc.) in any context.
Q: the clock on CHris’ computer and mine change at the same time…looks like Apple does a good jb syncing time…just a side note :)
A1: Now you are going to make me watch that too to see if I am synced also
Q: LOL
A1:
Q: Is it advised to use double over float?
A1: yes. Anything float can do, double can do (better)
Q: are we gnna get through this whole example today?
A1: Chris is a man on a mission! buckle up!
Q: is the two lines constructor equal to init in python?
A1: Effectively, yes!
Q: what about the destructor?
A1: You’ll have to wait til Wednesday for destructors…
Q: Is there a reason for the }; at the end?
A1: copy paste from header, but he is going to need to remove it eventually :-)
Q: why is he defining the functions outside the class?
A1: The scope resolution operator Fraction:: indicates these functions are inside Fraction class
Q: Do you have to make the default constructor? Can you just define the constructor that has parameters?
A1: If you did that, you would not be able to declare a variable of Fraction type without calling the constructor that takes parameters (if no default zero-arg constructor)
Q: But I thought we couldn’t acces num and denom because they’re private?
A1: This function is part of the class implementation, so it has those privileges
Q: Oh… I guess my question is why he isn’t defining these inside of function.h?
A1: It is common practice to include the class + function declarations in the .h file and the function implementations in the .cpp file
Q: where did we define .denom and .nom?
A1: These are the private variables of the Fraction classs (declared in fraction.h)
Q: Since the denom and num are private variables, how come you can access them directly in multiply?
A1: The function multiply is as part of the Fraction implementation, so it has privileges to see private data
Q: are all the variables defined inside fraction.cpp public variables that can be called by any functions inside this file?
A1: The functions we are defined are within the Fraction implementations so they have privileges
Q: can chris explain again where other.num and other.denom came from?
A1: live answered
Q: is other just an instance of the function class?
A1: of the Fraction class, yes
Q: im confused why its called other.num instead of just other
A1: The expression is accessing just the num field of the other fraction
Q: what does the reduce function do? was it defined earlier?
A1: Not yet written but idea is to put fraction in simplest form, i.e. 12/24 reduces to 1/2
Q: Style-wise, should we put explanation comments in the .h file? The .cpp file? Both?
A1: Interface comments (for client) go in .h, implementation comments (for implementers) go in .cpp
Q: are we finished with recursion?
A1: We wil revisit some recursive data types and algorithsm later
Q: Is the (double) in parentheses necessary in the decimal() method?
A1: live answered
Q: Hi Chris, can you explain what a constructor is again? A bit more concrete rather than theoretical would be appreciated
A1: live answered
Q: from my understanding i thought client is the program that uses the class. so why would that have comments? and also for implementers, what i understood is that we mainly look at the header files and rarely look at the cpp files. am i getting that correctly?
A1: live answered
Q: what about ::npos?
A1: live answered