Answer template class t t findminvectort vect

Info icon This preview shows pages 5–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Answer: template <class T> T findMin(vector<T> vect) { typename vector<T>::iterator it = vect.begin(); T x = *it; ++it; while (it != vect.end()) { T temp = *it; if (temp < x) x = temp; ++it; } return x; } Part B (10 points): The function you wrote in part A uses the < operator, which is not defined on all types. Explain how the C++ compiler handles this. When/where would an error occur if you tried to use this function on a type for which < was not defined? Answer: The C++ compiler type checks a template only when it is instantiated (i.e. when it is used). This means that the the compiler only checks if the less-than operator is defined on the types for which findMin is actually used. If you tried to instantiate findMin on a type that did no have a less than operator, the compiler would report the error for that instantiation (it would tell you where in the template body the less-than is, but first would tell you where the offending intstantiation is). 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Question 4: Object Layout For this question, use the following object layouts for objects of type Foo and Bar : <vtbl> <vtbl> int num1 int counter min inc max Objects of type Bar Objects of type Foo int num2 Part A (10 points) Class Baz is declared as follows: class Baz: public Foo, public Bar { int xyz; }; Draw the layout of an object of type Baz in the space below: Answer: <vtbl> <vtbl> int num1 int counter min inc max int num2 int xyz 6
Image of page 6
Part B (5 points) Briefly explain what a vtable is and what is used for. Answer: A vtable is a table of function pointers that are used for dynamically dispatching functions. Each object that has at least one virtual function has as its first field a pointer to a vtable. When a virtual function is called, the pointer to the vtable in the object is followed, and the appropriate function pointer is read from the vtable. That function is then called. Part C (5 points) Given the following declarations: class A {...} class B: public A {...} class C: public A {...} class D: public B, public C {...} How many A sub-objects does a D have? Suppose that this is not the behavior you desired, indicate what changes need to be made to the above declarations to get the other possible behavior.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern