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?

    An object holds variables and functions (methods). You can put an object in a variable. A class is a template for producing objects. It is just a type of objects. You cannot put a class in a variable.

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

    The constructor of a class is used to initialize the objects that the class produces.

  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.

    Either of the following will work. The first approach,

         ifstream in;
         in.open("mydata.txt");
      
    creates the object without tying it to a file, and then ties it to the file using the open method. The second approach,
       ifstream in("mydata.txt");
      
    uses a constructor for the ifstream class that creates the object and ties it to a file as part of the initialization process.

  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.

    Either of the following will work.

        in.get(c);
      
    or
        c = in.get();
      

  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.

    Here are two ways to do this.

       ofstream out;
       out.open("myout");
      
    or
       ofstream out("myout");
    

  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.

        out << c;
      

  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.

       void copyfile(char name[])
       {
         char c;
    
         ifstream in(name);
         if(!in) return;
    
         while(1) {
           in.get(c);
           if(!in) break;
           cout << c;
         }
         in.close();
       }
    

  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;
      };
    

        double Circle::area()
        {
          return 3.14159265*myRadius*myRadius;
        }
    
        Circle::Circle(const Point& center, double radius)
        {
          myCenter = center;
          myRadius = radius;
        }