You are on page 1of 7

24

Other Topics
What's in a name? that
which we call a rose
By any other name would
smell as sweet.
William Shakespeare

O Diamond! Diamond!
thou little knowest the
mischief done!

OBJECTIVES
In this chapter you will learn:

Sir Isaac Newton

To use const_cast to temporarily treat a const object


as a non-const object.

To use namespaces.

To use operator keywords.

To use mutable members in const objects.

To use class-member pointer operators .* and ->*.

To use multiple inheritance.

The role of virtual base classes in multiple


inheritance.

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 24 Other Topics

Instructors Manual
Self-Review Exercises
24.1

Fill in the blanks for each of the following:


a) The
operator qualifies a member with its namespace.
ANS: binary scope resolution (::).

b) The

ANS: const_cast.

operator allows an objects const-ness to be cast away.

c) Because an unnamed namespace has an implicit using directive, its members appear to
occupy the
, are accessible directly and do not have to be qualified with a
namespace name.
ANS: global namespace.
d) Operator
is the operator keyword for inequality.
ANS: not_eq.
e) A class may be derived from more than one base class; such derivation is called
.
ANS: multiple inheritance.
f) When a base class is inherited as
, only one subobject of the base class will
appear in the derived class.
ANS: virtual.
24.2

State which of the following are true and which are false. If a statement is false, explain why.
a) When passing a non-const argument to a const function, the const_cast operator
should be used to cast away the const-ness of the function.
ANS: False. It is legal to pass a non-const argument to a const function. However, when
passing a const reference or pointer to a non-const function, the const_cast operator should be used to cast away the const-ness of the reference or pointer.
b) namespaces are guaranteed to be unique.
ANS: False. Programmers might inadvertently choose the namespace already in use.
c) Like class bodies, namespace bodies also end in semicolons.
ANS: False. namespace bodies do not end in semicolons.
d) namespaces cannot have namespaces as members.
ANS: False. namespaces can be nested.
e) A mutable data member cannot be modified in a const member function.
ANS: False. A mutable data member is always modifiable, even in a const member function.

Exercises
24.3

Fill in the blanks for each of the following:


a) Keyword
specifies that a namespace or namespace member is being used.
ANS: using

b) Operator
ANS: or

c) Storage specifier
ANS: mutable

d) The

ANS: volatile.

is the operator keyword for logical OR.


allows a member of a const object to be modified.
qualifier specifies that an object can be modified by other programs.

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Exercises

e) Precede a member with its


name and the scope resolution operator
if the possibility exists of a scoping conflict.
ANS: namespace, ::.
f) The body of a namespace is delimited by
.
ANS: braces ({}).
g) For a const object with no
data members, operator
must be
used every time a member is to be modified.
ANS: mutable, const_cast.
24.4

Write a namespace, Currency, that defines constant members ONE, TWO, FIVE, TEN, TWENTY,
and HUNDRED. Write two short programs that use Currency. One program should make all
constants available and the other should make only FIVE available.

FIFTY

ANS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Exercise 24.4 Part A Solution: Ex24_04.cpp


// Program makes namespace members accessible.
#include <iostream>
using std::cout;
using std::endl;
// create namespace Currency
namespace Currency
{
enum Money { ONE = 1, TWO, FIVE = 5, TEN = 10,
TWENTY = 20, FIFTY = 50, HUNDRED = 100 };
} // end namespace Currency
int main()
{
using namespace Currency; // use Currency namespace
cout << "TWO's value is: " << TWO
<< "\nTEN's value is: " << TEN << endl;
return 0;
} // end main

TWO's value is: 2


TEN's value is: 10

1
2
3
4
5
6
7
8
9
10
11
12
13

// Exercise 24.4 Part B Solution: Ex24_04.cpp


// Program makes one namespace member accessible.
#include <iostream>
using std::cout;
using std::endl;
// create namespace Currency
namespace Currency
{
enum Money { ONE = 1, TWO, FIVE = 5, TEN = 10,
TWENTY = 20, FIFTY = 50, HUNDRED = 100 };
} // end namespace Currency

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

4
14
15
16
17
18
19
20

Chapter 24 Other Topics

int main()
{
using Currency::FIVE; // use Currency FIVE
cout << "FIVE's value is: " << FIVE << endl;
return 0;
} // end main

FIVE's value is: 5

24.5 Given the namespaces in Fig. 24.15, determine whether each statement is true or false. Explain any false answers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

