What is a virtual member?
A virtual member in a base class expects its derived class define its own version. In particular base classes ordinarily should define a virtual destructor, even if it does no work.
How does the protected access specifier differ from private?
- private member: base class itself and friend can access
- protected members: base class itself, friend and derived classes can access
Define your own versions of the
Quote
class and theprint_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;
- (a): incorrect, derive from itself.
- (b): incorrect, this is a definition not a declaration.
- (c): incorrect, A derived class is declared like any other class. The declaration contains the class name but does not include its derivation list.
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 bothQuote
andBulk_quote
objects o that function.
Define static type and dynamic type.
The static type of an expression is always known at compile time.
The dynamic type is the type of the object in memory that the variable or expression represents. The dynamic type may not be known until run time.
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.
The static type of a pointer or reference to a base class may differ from its dynamic type. Anything like this can be an example. Exercise 15.9
Recalling the discussion from §8.1 (p. 311), explain how the program on page 317 that passed an
ifstream
to theSales_data
read function works.
The function takes a std::istream
from which std::ifstream
is derived. Hence the ifstream
object "is a" istream
, which is why it works.
Add a virtual debug function to your
Quote
class hierarchy that displays the data members of the respective classes.
void Quote::debug() const
{
std::cout << "data members of this class:\n"
<< "bookNo= " <<this->bookNo << " "
<< "price= " <<this->price<< " ";
}
Is it ever useful to declare a member function as both override and final? Why or why not?
Sure. override means overriding the same name virtual function in base class. final means preventing any overriding this virtual function by any derived classes that are more lower at the hierarchy.
Define your own versions of
Disc_quote
andBulk_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.
error: cannot declare variable 'd' to be of abstract type 'Disc_quote': Disc_quote d;
note: because the following virtual functions are pure within 'Disc_quote': class Disc_quote : public Quote
note: virtual double Disc_quote::net_price(std::size_t) const: virtual double net_price(std::size_t n) const override = 0;
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
- Base *p = &d1; legal
- p = &d2; illegal
- p = &d3; illegal
- p = &dd1; legal
- p = &dd2; illegal
- p = &dd3; illegal
User code may use the derived-to-base conversion only if D inherits publicly from B. User code may not use the conversion if D inherits from B using either protected or private.
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.
Member functions and friends of D can use the conversion to B regardless of how D inherits from B. The derived-to-base conversion to a direct base class is always accessible to members and friends of a derived class.
Hence, the 3 below are all legal:
- Pub_Derv
- Priv_Derv
- Prot_Derv
Member functions and friends of classes derived from D may use the derived-to-base conversion if D inherits from B using either public or protected. Such code may not use the conversion if D inherits privately from B.Hence:
- Derived_from_Public legal
- Derived_from_Private illegal
- Derived_from_Protected legal
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)
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 thatfcn
matched the definition in Base, how would the calls in that section be resolved?
remove the parameter int.
What kinds of classes need a virtual destructor? What operations must a virtual destructor perform?
Generally, a base class should define a virtual destructor.
The destructor needs to be virtual to allow objects in the inheritance hierarchy to be dynamically allocated and destroyed.
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?
Without it, when building the statement below, the compiler would complain that:
note: 'Bulk_quote::Bulk_quote()' is implicitly deleted because the default definition would be ill-formed.
The reason is that a constructor taking 4 parameters has been defined, which prevented the compiler generate synthesized version default constructor.
As a result, the default constructor of any class derived from it has been defined as deleted. Thus the default constructor must be defined explicitly so that the derived classes can call it when executing its default constructor.
Define the
Quote
andBulk_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.
rules:
- only inherit from the direct base class.
- default, copy and move constructors can not inherit.
- any data members of its own are default initialized.
- the rest details are in the section section 15.7.4.
Repeat your program, but this time store
shared_ptrs
to objects of typeQuote
. 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.
Since the vector from the previous exercise holds objects, there's no polymorphism happened while calling the virtual function net_price. Essentially, the objects held in it are the Quote subjects of the Bulk_quote objects being pushed back, Thus, the virtual net_price functions called are Quote::net_price. As a result, no discount was applied. The outcome was 9090.
The objects held for this exercise are smart pointers to the Quote objects.In this case, polymorphism happened as expected.The actual virtual functions being called are Bulk_quote::net_price that ensure discount is applied.Thus, the outcome is 6363. It can be found that 30% discount has been applied to the price calculation.
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.
Basket h | Basket cpp | main
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)));
- (a)
OrQuery, AndQuery, NotQuery, WordQuery
- (b) the same as the previous one
- (c)
OrQuery, AndQuery, WordQuery
What happens when an object of type Query is copied, moved, assigned, and destroyed?
-
copy: While being copied, the synthesized copy constructor is called. It copies the data member into the new object. Since in this case, the data member is a shared pointer, while copying, the corresponding shared pointer points to the same address and the use count from the both shared pointer becomes 2.
-
move: while being moved, the synthesized move constructor is called. It moves the data member into the new object. In this case, the shared pointer from the newly created object will point to the address to which the original shared pointer pointed. After the move operation, the use count of the shared pointer in the new object is 1, whereas the pointer from the original object becomes
nullptr
. -
copy assignment: The synthesized copy assignment will be called. The outcome of this operation is identical with the copy operation.
-
move assignment: The synthesized move assignment will be called. The rest is the same as the move operation.
-
destroy: The synthesized destructor will be called. It will call the destructor of
shared_ptr
which decrements the use count. If the count becomes zero, the destructor from shared_ptr will delete the resources it point to.
What about objects of type
Query_base
?
Managed by the synthesized version. Since Query_base a abstract class, the object of this type is essentially a subobject of its derived class.
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 fromq.eval()
.
- a: Query q = Query("fiery") & Query("bird") | Query("wind");
Query::Query(const std::string& s)
where s == "fiery","bird" and "wind"WordQuery::WordQuery(const std::string& s)
where s == "fiery","bird" and "wind"AndQuery::AndQuery(const Query& left, const Query& right);
BinaryQuery(const Query&l, const Query& r, std::string s);
Query::Query(std::shared_ptr<Query_base> query)
2timesOrQuery::OrQuery(const Query& left, const Query& right);
BinaryQuery(const Query&l, const Query& r, std::string s);
Query::Query(std::shared_ptr<Query_base> query)
2times
- b:
query.rep()
inside the operator <<().q->rep()
inside the member function rep().OrQuery::rep()
which is inherited fromBinaryQuery
.Query::rep()
forlhs
andrhs
: forrhs
which is aWordQuery
:WordQuery::rep()
wherequery_word("wind")
is returned.Forlhs
which is anAndQuery
.AndQuery::rep()
which is inherited fromBinaryQuery
.BinaryQuer::rep()
: forrhs: WordQuery::rep()
where query_word("fiery") is returned. Forlhs: WordQuery::rep()
where query_word("bird" ) is returned.
- c:
q.eval()
q->rep()
: where q is a pointer toOrQuary
.QueryResult eval(const TextQuery& )const override
: is called but this one has not been defined yet.
Implement the
Query
andQuery_base classes
, including a definition of rep but omitting the definition ofeval
.
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.
Query q = Query("fiery") & Query("bird") | Query("wind");
WordQuery::WordQuery(wind)
Query::Query(const std::string& s) where s=wind
WordQuery::WordQuery(bird)
Query::Query(const std::string& s) where s=bird
WordQuery::WordQuery(fiery)
Query::Query(const std::string& s) where s=fiery
BinaryQuery::BinaryQuery() where s=&
AndQuery::AndQuery()
Query::Query(std::shared_ptr<Query_base> query)
BinaryQuery::BinaryQuery() where s=|
OrQuery::OrQuery
Query::Query(std::shared_ptr<Query_base> query)
Press <RETURN> to close this window...
std::cout << q <<std::endl;
Query::rep()
BinaryQuery::rep()
Query::rep()
WodQuery::rep()
Query::rep()
BinaryQuery::rep()
Query::rep()
WodQuery::rep()
Query::rep()
WodQuery::rep()
((fiery & bird) | wind)
Press <RETURN> to close this window...
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");
- Illegal. Because
BinaryQuery
is an abstract class. - Illegal. Because operator & returns a
Query
which can not convert to anAndQuery
object. - Illegal. Because operator & returns a
Query
which can not convert to anOrQuery
object.
Implement the
Query
andQuery_base
classes. Test your application by evaluating and printing a query such as the one in Figure 15.3 (p. 638).
Query | Query_base | main
In the
OrQuery
eval function what would happen if itsrhs
member returned an empty set? What if itslhs
member did so? What if bothrhs
andlhs
returned empty sets?
Nothing special will happen. The codes as following:
std::shared_ptr<std::set<line_no>> ret_lines =
std::make_shared<std::set<line_no>>(left.begin(), left.end());
Since std::make_shared
will allocate dynamically a new std::set
, nothing will be added into this std::set
if any set is empty.The codes in main function proves this.
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.