CS510_CW12_Nengyin & Kaiqin
Examine the source code in src/structs carefully.
i. Explain how this code differs from the equivalent C implementation that you examined in a previous class. Be specific and detailed.
ii. Explain what a #define guard does, and why it could be useful.
It used to ensure the header file is only included once by the preprocessor. Thus avoid the second definition of objects, like struct, type, functions and so on. So that the program can compile correctly.
iii. Explain what type overloading is, and why it could be useful.
It defines both operator and function overloading, which is the same name but different types or different numbers of arguments, but only return type different doesn't work. It is useful because you can have multiple definitions for the same function name in the same scope. When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions.
// From: https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
iv. Explain the difference between "pass by reference" in C and "pass by reference" in C++.
In C, the function argument must defined as a pointer (memory address), like *wp, which can be accessed by either (*wp).x or wp->x. When passing a pointer to a function, the address it contains is copied into the function. Modifying that pointer inside the function will not change the pointer outside the function - however, modifying the object it points to will change the object outside the function.
In C++, it is an explicit reference, like &wp, which can be accessed by wp.x. When passing an argument to a function that takes reference type, the object is truly passed by reference. There are no pointers involved, no copying of objects, nothing. The name inside the function actually refers to exactly the same object that was passed in.
v. Explain what operator type overloading is, and why it could be useful
Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other functions, an overloaded operator has a return type and a parameter list. Because you can define the same operater with different type or numbers of arguments. It can choose the most appropriate operater by comparing the argument type or numbers you used.
vi. Finish implementing the needed functions, and make sure your code compiles and runs. Explain the output carefully.
Output and explanation:
-----Pass by value test-----
Original 3 vector:
3-Vector : [0, 0, 0] # Print the value of Vec3 before set_vec3, which uses pass-by-value.
Original 3 vector after modification:
3-Vector : [0, 0, 0] # Print the value of Vec3 after set_vec3, but the result stays the same because it uses pass-by-value, which does not change the value of outside function.
New 3 vector after modification:
3-Vector : [1, 2, 3] # It returns the modified value of vprime.
-----Pass by reference test-----
Original 2 vector:
2Vector : [0, 0] # Print the value of Vec2 before set_vec2, which uses pass-by-referess.
Original 2 vector after modification:
2Vector : [1, 2] # Print the value of outside value of Vec2, which has been changed to the new value because it uses pass-by-reference.
-----Operator+ Vec3 test-----
Original 3 vector:
3-Vector : [1, 2, 3]
The sum of two vprimes is:
3-Vector : [2, 4, 6]
-----Operator+ Vec2 test-----
Original 2 vector:
2Vector : [1, 2]
The sum of two vprimes is:
2Vector : [2, 4] # Added two tests for the newly-written operator+ functions.