Docsity

CSE687 MidTerm #3: Object Oriented Design - N-dimensional Vector and Shared Object Access , Exams of Engineering

Toggle sidebar
bg1
CSE687 Object Oriented Design MidTerm #3 Spring 2003
Midterm #3 – Instructor’s Solution
Name:_________________________________ SUID:____________________
This is a closed book examination. Please place all your books on the floor beside
you. You may keep one page of notes on your desktop in addition to this exam
package. All examinations will be collected promptly at the end of the class period.
Please be prepared to quickly hand in your examination at that time.
If you have any questions, please do not leave your seat. Raise your hand and I will
come to your desk to discuss your question. I will answer all questions about the
meaning of the wording of any question. I may choose not to answer other
questions.
You will find it helpful to review all questions before beginning. All questions are
given equal weight for grading, but not all questions have the same difficulty.
Therefore, it is very much to your advantage to answer first those questions you
believe to be easiest.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CSE687 MidTerm #3: Object Oriented Design - N-dimensional Vector and Shared Object Access and more Exams Engineering in PDF only on Docsity!

Midterm #3 – Instructor’s Solution

Name:_________________________________ SUID:____________________

This is a closed book examination. Please place all your books on the floor beside

you. You may keep one page of notes on your desktop in addition to this exam

package. All examinations will be collected promptly at the end of the class period.

Please be prepared to quickly hand in your examination at that time.

If you have any questions, please do not leave your seat. Raise your hand and I will

come to your desk to discuss your question. I will answer all questions about the

meaning of the wording of any question. I may choose not to answer other

questions.

You will find it helpful to review all questions before beginning. All questions are

given equal weight for grading, but not all questions have the same difficulty.

Therefore, it is very much to your advantage to answer first those questions you

believe to be easiest.

1. Write a declaration for a class that represents an n-dimensional vector, where

n is set by the application using the vector class. Assume that the vector

must support element^1 -wise addition and multiplication by a scalar. Try to

support all the operations expected of a built in type, plus the need to access

each element of the vector. You are not asked to provide definitions for the

class members.

//---- start of the required answer --------------------------- template class Vector { public: Vector(int n); Vector(const Vector& v); ~Vector(); Vector& operator=(const Vector& v); // v1 = v Vector& operator=(const T& t); // v1 = t Vector operator(const T&t); // v1 = v2t Vector& operator+=(const Vector& v); // v1 += v Vector operator+(const Vector& v); // v1 = v2 + v Vector& operator-=(const Vector& v); // v1 -= v Vector operator-(const Vector& v); // v1 = v2 – v T& operator[](int n); // v1[3] = t T operator[](int n) const; // t = v1[2] private: int _numElem; T* array; }; //----< global function  v1 = tv2 >------------------------- Vector operator(const T& t, const Vector& v); //---- end of the required answer ----------------------------- template Vector::Vector(int n) : array(new T[n]) {} template Vector::~Vector() { delete [] array; } // other members defined here int main() { Vector v(10); // statements using v } (^1) There are n elements in an n-dimensional vector.

3. Given that the statements below compile:

someType array[3] = { b<1>, b<2>, b<3> };

arrayi;

What can you say about someType and b?

Consider the second line. Assuming that i is defined – the statement would

not compile if it was not – array[i] must be a function pointer.

The parentheses after the array item gives away the fact that this must be an

array of function pointers. Arrayi is an invocation of the ith pointer.

Also, the syntax b<1> can only belong to a template function pointer.

Therefore, someType must be the type of a function pointer, taking no

arguments, and b must be a template function pointer. We know that the

functions pointed to must take no arguments, but we have no way to tell,

from the statements above, what they return, if anything.

Here is some code that implements the statements, given above.

#include

template

int b(){ return 2*i; }

int main()

typedef int(*funPtr)();

funPtr fp[3] = { b<1>, b<2>, b<3> };

std::cout << "\n ";

for(int i=0; i<3; ++i)

std::cout << (fpi);

std::cout << "\n\n";

return 0;

4. How can you access some specific object anywhere in your program without

passing it into every function as a parameter and without making it a global

object? Write a class that supports this behavior and prevents access to the

object by another object unless the accessing object is registered with the

class you are implementing. This should work for any type of shared object.

//---- start of required answer -------------------- template class shared { public: static T& value(R &r) { pR = &r); template void register(R &r); private: static T _value; R* pR; }; template T shared::_value; template T& shared::value(R &r) { if(pR != &r) { // if access is not allowed you can throw an exception // or return a static dummy value of type T } return _value; } //---- end of required answer ------------------------- #include using namespace std; void someFunction() { shared sd; // use the shared value cout << "\n getting shared value = " << sd.value(); } int main() { shared sd; cout << "\n setting shared value to " << (sd.value() = 2.5); someFunction(); cout << "\n\n"; }

6. Given the definintions below, what is the result of executing main?

class A { str say() { return "class A"; } }; class B { public: virtual ~B() {}; str say() { return typeid(*this).name(); } }; class D : public B { public: A a; }; void fun1(B b[], int size) { for(int i=0; i

the second D object, and we get a memory access violation.

8. Write a class with a constructor that accepts a string reference and provides

two functions, void up() and void down(), that result in the original string

being converted to upper case or lower case, respectively.

#include #include typedef std::string str; //---- start of required answer ---------------------- class caser { public: caser(str &change) : _cased(change) {} void up() { for(int i=0; i<_cased.length(); ++i) _cased[i] = toupper(_cased[i]); } void down() { for(int i=0; i<_cased.length(); ++i) _cased[i] = tolower(_cased[i]); } private: str& _cased; }; //---- end of required answer -------------------------- int main() { str change = "This Will Be Changed To All One Case"; std::cout << "\n " << change; std::cout << "\n " << (caser(change).up(), change); std::cout << "\n " << (caser(change).down(), change) << "\n\n"; }

9. How can reusable classes help clients avoid creating compilation

dependencies with them?

They must provide an interface, e.g., an abstract base class containing no

data members and having all pure virtual members that are overridden by the

implementing class to provide the needed functionality. This interface must

have its own header file^3.

They must provide a static member function that clients can call to create an

instance of the class or some form of class factory. Since an instance of the

reusable class is created by the class and not the client, the client does not

need to include the header file for the implementation, but only the header

file for the interface, and consequently has no compilation dependency on the

reusable class.

(^3) This is the one case where a header file without a corresponding implementation file is an acceptable structure for a modular system.