FINAL EXAM ANSWERS > > 1. [15] Differentiate object-oriented and procedural programming. > Describe the benefits of using object oriented programming for > code maintenance and development. object oriented programming introduces classes and objects that combine data and code that operates on them. The uses of classes and objects allows to make code more modular and thus simplify code maintenance and development. > > Differentiate between public and private member functions. private members may be accessed only inside member this class' member functions. > Explain why this feature is introduced. Access modifiers (private and public) are used ensure that private members are only accessed within public member functions. This makes the code that accesses the private members limited. This way object-oriented programming makes code more modular and easier to understand and maintain. > For the class definition > below, add one private member variable and one public member > function that accesses the variable. > > class myclass{ > public: > private: > }; class myclass{ public: void setData(int d){data = d;} private: int data; }; > > > 2. [10] Define pointer and explain its purpose. pointer is a variable that holds memory address to a memory location. With operations of reference and dereference, a pointer may be used to access and modify the value stored in this memory location. Since the memory address stored by the pointer may be changed, a pointer may be used to sequentially manipulate several memory locations. > Given the following > code, explain what each line does and what the last line outputs > int *p, a[10], i=5; // declares a pointer, an array and a scalar variable p=&i; // pointer holds the address of variable "i" ++(*p); // increments the value of variable pointed to by p (to 6) a[0]=*p; // stores the value pointed to by p, in the first element of array "a" // so a[0] is now 6 p=a; // p stores the address of the first element of the array "a" cout << p[0]; // outputs the value of the first element of the array, outputs 6 > > 3. [15] The following class definition uses a pointer attribute > to store a dynamically allocated array > class myclass{ > public; > myclass(int s){size=s; a=new int[size];} > private: > int size; > int *a; > }; > > Explain the purpose for overloaded assignment. For the above class, > write the prototype and an outside definition of the overloaded > assignment that protects against self-assignment and permits > stackability. overloaded assignment is used to implement the correct operation of assignment for objects with dynamically allocated member variables myclass& operator=(const myclass& rhs); // prototype // out-of-line definition myclass& myclass::operator=(const myclass& rhs){ if(this != &rhs){ delete [] a; size = rhs.size; a = new int[size]; for(size_t i=0; i < size; ++i) a[i] = rhs.a[i]; } return *this; } > > 4. [10] Differentiate between vectors and (raw) arrays. Unlike raw arrays, vectors make its size accessible through a member function; they can be passed by value and returned from a function; their size can be changed; elements can be added to the tail, and middle of the vector. > Explain what iterator is. Iterator is a way to access elements in a vector. It is related to pointers. Its primary purpose is for inserting and erasing elements in the middle of the vector. > Given the following vector definition > > vector v; > > Write code that uses an interator to insert an element into > this vector and removes an element from this vector. vector::iterator it = v.begin(); v.insert(it, 55); v.erase(v.begin()); > > 5. [10] Given this code inside main() function definition > > const int SIZE=100; > int a[SIZE][SIZE]; > > give a definition of a function that accepts this array as a parameter, > finds the maximum element in this array and returns this maximum > element as a return value of the function. int maxFind(int arr[][100], int size){ int max = arr[0][0]; for(int i = 0; i < size; ++i) for(int j =0; j < size; ++j) if(max < arr[i][j]) max = arr[i][j] return max; } > > 6. [10] Define recursion. Give an example recursive function > definition. Compare recursion with iteration. Rewrite your > recursive solution with iteration. recursive function definition contains the invocation of the same function. Every task that has a recursive solution also has an iterative solution. Recursive solution tends to be more elegant. Iterative solution may be less easy to understand (more verbose) since the parameters of the problem has to be explicitly maintained. recursive solution: int sum(int array[], int size, int index){ if(index < size) return array[index] + sum(array, size, index+1); else return 0; } iterative solution: int sum = 0; for(int index=0; index < size; ++index) sum += array[index]; > > 7. [10] Describe the need for namespaces. Define three styles of > employing (using) names from a namespace. List these styles > relative advantages and disadvantages. Explain why importing all > the names from a namespace in a header file is considered bad > style. namespaces are a mechanism to break up global scope and minimize name collisions. The three ways are: 1. explicitly resolving scope: MyNamespace::myfunc(); adv: no possiblity of a name collision, clearly states which namespace the name comees from dis: code becomes sparse (verbose) 2. importing a particular namespace using MyNamespace:myfunc; myfunc(); adv: more terse, greater possibility of name collision dis: have to state a using-statement for each imported name 3. importing all names from a namespace using namespace MyNamespace; myfunc() adv: tersest dis: greatest possibility of name collision Importing all names in a header file is poor style because this happens without the knowledge of the programmer who includes the header file. > > > 8. [10] Explain the need fro UML diagrams in > programming. Differentiate between class and object diagrams. Give > examples of use-case and state diagrams. UML diagrams are used for code documentation and development. Pictures in diagrams make it easy to see the structure and operation of the code. Class diagram is static relationships between classes. Object diagram is shows relationship between specific objects during the the program execution. Objects contain state which is shown on object diagram. Also, as object state changes during program execution, there may be several different object diagrams. > > 9. [10] Motivate (explain the need for) automatic code documentation > system like Doxygen. Give an example of Doxygen short (one line) > and long (multi-line) comment and explain what they are for. not covered in course this semester