Download CSE687 Object Oriented Design MidTerm Exam A2 - Spring 2001 - Prof. James Fawcett and more Study Guides, Projects, Research Engineering in PDF only on Docsity! CSE687 Object Oriented Design MidTerm Exam A2 Spring 2001 1. Given the class declaration: class A { … }; class B { … }; class C : public A, public B { … }; Implement two functions in C, one that returns a pointer to C’s base A, and one that returns a pointer to C’s base B. How would you demonstrate that the operation of these functions is correct. Assume that you can modify the base classes in some small way if you need to. Do you need to? see attached code. To prove the operations are correct, you must show that the addresses of the A and B objects are contained in the memory extent of C. 2. Describe how you would apply the Open/Closed Principle to your design for project #3. A statement for project #3 is attached to the back of this packet for reference. The Open/Closed Principle states that a programs classes must be closed for modification, but open for extension. The tokenizer class provides two ways to accomplish that. The first is through inheritance. The tokenizer module provides a notification interface. Applications derive from the notification interface and override virtual functions in the interface to extend the behavior of the tokenizer’s response to its internal events. The project also extends the tokenizer by using template-based rules for tokenizing passed as a template parameter to the tokenizer. Note that the tokenizer code is not modified in any way to accomplish these extensions. 3. Write a function that accepts an STL vector of strings and returns a vector of doubles. It is required to do that by converting any string in the incoming vector to a double if its characters can be interpreted as the representation of an double. What happens if there are no such strings in the input vector? see attached code. If there are no convertible strings then the returned vector is empty. 4. Write a template-based function that accepts two pointers to objects of some arbitrary type and returns with the pointers swapped. That is, if the first pointer points to object A and the second points to object B when the function is called, then when the function returns, the first pointer must point to B and the second to A. Please do this as efficiently as possible. see attached code. CSE687 Object Oriented Design MidTerm Exam A2 Spring 2001 5. Given the class declaration, where the ellipsis “…” represents undisclosed operations: class X { public: void mf(); … }; class Y : public X { … private: double d; }; void aFunction(X *x) { x[1].mf(); } Are there any inherent problems with this code? If you think so, say what the problem is. If there is a problem, is there anyway to fix it without changing the signature of the function? Y y[3]; aFunction(y); Problem here is that pointer arithmetic is not polymorphic. When y is passed to aFunction, the statement x[1] is equivalent to *(x+1), which adds one base object size to the memory address stored in pointer x and attempts to access an object there. That only works when the array passed is an array of base objects, as shown in the first part of main in the attached code. You can fix problem by passing an array of base pointers to objects, rather than an array of objects. That, however, changes the signature to aFunction(X **x); 6. Students are supported by many different financial sources: loans, relatives, their government, corporate scholarships, and by the university, based on both academic merit, and need. The rules by which the university and these sources of financial aid interact vary, depending on the source. Use OMT or UML diagrams to represent the financial relationships between the university, its students, and the various specialized sources of financial aid. You should assume that all nouns and the word specialized in this paragraph are significant. see attachment. 7. Suppose that X is a class that is declared as follows: class X { public: explicit X(const char *) ; void aFunction(const X &x) const; … }; Can we directly pass a literal string to aFunction? If not, how can you pass a literal string to the function? What contract does this function make with its clients? No, a literal string cannot be directly passed to aFunction, because the promotion constructor is qualified as explicit. You can pass a literal string by enclosing it in an explicit call to the promotion constructor, e.g., X(“a string”), before passing it to the function. The function guarantees that it will not change the state of the object used to invoke it. It also guarantees that it will not change the object passed as an argument.