namespace CountryInformation
{
using namespace std;
enum Countries { POLAND, SWITZERLAND, GERMANY,
AUSTRIA, CZECH_REPUBLIC };
int kilometers;
string string1;
namespace RegionalInformation
{
short getPopulation(); // assume definition exists
MapData map; // assume definition exists
} // end RegionalInformation
} // end CountryInformation
namespace Data
{
using namespace CountryInformation::RegionalInformation;
void *function( void *, int );
} // end Data

Fig. 24.15 |

namespaces

for Exercise 24.5.

a) Variable kilometers is visible within namespace Data.


ANS: False. Variable kilometers is visible only within namespace CountryInformation.
b) Object string1 is visible within namespace Data.
ANS: False. Variable string1 is visible only within namespace CountryInformation.
c) Constant POLAND is not visible within namespace Data.
ANS: True.
d) Constant GERMANY is visible within namespace Data.
ANS: False. Constant GERMANY is visible only within namespace CountryInformation.
e) Function function is visible to namespace Data.
ANS: True.
f) Namespace Data is visible to namespace CountryInformation.
ANS: False. Data and CountryInformation are two separate namespaces.
g) Object map is visible to namespace CountryInformation.
ANS: False. It is a member of a nested namespace.

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Exercises

h) Object string1 is visible within namespace RegionalInformation.


ANS: False. Object string1 is visible within namespace CountryInformation::RegionalInformation.
24.6 Compare and contrast mutable and const_cast. Give at least one example of when one
might be preferred over the other. [Note: This exercise does not require any code to be written.]
ANS: Operator const_cast is used to cast away const or volatile qualifications. Users of
a class usually will not be aware of const_cast operations, because this implementation is typically hidden. Storage-class specifier mutable allows a variable to be modified even if the object is const. Users of the class are not likely to be aware of a
mutable member. Members that are mutable usually correspond to some secret implementation. mutable members are always modifiable, whereas const_cast operations are confined to the line where the cast is performed.
24.7 Write a program that uses const_cast to modify a const variable. [Hint: Use a pointer in
your solution to point to the const identifier.]
ANS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// Exercise 24.7 Solution: Ex24_07.cpp


#include <iostream>
using std::cout;
using std::endl;
int main()
{
const char c = 'A';
const char *ptr = &c;
cout << "c is " << *ptr;
*const_cast< char * > ( ptr ) = 'Z';
cout << "\nc is " << *ptr << endl;
return 0;
} // end main

c is A
c is Z

24.8

What problem do virtual base classes solve?

ANS: virtual base classes solve the problem of diamond inheritance, where a derived

class receives duplicate subobjects from its base classes. With virtual base classes,
only one copy of the subobject is inherited into the derived class (at the bottom of
the diamond).

24.9 Write a program that uses virtual base classes. The class at the top of the hierarchy should
provide a constructor that takes at least one argument (i.e., do not provide a default constructor).
What challenges does this present for the inheritance hierarchy?
ANS: If a virtual base class provides a constructor that requires arguments, the most derived class must explicitly invoke the virtual base classs constructor to initialize the
members inherited from the virtual base class. Implementing hierarchies with virtual base classes is simpler if default constructors are used for the base classes.

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

Chapter 24 Other Topics

// Exercise 24.9 Solution: Ex24_09.cpp


#include <iostream>
using std::cout;
using std::endl;
// class Base definition
class Base
{
public:
Base( int n )
{
num = n;
} // end Base constructor
void print()
{
cout << num;
} // end function print
private:
int num;
}; // end class Base
// class D1 definition
class D1 : virtual public Base
{
public:
D1(): Base( 3 ) {} // D1 constructor
}; // end class D1
// class D2 definition
class D2 : virtual public Base
{
public:
D2(): Base( 5 ) {} // D2 constructor
}; // end class D2
// class Multi definition
class Multi : public D1, D2
{
public:
Multi( int a ): Base( a ) {} // Multi constructor
}; // end class Multi
int main()
{
Multi m( 9 );
m.print();
cout << endl;
return 0;
} // end main

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Exercises

24.10 Find the error(s) in each of the following. When possible, explain how to correct each error.
a) namespace Name {
int x;
int y;
mutable int z;

};
ANS: A mutable member can belong only to a class.

b)

int integer = const_cast< int >( double );

ANS: Operator const_cast cannot be used to convert a float to an int. Either


static_cast or reinterpret_cast should be used. [Note: The keyword double in pa-

c)

rentheses should be a floating-point value.]

namespace PCM( 111, "hello" );

// construct namespace

ANS: A namespace cannot be constructed, because it only defines a scope.

2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

You might also like