Concept Questions for HW02
Concept Questions for HW02
Questions
Before you begin the programming portion of this homework assignment, read and answer the following conceptual questions. Your answers should be submitted to Canvas.
- (3 pts) In C++, what is a virtual function and how does it relate to object polymorphism? Say I have a class
Base
and a classDerived
that inherits fromBase
. Both classes implement a non-virtual function calledfunc()
. Iffunc()
is invoked by dereferencing aBase*
that actually points to an instance ofDerived
, which implementation offunc()
will be called, and why? - (2 pts) In
polygon.cpp
there is a constructor for thePolygon
class that takes in a number of sides to construct a regular, convex polygon. There is a section of this constructor's body that sets up "indices" to construct triangles out of the polygon's perimeter vertices. Please draw how these triangles would be formed for a regular convex pentagon (five-sided polygon), and label each vertex with its index number. You may draw your image on a piece of paper and take a photograph, or create it in some sort of image editing software. Your drawing need not be perfect, just make sure it's understandable. In the end, please submit your drawing as a.png
image. - (5 pts) If I want to write a function that builds my entire scene graph and returns its root node, what should the return type of the function be? Additionally, where in memory should I instantiate the nodes of my scene graph? Why should I instantiate my nodes in this portion of memory?
Answers
(3 pts) Virtual Function and Object Polymorphism in C++
A virtual function in C++ is a member function declared in a base class that can be overridden in a derived class. It enables polymorphism, allowing the program to decide at runtime which version of the function to call, depending on the actual type of the object pointed to by a base pointer or reference.
If both the Base
and Derived
classes have a non-virtual function func()
, and you dereference a Base*
that actually points to an instance of Derived
, the implementation of func()
from the Base
class will be called. This is because non-virtual functions use static binding (compile-time binding), meaning the function to call is determined by the type of the pointer (Base*
), not the actual object it points to (Derived
).
(2 pts) Triangles Formed in a Regular Convex Pentagon
The constructor generates the vertices of the polygon in a loop and stores them in m_vertPos
. Then, it forms triangles by pushing vertex indices into m_vertIdx
. The first vertex (index 0) is shared by all triangles, and the other two vertices for each triangle are consecutive perimeter vertices.
(5 pts) Function Return Type for Scene Graph
To build the scene graph, the function should return a std::unique_ptr<Node>
. This ensures that the root node has sole ownership, preventing memory leaks and dangling pointers.
Nodes should be instantiated in heap memory because the scene graph may have a dynamic, growing structure. Allocating on the heap allows nodes to persist beyond the function scope and ensures efficient memory management, as stack memory is limited and unsuitable for objects with uncertain lifetimes or large sizes.