X459 Midterm

Due in class April 7

Please answer the following questions. For questions where description is necessary, please be specific and include at least a couple of sentences of detail. Include examples where needed. You may use any textbook, WWW, compiler, or code resources that you would like. Just don't talk to anyone!


Section I: Code analysis (25 pts)

For each of the following pieces of code, answer the following questions:

  1. Would it compile? Explain why or why not.
  2. Would it run correctly? Explain why or why not.


// 1.
int& abc()
{
   int a  = 5;
   return a;
}


// 2.
int* bcd()
{
   int a = 5;
   return &a;
}


// 3.
int* cde()
{
   int a = 10;
   return a;
}


// 4.
int& def(int &a)
{
   return (a = 6);
}


// 5.
int* efg()
{
   return new int(3);
}

Section II: Descriptive. (35 pts)

  1. Why is it important to keep declarations out of the global namespace and declare as much locally as possible?




  2. Why is it important to keep data and methods private in classes when possible?




  3. Why is operator overloading useful?




  4. Give 3 code examples of when a copy constructor of a class might get called even though the code does not explicitly call it.




  5. Why does operator= typically return a reference and not a value?




  6. Why does operator[] typically return a reference and not a value? Give an example




  7. Explain the output of the following code:
    #define NUM 2
    #define NUM2 NUM + NUM
    #define NUM3 3 * NUM2
    cout << NUM3 << endl;
    

Section III: Programming

Below, you have Map.h and main.cpp. Map.h is the prototype for a class, and main.cpp is an example main program which should show you how the class behaves and allow you to test your implementation. Please write Map.cpp. (extra credit: implement the [] operator for the class instead of getMapping() and addMapping()).


// Map.h.
//

const int MAXMAPS = 100;

// 
// Class to store mappings of strings to integers.
//
class Map
{
   private:
    // Array of the strings and values we've mapped.
    // The string in position 0 of _index will correspond
    // to the integer in position 0 of _values, and so on.
    char* _index[MAXMAPS]; 
    int _values[MAXMAPS]; 
// Number of strings that have been mapped so // far. Starts out at 0 and is incremented whenever addMapping() // is called. int _numMaps; public: // Constructor. Should build a Map with no elements. Map(); // Copy constructor. Contents of cmap should be copied. // The map should be left with the same number of elements // and the same elements (but be sure that the two Maps // don't share the same data on the heap Map(const Map& cmap); // Same comments as above for the copy constructor. Map&; operator=(const Map& cmap); // Destructor. Should deallocate all of the strings that were // Allocated into _index[]. ~Map(); // Map str to value. Do this by adding elements into // the _index and _values array and then incrementing _numMaps. // If there are more than MAXMAPS mappings, then print an // error and return. void addMapping(const char* str, int value); // Get the mapping of str. Look up str in the _index array // then return the corresponding integer from _index (this // routine may be inefficient, but that's OK for this usage.) // If no mapping exists for this string, return a -1. int getMapping(const char* str); }; // Main.cpp. Tests the Map class // #include #include "map.h" void main() { Map mp; // Default constructor mp.addMapping("Hello",2); // Map "Hello" to 2 mp.addMapping("There",4); // Map "There" to 4. cout << mp.getMapping("Hello") << endl; // Should print 2 cout << mp.getMapping("There") << endl; // Should print 4 cout << mp.getMapping("Class") << endl; // Should print -1 Map* mp2 = new Map(mp); // Copy constructor cout << mp2->getMapping("Hello") << endl; // Print 2 mp2->addMapping("Student",3); cout << mp2->getMapping("Student") << endl; // Print 3 cout << mp.getMapping("Student") << endl; // Print -1 Map mp3; mp3 = *mp2; // Assignment operator delete mp2; cout << mp3.getMapping("Student") << endl; // Print 3 cout << mp.getMapping("Hello") << endl; // Print 2 }