Docsity

CSE687 Midterm 3 Solutions: Object Oriented Design - Prof. James Fawcett, Exams of Engineering

Toggle sidebar
bg1
CSE687 Object Oriented Design Midterm #3 Spring 2006
Instructor’s Solution for CSE687 Midterm #3
Jim Fawcett
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 Solutions: Object Oriented Design - Prof. James Fawcett and more Exams Engineering in PDF only on Docsity!

Instructor’s Solution for CSE687 Midterm # Jim Fawcett 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. Describe function overloading. Be as specific and complete as you can. When does overload binding occur? Are there any issues with respect to overloading that you need to remember when designing a class? Answer: Function overloading is the process of defining two or more functions in the same scope with the same identifier, distinguished by the sequence of the functions’ parameter types. Note that two functions that differ only in return type do not qualify as an overload, and is a compile-time error. You should not overload virtual functions of a class. That causes hiding of all of the overloads in a class that derives from it, except for any overridden functions, violating the Liskov Substitution Principle.
  1. Given the partial descriptions: class D : public B { … }; B* f(const D& d) { return new D(d); } D d; B b = *f(d); where the ellipsis, … , represents code not disclosed to you, under what conditions will this code successfully compile? If it does compile, describe each of the operations that occur when the last statement is executed. Answer: For the statement D d; to compile, both D and B must have publicly accessible constructors that can be called with no arguments – either default constructors or constructors with all parameters provided with default values. If no destructors are defined for either of the classes the compiler will generate them. Otherwise, compilation fails. The statement B b = *f(d); will compile, provided that both D and B have publicly accessible copy constructors. If none are defined for either of the classes, then the compiler will generate them. Note that no copy is created to pass d by reference in the function’s argument, but is used to create a new instance on the C++ heap with a copy of the passed object’s state. The function f(d) returns the address of the D instance, created on the heap, statically typed as a base pointer. The statement dereferences this pointer to get a derived class object (Liskov Substitution) and uses that instance to copy construct the base instance b. Note that this uses the B copy constructor, slicing the D object to a B.
  1. State the Dependency Inversion Principle. Why is the use of object factories required in systems that wish to implement this principle with the C++ programming language? Answer: Dependency Inversion Principle states: a. High level components should not depend upon low level components. Instead, both should depend on abstractions. b. Abstractions should not depend upon details. Details should depend upon the abstractions. The Dependency Inversion Principle is supported by inserting interfaces between major layers of a program, and providing object factories to allow code to create objects in a layer on which the code depends. Each of the interfaces is a contract for service, provided by the interface implementer. Object factories are required to avoid coupling clients to the concrete instances they need in program layers on which the clients depend. With factories, the clients include headers with the interface and factory declarations, which need have no concrete binding to specific class instances, as we have seen in examples handed out in class. Clients specify, to the factory, the object they wish to use with some form of key that will not change if the requested class changes or new classes are added to the factory. Even in this later case, the factory header does not change, so the client does not need to change.
  1. Draw a UML class diagram that represents your design for Project #4c^1. You do not need to show the methods and fields, just the classes and their relationships. Describe all the types of class relationships illustrated by the diagram. Answer: The relationships are: Composition is a strong ownership relationship, implying that the life- times of the owner and owned are the same. C++ accomplishes this by making the owned a data member of the owner. Inheritance is an “is-a” relationship. The SocketComm class inherits from std::iostream to be substitution compatible with any iostream. The design might separate the socket communication into an input class and an output class that derive form std::istream and std::ostream, respectively. The DirectorySynch class “uses” the Message class to form directory information as XML messages and files as XML messages with Base encoded contents. Using relationships imply no ownership. An instance of the message class will be created by the server and passed to its DirectorySynch object for processing. (^1) The statement for Project #4c is appended to this exam packet. Client DirectorySynch Server SocketComm UI Form Message Base 64 Convert XMLparser std::iostream uses composition inheritance
  1. Do you see any problem(s) with the code below? Please discuss. class B { public: B& operator=(const B& b); … private: std::string& s; }; Write an assignment operator for the class B, making, at most, one change to the class, if you think that is necessary. Answer: The reference member of B cannot be assigned. That is not allowed in the C++ language. We either must keep the same reference instead of trying to assign, or we can use an instance of the string, rather than a reference, that will allow assigning to the target string, or, as illustrated below, we can keep a pointer, rather than a reference to the shared string. The problem is this: the reference member implies non- ownership – the string was created elsewhere – so the owner can destroy the string at any time, leaving a dangling reference in the instance of this class. //////////////////////////////////////////// // class that shares a pointer to string // also could have lifetime problems class B { public: B2(std::string& str) : pS(&str) {} B2(const B2& b); B2& operator=(const B2& b); void say() { std::cout << "\n " << pS->c_str(); } private: std::string* pS; }; B2::B2(const B2& b) : pS(b.pS) {} B2& B2::operator=(const B2& b) { if(this == &b) return *this; if(pS != b.pS) { // delete pS; do this if you know pS points to heap pS = b.pS; } return *this; }
  1. What happens when the following code is executed? Please give as complete an answer as you can. delete this; Answer: The destructor of the instance “this” points to will be called. If “this” points to the C++ heap, then the object is gracefully de-allocated. If, however, “this” points to an instance allocated on some stack frame, then delete this is a memory access violation, and a Win32 structured exception will be thrown. The statement: delete this; is used in reference counted object implementations where the object knows it will be created on the heap by a smart pointer.