CS 176, Introduction to Computer Science II

Summer 2001 - Quiz 4


  1. A colleague of yours wrote the following code
    #include <iostream>
    int main() {
      cout << "Input a hex number:  ";
      int x;
      cin >> hex >> x;
      cout << "The hex number you entered was " << x << ".\n";
      }
    
    and got the following results when the program was run
    Input a hex number:  10
    The hex number you entered as 16.
    
    Your colleague, knowing that the hex stream manipulator is sticky, is confused by this output. What explanation do you give to clear up your colleague's confusion?


    Base manipulators are sticky, but only to the streams to which they're applied; cin and cout are separate streams. Your colleague's setting the base for cin to hexadecimal has no affect on cout, which maintains the default decimal base.


  2. Another colleague of yours wrote the following code
    class T {
      T() { }
      };
    
    int main() {
      T t;
      }
    
    and was puzzled when the compiler rejected it with an error about there being no constructor for class T. What explanation do you give to clear up your colleague's puzzlement?


    Your colleague's compiler could have been a bit clearer by pointing out that the class T has no public constructor. The given default constructor has private access, which is the default access for classes, and because the class contains one constructor, the compiler won't supply any other ones.



This page last modified on 3 August 2001.