Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please, answer the with c++ Module 9: Odds and Ends Questions Question 1 How is

ID: 3868692 • Letter: P

Question


Please, answer the with c++

Module 9: Odds and Ends Questions Question 1 How is the static_cast operator different from the dynamic_cast operator? Question 2 Which of the following are not examples of correct usage (conceptually or syntactically) of auto ptr? (Assume the needed header files have beenincluded.) auto ptr int> pia(new int[20]) auto ptr str> (new string); int ngue = 7; auto_ptr int pr(&rigue;): auto ptr dbl (new double) Question 3 Give at least three examples of convenience advantages that a vector object has over an ordinay array. Question 4 Why didn't the STL designers simply define a base iterator class, use inheritance to derive classes for the other iterator types, and express the algorithms in terms of those iterator classes?

Explanation / Answer

1)Answer:

static_cast operator:

The static_cast operator converts a given expression to a specified type.

static_cast can perform conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived). No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, it does not incur the overhead of the type-safety checks of dynamic_cast.

Syntax:

The output of this example is:

dynamic_cast operator:

The dynamic_cast operator checks the following types of conversions at run time:

dynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.

This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.

But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type.

example: