



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




V IR
G IN
IA
PO
LYTECHNIC IN ST IT U T E A N D
ST A (^) TE UNIVE
RS
IT
Y U TPROSIM
Instructions:
Print your name in the space provided below. Answer each question in the space provided. If you want partial credit, justify your answers briefly and concisely, even when justification is not explicitly required. There are 11 questions, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this examination with any student who has not taken it. You must return this test form when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code violation.
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
For the next two questions, consider the following class, which represents a rational number (ratio of two integers like 1/2):
class Rational { friend istream& operator>>(istream& In, Rational& toPrint); friend ostream& operator<<(ostream& Out, const Rational& toPrint); private: int Top, Bottom; public: Rational(); Rational(int T, int B); bool operator==(const Rational& RHS) const; bool operator<(const Rational& RHS) const; bool operator>(const Rational& RHS) const; Rational operator+(const Rational& RHS) const; Rational operator+(int RHS) const; Rational operator-(const Rational& RHS) const; Rational operator-(int RHS) const; Rational operator(const Rational& RHS) const; Rational operator(int RHS) const; Rational operator/(const Rational& RHS) const; Rational operator/(int RHS) const; };
Rational R1(1,1), R2(2,1), R3(3,1), R12(1,2), R13(1,3), R23(2,3);
Rational SumR, SubR, ProdR, QuotR;
SumR = R1 + R2 + R3 + R12;
SubR = 3 * R2 – R23;
ProdR = R3 * R13 + 4;
QuotR = R12 / Rational(2, 5);
Rational operator(int LHS, const Rational& RHS) {*
Return (RHS * LHS); // there are harder ways to do this }
Neither template makes use of private data or function members. That will make it easier for derived types to carry out necessary operations on the node and/or queue data.
It would be even better (perhaps) if the node and queue provided support for polymorphism by having some virtual functions.
The data element is secured by a pointer, rather than stored physically. That allows the client to store base-type pointers in the queue/node structure, which is necessary in order to support polymorphic data objects.
If all objects of that type share the value of an attribute, it is inefficient for each one to store an individual copy of that attribute. Aside from the storage space, if each object stores an independent copy there is no easy, automatic way to modify all of those at once.
Both of these objections can be overcome if the attribute is declared as a static member of the class, and a static mutator is also provided.
class A { protected: string X;
public: A(const string& iX) {X = iX;} string getX() const {return X;} void setX(const string& iX) {X = iX;} };
class B : public A { public: B(const string& iX) : A(iX) {} };
Aside from abandoning the use of inheritance, describe one way the designer could deal with this problem, and describe any shortcomings of your solution.
The class B could override the inherited function A::setX(). The question is what should the overriding function B::setX() do, and what should its access control be?
The implementation of B::setX() must do no harm; the obvious solution is for the implementation to do nothing at all. However, that is somewhat dishonest since the presence of the function would normally imply to a client that it has an obvious effect.
If B::setX() is public, then the complaint above applies. However, if B::setX() is declared as either protected or private then client code will not be able to access it at all, and the complaint is removed.
It would probably be best to make B::setX() private, since that would also hide its functionality (or lack thereof) from types derived from B as well as from client code.
A template implementation will contain references to its type parameter(s), which are just dummy names. Since those names are used as type names, the compiler will not recognize them as valid and will generate errors.
(b) [6 points] Explain briefly, in general terms, how the fact stated in (a) is dealt with (since we can, in fact, write useful template implementations and incorporate them in our programs).
The compiler never sees the template implementation. When a declaration using the template is encountered, the actual type(s) used in that declaration are substituted throughout the template implementation (and other necessary changes are made), resulting in the creation of a new class, which will be compilable.
and that the other standard arithmetic operations are not (usually) defined.
(a) [8 points] Assuming that the Rational class presented earlier has been implemented, consider deriving a class Farey from Rational. Are there any particular reasons that would be difficult or clumsy?
The Rational class has overloaded a rather large number of arithmetic operators, only one of which is appropriate for Farey numbers. While the inappropriate inherited operators could be overridden and rendered harmless, or even inaccessible, that is a clumsy approach to the goal of representing Farey numbers.
(b) [6 points] Would those reasons still apply if the hierarchy were reversed. That is, what if a complete Farey class were implemented first, and then a Rational class were derived from it? Explain.
Well, this certainly would eliminate the objection to unsuitable inherited members.
The only real objection to deriving Rational from Farey is that so little is gained by doing so. The non-member operators are not inherited anyway. The only member operator of Farey would be overridden in Rational.
An inheritance hierarchy, virtual functions, and accessing derived type objects via base type pointers.