What is a virtual member?
How does the protected access specifier differ from private?
Define your own versions of the Quote class and the print_total function.
Which of the following declarations, if any, are incorrect? Explain why.
class Base { ... };
(a) class Derived : public Derived { ... };
(b) class Derived : private Base { ... };
(c) class Derived : public Base;
Define your own version of the Bulk_quote class.
Test your print_total function from the exercises in § 15.2.1 (p. 595) by passing both Quote and Bulk_quote objects o that function.
Define a class that implements a limited discount strategy, which applies a discount to books purchased up to a given limit. If the number of copies exceeds that limit, the normal price applies to those purchased beyond the limit.
Define static type and dynamic type.
When is it possible for an expression’s static type to differ from its dynamic type? Give three examples in which the static and dynamic type differ.
Recalling the discussion from § 8.1 (p. 311), explain how the program on page 317 that passed an ifstream to the Sales_data read function works.
Add a virtual debug function to your Quote class hierarchy that displays the data members of the respective classes.
Is it ever useful to declare a member function as both override and final? Why or why not?
Given the following classes, explain each print function:
class base {
public:
string name() { return basename; }
virtual void print(ostream &os) { os << basename; }
private:
string basename;
};
class derived : public base {
public:
void print(ostream &os) { print(os); os << " " << i; }
private:
int i;
};
If there is a problem in this code, how would you fix it?
Given the classes from the previous exercise and the following objects, determine which function is called at run time:
base bobj;
base *bp1 = &bobj;
base &br1 = bobj;
derived dobj;
base *bp2 = &dobj;
base &br2 = dobj;
(a) bobj.print();
(b) dobj.print();
(c) bp1->name();
(d) bp2->name();
(e) br1.print();
(f) br2.print();
Define your own versions of Disk_quote and Bulk_quote.
Rewrite the class representing a limited discount strategy, which you wrote for the exercises in § 15.2.2 (p. 601), to inherit from Disc_quote.
Try to define an object of type Disc_quote and see what errors you get from the compiler.
Given the classes from page 612 and page 613, and assuming each object has the type specified in the comments, determine which of these assignments are legal. Explain why those that are illegal aren’t allowed:
Base *p = &d1; // d1 has type Pub_Derv
p = &d2; // d2 has type Priv_Derv
p = &d3; // d3 has type Prot_Derv
p = &dd1; // dd1 has type Derived_from_Public
p = &dd2; // dd2 has type Derived_from_Private
p = &dd3; // dd3 has type Derived_from_Protected
Assume that each of the classes from page 612 and page 613 has a member function of the form:
void memfcn(Base &b) { b = *this; }
For each class, determine whether this function would be legal.
Write code to test your answers to the previous two exercises.
Choose one of the following general abstractions containing a family of types (or choose one of your own). Organize the types into an inheritance hierarchy: (a) Graphical file formats (such as gif, tiff, jpeg, bmp) (b) Geometric primitives (such as box, circle, sphere, cone) (c) C++ language types (such as class, function, member function)
For the class you chose in the previous exercise, identify some of the likely virtual functions as well as public and protected members.
Assuming class D1 on page 620 had intended to override its inherited fcn function, how would you fix that class? Assuming you fixed the class so that fcn matched the definition in Base, how would the calls in that section be resolved?
What kinds of classes need a virtual destructor? What operations must a virtual destructor perform?
Why did we define a default constructor for Disc_quote? What effect, if any, would removing that constructor have on the behavior of Bulk_quote?
Define the Quote and Bulk_quote copy-control members to do the same job as the synthesized versions. Give them and the other constructors print statements that identify which function is running. Write programs using these classes and predict what objects will be created and destroyed. Compare your predictions with the output and continue experimenting until your predictions are reliably correct.
Redefine your Bulk_quote class to inherit its constructors.
Define a vector to hold Quote objects but put Bulk_quote objects into that vector. Compute the total net_price of all the elements in the vector.
Repeat your program, but this time store shared_ptrs to objects of type Quote. Explain any discrepancy in the sum generated by the this version and the previous program. If there is no discrepancy, explain why there isn’t one.
Write your own version of the Basket class and use it to compute prices for the same transactions as you used in the previous exercises.
Given that s1, s2, s3, and s4 are all strings, determine what objects are created in the following expressions:
(a) Query(s1) | Query(s2) & ~ Query(s3);
(b) Query(s1) | (Query(s2) & ~ Query(s3));
(c) (Query(s1) & (Query(s2)) | (Query(s3) & Query(s4)));
What happens when an object of type Query is copied, moved, assigned, and destroyed?
What about objects of type Query_base?
For the expression built in Figure 15.3 (p. 638):
(a) List the constructors executed in processing that expression.
(b) List the calls to rep that are made from cout << q.
(c) List the calls to eval made from q.eval().
Implement the Query and Query_base classes, including a definition of rep but omitting the definition of eval.
Put print statements in the constructors and rep members and run your code to check your answers to (a) and (b) from the first exercise.
What changes would your classes need if the derived classes had members of type shared_ptr<Query_base> rather than of type Query?
Are the following declarations legal? If not, why not? If so, explain what the declarations mean.
BinaryQuery a = Query("fiery") & Query("bird");
AndQuery b = Query("fiery") & Query("bird");
OrQuery c = Query("fiery") & Query("bird");
Implement the Query and Query_base classes. Test your application by evaluating and printing a query such as the one in Figure 15.3 (p. 638).
In the OrQuery eval function what would happen if its rhs member returned an empty set? What if its lhs member did so? What if both rhs and lhs returned empty sets?
Reimplement your classes to use built-in pointers to Query_base rather than shared_ptrs. Remember that your classes will no longer be able to use the synthesized copy-control members.
Design and implement one of the following enhancements:
(a) Print words only once per sentence rather than once per line.
(b) Introduce a history system in which the user can refer to a previous query by number, possibly adding to it or combining it with another.
(c) Allow the user to limit the results so that only matches in a given range of lines are displayed.