Homework 2 Solutions

I. Loops

  1. Question: Assuming all appropriate libraries are included and all variables needed exist and have values assigned, write a for loop that will do the following calculation:


int sum = 0;
for (unsigned int i = 0; i < n; i++)
   sum += pow(2,i);

II. Pass by Reference

  1. Question: What is pass by reference?

Solution: There are two ways of passing a variable into a function: pass by value and pass by reference. Pass by value just passes a copy of the variable into another temporary variable in the function, so when you change the value of the variable in the function it doesn't effect the value of the variable in the calling function. Pass by reference is the exact opposite. It references to the variable in the calling function, so if you change the value of the passed variable in the function it changes the value of the variable in the calling function.

  1. Question: When will you need to use pass by reference as opposed to pass by value? Give specific reasons.

Solution: When you have a huge object and you want to move it around. You also need to pass by reference if you want to have a relation instead of a function, what that means is that you want multiple results. A function only allows you to have one result, a relation has many.

  1. Question: What would the output of the following code be?

void foo1(int a);
void foo2(int & a);

int main()
    int a = 0,
    b = 0,

    c = a;
    cout << c << endl;
    c = b;
    cout << c << endl;

    return 0;

void foo1(int a)
    a = 8;

void foo2(int & a)
    a = 8;

Solution: 08


  1. Question:What is the purpose of encapsulation? Please elaborate as much as you can.

Solution: Encapsulation is often times called data hiding. What that means is that you protect important data in the private section of your object. It means more than that though. Encapsulation can also mean that you are reducing the interface of the object to the absolutely necessary components. What that means is that you can have a bunch of other facilitating functions of that object hidden in the private so that the person using that class doesn't have to worry about that function if they don't have to.

  1. Question: Explain what the following terminologies are: attribute, inspector, mutator, facilitator.


  1. Attribute: A descriptive element of an object. It describes the look and feel of the object, like what it consists off.
  2. Inspector: A function that lets you know what an attribute looks like. All it ever does is return an attribute. In the strictest format it is also a constant function.
  3. Mutator: A function that allows the user to change an attribute of the object. In the strictest format the value passed in is constant too.
  4. Facilitator (aka Auxilary Functions): It aides the object in doing "object specific" tasks. Many times they are functions like display or helper functions for other methods outside the object or they are just private methods that help the other functions in the object.

Consider the following code for Questions 3 and 4:

class Foo
    int _data;

    Foo function1(const Foo a);
    Foo function2(Foo a) const;
    Foo function3(const Foo & a);
    const Foo function4(Foo a);

  1. Question: Which of the member functions of Foo are equivalent in security?

Solution: function1 and function3. Both have parameters that are not pointers that can't be changed within the function.

  1. Question: Write a piece of code in the main program that will cause an error with function1 (oops, I said function2 in the actual question) of the Foo object, assuming Foo is declared and/or included. Do the same with function4.

Solution: The following code will break both functions:

const Foo myFoo, foo2;
myFoo.function1(foo2); //will cause an error

Foo foo3;
foo3 = myFoo.function4(foo2); //will cause an error

IV. Others

  1. Question: What is the double colon (::)? What does it mean?

Solution: This is called a scope resolution. What a scope resolution does is that it tells the compiler that the entity on the right of it belongs to the entity on the left of it. When the you leave the left side blank it just means that the entity on the right of it belongs to the current entity's parent entity. What that means can be illustrated with the next question.

  1. Question: What will the output of the following code be?

    int main()
        int a = 0;

        cout << a << endl;

            int a = 9;

            cout << ::a << endl;

            a = 5;

            ::a = 4;

        cout << a << endl;
        return 0;

    Solution: (NOTE: It does not cause an error! You can have a variable of the same name in a scope inside the scope that has the duplicate name)