Final Exam, CSCI 330, Fall 2020


  1. [4 points] What is a namespace and what problems do namespaces solve when implementing a large project?
    A namespace is a scope. It allows for identifiers to be associated with an segment of code. Variables and constants in different namespaces can have the same identifier yet be distinctly identified. Functions in different namespaces can have exactly the same signature and still be uniquely identified.

    Namespaces eliminate the need to coordinate identifiers to assure that they are unique across different portions of a large project.

  2. [6 points] Name or describe three methods by which class member data can be initialized. When, if ever, is the use of one of these methods preferred or required.
    Member data can be initialized
    • in the body of the constructor.
    • in the constructor intialization list.
    • when the member is defined.
    Constant and reference initialization must be done when the member is defined or in the initalization list.
    If you did not the mention constant and reference part, I took two points off. If you mentioned it later, I added a point back in. I should not have. You should have gone back and fixed this part.

  3. Friends and Encapsulation
    1. [2 points] What is a friend function?
      A friend function is a function that has been given access to all members of a class or struct.
    2. [4 points] Provide an argument against employing friend functions liberally when implementing classes.
      In general allowing any access to private data decreases encapsulation. This allows more code to have access to the private implementation data and increases the amount of code which will need to be modified or can potentially break if the underlying implementation for the class is changed.

      This access also removes any protection from incorrectly setting or manipulating the data in the class.

  4. Class Members
    1. [3 points] Describe the unique properties of static member functions.
      A static member function can be accessed without going through an instance of the class, however, it only has access to static member data.
    2. [3 points] Describe the unique properties of static member data.
      Static member data a single instance of the data shared by all instances of the class.

  5. [4 points] The operator [] can be overloaded in two ways. Describe each. Describe the difference between the two routines.
    These can be implemented as a const to they are on the right hand side of an assignment or as non-const where they would be on the left hand side of an assignment operator.

    IE

    const type & operator[](size_t d) const;
    
    Can be used to read the value and
    type & operator[](size_t d);
    
    Can be used to write a new value.

  6. [5 points] What will be the result of executing the following code. There are no intentional syntax errors. Assume all member functions are correctly implemented elsewhere.
    This will result in a run time error. The function PersonFactory returns a reference to memory which is allocated on the stack. By the time this function returns, that memory has been deallocated and is no longer valid.
    class PersonT {
        public: 
           PersonT()=default;
           void SetName(string s);
           void SetAge(int i);
           string GetName();
           int GetAge();
        private:
           int age;
           string name;
    };
    
    PersonT & PersonFactory() {
        PersonT data;
        data.SetName("Unknown");
        data.SetAge(0);
        return data;
    }
    
    int main() {
         PersonT & person = PersonFactory();
         cout <<  person.GetName() << " is " << person.GetAge() << endl;
       return 0;
    }
    

  7. Consider the following code
    class Base {
        public:
            Base()=default;
            virtual ~Base()=default;
            void PrintClass() { cout << data;}
            void SetData(int d) {data = d;}
        private:
            int data;
    };
    
    class Derived: public Base {
         public:
             void PrintClass();
             void SetData(int d){Base::SetData(d);}
             void ChangeData(int d) {data = d;}
    };
    
    int main() {
        Base * item = new Derived;
        item->ChangeData(x);
        item->PrintClass();
        return 0;
    } 
    1. [4 points] There are two non-syntax reasons that this code will not compile. What are they? (Identify the line where the error occurs and describe the error)
      Line 15 is problematic as the derived function does not have access to the private member data in the base class.

      Line 21 is problematic as item is a pointer to the base class and ChangeData is a member of the derived class.

    2. [4 points] How can each error be corrected.
      Line 15, either add a setter in the base class to change the data, call the base class' SetData function or make data protected.

      Line 21, make a virtual function ChangeData in the base class,

  8. [5 points] Describe the problem caused by the following code. How can this problem be corrected? (Again, there are no intentional syntax errors)
    This has the diamond problem. Make the derived classes virtual
    class Derived1: virtual public Base { ...
    class Derived2: virtual public Base { ...
    
    class Base {
        public:
            virtual ~Base() = default;
            ...
        private:
            int baseData[MAX_DATA_SIZE];
    };
    
    class Derived1: public Base {
      ...
    };
    
    class Derived2: public Base {
      ...
    };
    
    class Mixed: public Derived1, public Derived2 {
      ...
    }; 

  9. [6 points] Provide the code for a templated function Mode that takes a vector of elements and returns the item occurring most frequently. If multiple items occur with equal maximum frequency, return the one with the minimum value.
    template <typename T>
        T Mode(vector<T> data) {
            // assumes that there is data in the array.
    
            T frequent;
            T current;
            int frequentCount{1};
            int currentCount;
    
            size_t i;
    
            sort(begin(data), end(data));
    
            frequent = data[0];
            // find the count of frequent;
            i = 1;
            while ( i < data.size() and data[i] == frequent) {
               frequentCount ++;
               i++;
            }
    
            if (i < data.size()) {
               current = data[i];
               currentCount = 1;
               i++;
            }
    
            while (i < data.size()) {
                if (current == data[i]) {
                  currentCount++;
                } else {
                   if (currentCount > frequentCount) {
                      frequent = current;
                      frequentCount = currentCount;
                   }
                   current = data[i];
                   currentCount = 1;
                }
                i++;
    
            }
    
            if (currentCount > frequentCount) {
                 frequent = current;
                 frequentCount = currentCount;
            }
            return frequent;
        }
    
    I am mostly looking for the template information. I needed something you could not google for an answer. Only minor points will be taken off for other coding errors.

Submission Instructions