Skip to main content

Section 1.8 Homework 8 -- More on Pointers, Dynamic Memory, Operator Overloading

Exercises Exercises

1.

A primary purpose of the destructor is to ...
  • release an object’s dynamic (heap)memory allocation when the object is no longer in use
  • release an object’s static (stack)memory allocation when the object is no longer in use

2.

The name of the destructor always begins with the special character ____ .
  • ~
  • #
  • $
  • ?

3.

Which of the following is TRUE of the class destructor?
  • The destructor has no parameters and no return value.
  • The destructor has no parameters, but it does have a return value.
  • The destructor has no return value, but it does have parameters.
  • The destructor has both a return value and parameters.

4.

Which of the following is a true statement about when the destructor of a class is activated?
  • If an object of class Foo was created in dynamic memory with the `new` keyword, then the Foo class destructor will be activated when `delete` is called on a pointer holding the address of the object.
  • If an object of class Foo was created and assigned to a variable in the stack, the Foo class destructor will be activated when the variable goes out of scope.
  • If an object of class Foo was passed by value to a function, then the Foo class destructor will be activated to delete the local copy of the object when the function ends.
  • The class destructor is only activated when called explicitly, such as: ~Foo(my_foo)

5.

Dynamic data structures, created in the heap using pointers, let us set the SIZE of the data structure ...
  • at runtime
  • at compilation time
  • during pre-processing

6.

We are implementing an assignment operator with the following prototype:
Image& operator=(const Image& source);
It’s important to check for self-assignment when implementing the assignment operator for a class.Β  Which of the following is a correct check for possible self-assignment?
  • if (this == &source) { return; }
  • if (this == source) { return; }
  • if (*this == &source) { return; }
  • if (*this == source) { return; }

7.

True, or false?Β  The automatic assignment operator fails for any class that uses dynamic memory.
  • True
  • False

8.

Suppose im1 is an existing Image object.
When the following line of code is executed, which function will be activated?
Image im2(im1);
  • The Image class copy constructor.
  • The Image class default constructor.
  • The Image class assignment operator (operator=).

9.

Consider an Image class like the one in our labs. Suppose the Image object `im_2` is already initialized. Consider the following line of code:
Image im_1(im_2.getWidth(), im_2.getHeight());
When this code executes, does it activate the copy constructor?
  • No
  • Yes

10.

Refer to the Image class. Suppose the Image object `im_2` is already initialized. Consider the following line of code:
Image im_1 = im_2;
When this code executes, does it activate the copy constructor?
  • Yes
  • No

11.

Refer to the Image class, as in Lab 3. Suppose the Image objects `im_1` and `im_2` are already initialized. Consider the following line of code:
im_1 = im_2;
When this code executes, does it activate the copy constructor?
  • No
  • Yes

12.

Suppose the function getImage() has the following prototype:
Image getImage();
When the return statement of getImage() is executed, does it activate the Image class copy constructor?
  • Yes
  • No

13.

14.

Assume a Pixel class with member variables named blue, red and green
Implement a member function called is_white for the Pixel class. It takes no arguments.
The function is_white returns true if this is a white pixel (all three color values are 255); otherwise, is_white returns false.

15.

Assume a point class with member variables x, y, member functions get_x() and get_y(), and a constructorpoint(x,y).
Below is the documentation for the addition (+) operator for the point class.
/*
 * addition operator (+)
 *
 * The x value for the result is the sum of the x values for the two points being added, and 
 * the y value for the result is the sum of the y values for the two points being added.
 * returns a new point that is the sum of two other points
 *
 * Example:  if point1 is (4, -2) and point2 is (3, 1), then 
 *           point1 + point2 will return the point (7, -1)
 */
Write the code to turn the given stub definition for the function into a complete implementation. Note that you will need to use get_x and get_y to reach the values, as the operator is not defined as a friend of the class.

16.

Assume a point class with member variables x, y, member functions get_x() and get_y(), and a constructorpoint(x,y).
Below is the documentation for the multiplication (*) operator for the point class.
/*
 * multiplication operator (*)
 *
 * Calculates the dot product of two points by adding the product of the x's
 * and the product of the y's.
 * returns the dot product of two points
 *
 * Example:  if point1 is (4, -2) and point2 is (3, 1), then 
 *           the dot product would be (4 * 3) + (-2 * 1), i.e., 10
 */
Write the code to turn the given stub definition for the function into a complete implementation. Note that this will not be a friend to the class, so you will need to use get_x and get_y to access the member variables values.

17.

Assume a point class with member variables x, y, member functions get_x() and get_y(), and a constructorpoint(x,y).
Below is the documentation for the equivalence (==) operator for the point class.
/*
 * equivalence (==)
 *
 * Two points are equivalent if they have the same x value and the same y value.
 * returns true if point1 and point2 are equivalent; otherwise, returns false
 */
Write the code to turn the given stub definition for the function into a complete implementation. Note that this will not be a friend to the class, so you will have to use get_x and get_y to access the variables.

18.

Assume a point class with member variables x, y, member functions get_x() and get_y(), and a constructorpoint(x,y).
Below is the documentation for an input stream operator (>>) function for the point class.
/*
 * input stream operator (>>)
 *
 *    - takes data from the input stream and assigns new values to the 
 *      instance variables of the point
 *    - returns the 'in' parameter to allow for chaining
 *
 * Note:  The input consists of the following: "(x, y)" where x and y
 *        are doubles.
 *
 * Example:  If the input stream 'in' holds the message
 *               "(-1.5, 3.75)"
 *           then 'in >> pt' will cause pt's data to become
 *           x: -1.5; y: 3.75
 *
Write the code to turn the given stub definition for the function into a complete implementation. This will be a friend to the class, so you can directly write into the x and y variables.
HINT: Be sure to consume all the input, "(x here..., y here...)", including the parentheses, and the comma. You can use the input stream function ignore to skip characters.

19.

Assume a point class with member variables x, y, member functions get_x() and get_y(), and a constructorpoint(x,y).
Below is the documentation for an output stream operator (<<) function for the point class.
/*
 * output stream operator (<<)
 *
 * output a point to a stream
 * returns the 'out' parameter to allow for chaining
 *
 * Note:  The output of a point looks like the following:
 *           "(x, y)"
 *        where x is the x-value and y is the y-value for the point.
 *
 * Example:  Calling the output stream operator on a point with 
 *           x = 4 and y = -2 would produce the following: (4, -2)
 */
Write the code to turn the given stub definition for the function into a complete implementation.
HINT: The output stream operator is NOT a friend of the class. Use the getters to access the values of member variables x and y.
You have attempted of activities on this page.