CSE 143 Quiz 2: July 9th, 1998
Time limit: 20 minutes
Closed book, closed notes

Name:

Section:

Question 1:
Please circle true or false.
 
1. True False   The class mechanism is the only way to define new types in C++.
2. True False   In a class declaration, the collection of all public and private member functions is referred to as the interface of that class.
3. True False   Inside the body of a member function of a class, you must use the scope resolution operator (::) to refer to the data fields of the receiver.
4. True False   In C++, a struct is just like a class except that members are assumed by default to be public.
5. True False   In the declaration 
       int x; 
x is assigned a default value of 0 because no explicit initializer is given.
6. True False   Constructors force every declaration to be tied to some piece of initialization code.
7. True False   It is possible to have more than one constructor in a class.
8. True False   Overloading works by differentiating functions based on the names of the their arguments.
9. True False   Constructors eliminate the need for further error checking in a class, because the instances are guaranteed to always be well-formed.
10. True False   It is possible to create many instances of a single type at runtime.
Question 2:
Why is it unfortunate that a class declaration in a header file must include all private data and functions?

Putting private member data and function declarations inside the header file makes some of the implementation of a class visible to the client.  A mischievous client could exploit that information and break the abstraction barrier provided by the interface to that class.
(more on reverse of page)

Question 3:
Consider the following partial class declaration: 

        class Square
        {
        public:
            Square( double sl );

            // Return the area of the square  
            double getArea();
            // Return the perimeter of the square
            double getPerimeter();
        private:
            double side_length;
        };
  1. Add a data member to store the side length of the square as a double. see above
  2. Add a constructor declaration that takes a double as an argument, representing the initial side length of the square. see above
  3. In the space below, give an implementation of the getArea member function.
  4.    double Square::getArea()
       {
           return side_length * side_length;
       }
Question 4:
There's a problem with the constructors in this program that prevents it from being compiled. Explain the problem.
        class A {
        public:
            A( int n );
        private:
            int field;
        };

        A::A( int n )
        {
            field = n;
        }

        class B {
        private:
            A a;
        };

        int main( void )
        {
            B b;
            return 0;
        }
This one is a bit tricky.  The problem is that the class A has an explicit constructor taking an int, and no default constructor.  B does not have an explicit constructor, and so one is provided by the compiler.  The automatically-generated constructor tries to initialize all the fields of B by calling their default constructors; in particular it tries to call A::A().  But A::A() doesn't exist, so the compiler comlpains that B's constructor is illegal!  This is a pretty subtle question, so don't worry if you didn't get it.

Question 5:
What two overloaded operators have we been using since the start of the course?

<< and >>, overloaded as the insertion and extraction operators for stream I/O.