What is a virtual member?
A member function should be dynamically bound by preceding its declaration.
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 fromprivate
?
base class members:
private:
: base class itself and friends can accessprotected
: base class itself, friends 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 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.
-
static type: Type with which a variable is defined or that an expression yields. Static type is known at compile time.
-
dynamic type: Type of an object at run time. The dynamic type of an object to which a reference refers or to which a pointer points may differ from the static type of the reference or pointer.
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.
A pointer or reference to a base-class type can refer to an to object of derived type. In such cases the static type is reference (or pointer) to base, but the dynamic type is reference (or pointer) to derived.
Anything like this can be an example.
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 istream
from which ifstream
is derived. Hence the ifstream
object "is a" istream
,which is why it works.
Add a virtual
debug
function to yourQuote
class hierarchy that displays the data members of the respective classes.
Quote | Bulk_quote | Limit_quote
Is it ever useful to declare a member function as both
override
andfinal
? 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. (@Mooophy)
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?
The print
in derived::print
wanted to call the print
from the base class. However, the class scope base::
was omitted. As a result, it will cause an infinite recursion.
Fixed:
void print(ostream &os) { base::print(os); os << " " << i; }
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(); // base::print()
(b) dobj.print(); // derived::print()
(c) bp1->name(); // base::name()
(d) bp2->name(); // derived::name()
(e) br1.print(); // base::print()
(f) br2.print(); // derived::print()
e and f are called at run time.
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.
note: because the following virtual functions are pure within 'ch15::EX15::Disc_quote':
virtual double net_price(size_t) const = 0;
^