CS35: Quiz 1 Study Guide

You should be able to define or explain the following terms:

You should be familiar with all aspects of basic C++ programs, such as those you created for labs 01 and 02. This includes:

Practice problems

  1. Write a C++ program that prompts the user for an integer n and then prints a single output integer, the sum of the integers from 1 to n.

  2. Write a boolean function isPrimaryColor that takes a string as an argument and returns true if the string is "red", "yellow", or "blue", and returns false otherwise. Then write a program that prompts the user for a color and uses isPrimaryColor to determine if their color is primary.

  3. Draw a memory diagram for examples:week-03/inheritance/testPerson.cpp as it executes. Include the stack and the heap.

  4. Write a Shape class with the following features:
    • private color (a string) data member.
    • a constructor which takes in a color and initializes the color of the Shape.
    • appropriate public getColor(), setColor() and print() accessor functions. print() should output the color of the object, e.g. "Shape with color black."
    Then write a Rectangle subclass of Shape with the following features:
    • a private length and width (both int) data members
    • a constructor which takes a color, length, and width, which it uses to initialize the data members.
    • accessor functions: getArea() and print(). print should output the area and color of the Rectangle, e.g. "Rectangle with area 20. Shape with color black."
    Finally, write a main() function that declares a pointer p to a Shape, creates a single Rectangle on the heap and saves the pointer to that Shape as p, and then prints the Shape and releases its memory.