Computer Science 2610
Fall 2004
Practice questions for quiz 3

This will be a closed book quiz. You may bring one 8.5x11 page of prepared notes, written on both sides.

  1. What is the difference between an object and a class?

  2. What is the responsibility of a constructor for a class? What do you use it for?

  3. You would like to open a file called "mydata.txt" for reading. Write a C++ declaration that will create a object that can be used to read from that file, in a way similar to the way the object cin is used to read from the standard input.

  4. Now you would like to use the object that you created in the previous question to read the first character from file "mydata.txt", and to store it into variable c. Write a statement to do that.

  5. You would like to open a file called "myout" for writing. Write a declaration that will create an object that can be used to write to that file, in a way similar to the way the object cout is used to write to the standard output.

  6. Now you would like to use the object created in the previous questions to write character c to filel"myout". Write a statement to do that.

  7. Write a function that takes a file name as a parameter, and that copies the entire content of that file to the standard output. That is, it should read the file and write it to cout. If the file cannot be opened, the function should return without printing anything.

  8. A circle in the plane can be described by its center point and its radius. Class Circle describes objects that are circles in the plane, and its definition is partially given below. Some operations on circles are provided. For example, function area returns the area of a circle.

    Write the definition of function area, as it would occur in file circle.cpp. That is, the definition of area is physically outside the class. Use 3.14159 as an approximation of pi.

    Also write a definition of the constructor. It should install the given center and radius into the object's variables myCenter and myRadius.

    Note: it is not necessary for you to know the definition of type Point to solve this problem. However, you know that the assignment operator, =, is available for type Point, so you can assign a Point value to a variable of type Point.

      class Circle 
      {
        public:
          Circle(const Point& center, double radius);
          double area();
          ...
        private:
          Point myCenter;
          double myRadius;
      };