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 cs35/inclass/w02/inheritance/testPerson.cc as it executes. Include the stack and the heap.
  4. Write an Animal class with the following features:
    • private type (a string) and weight (a float) data members.
    • a constructor which takes two appropriate parameters, which it uses to initialize the type and weight of the Animal.
    • appropriate public getType(), getWeight(), and print() accessor functions
    Then write a Pet subclass of Animal with the following features:
    • a private name (a string) data member
    • a constructor which takes a type, a name, and a weight, which it uses to initialize the type, name, and weight of the Pet
    • appropriate public getName() and print() accessor functions, such that the Pet print() function will be executed when a Pet-as-Animal's print() function is called.
    Finally, write a main() function that declares a pointer p to an Animal, creates a single Pet on the heap and saves the pointer to that Pet as p, and then prints the Pet and releases its memory. (For my inadequate representation, I heartily apologize to anyone with a pet rock...)