Professional Documents
Culture Documents
Outline
22.1
22.2
22.3
22.4
22.5
22.6
22.7
22.8
22.9
22.10
Introduction
const_cast Operator
reinterpret_cast Operator
namespaces
Operator Keywords
explicit Constructors
mutable Class Members
Pointers to Class Members (.* and ->*)
Multiple Inheritance
Multiple Inheritance and virtual Base Classes
22.1 Introduction
Consider additional C++ features
Cast operators
Namespaces
Operator keywords
Multiple inheritence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig22_01.cpp
(1 of 2)
using std::cout;
using std::endl;
// class ConstCastTest definition
class ConstCastTest {
public:
void setNumber( int );
int getNumber() const;
void printNumber() const;
private:
int number;
}; // end class ConstCastTest
// set number
void ConstCastTest::setNumber( int num ) { number = num; }
// return number
int ConstCastTest::getNumber() const { return number; }
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// output number
void ConstCastTest::printNumber() const
{
cout << "\nNumber after modification: ";
// cast away const-ness to allow modification
const_cast< ConstCastTest * >( this )->number--;
Outline
Cast away the const-ness
fig22_01.cpp
the this pointer. This allows
(2 of 2)
the data to be modified.
fig22_01.cpp
output (1 of 1)
// end printNumber
int main()
{
ConstCastTest test;
test.setNumber( 8 );
// end main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Outline
fig22_02.cpp
(1 of 1)
using std::cout;
using std::endl;
int main()
{
int x = 120;
int *ptr = &x;
fig22_02.cpp
output (1 of 1)
// end main
22.4 namespaces
Program has identifiers in different scopes
Sometimes scopes overlap, lead to problems
22.4 namespaces
using statement
using namespace namespace_name;
Members of that namespace can be used without preceding
namespace_name::
Can also be used with individual member
Examples
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
// use std
Outline
fig22_03.cpp
(1 of 3)
// global variable
// prototypeNote
// define enumeration
enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 };
}
}
// end Inner
// end Example
1
0
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// declare
Create an unnamed
Outline
namespace. Its variables are
global.
variable
fig22_03.cpp
(2 of 3)
int main()
{
// output value doubleInUnnamed of unnamed namespace
cout << "doubleInUnnamed = " << doubleInUnnamed;
// output global variable
cout << "\n(global) integer1 = " << integer1;
// output values of Example namespace
cout << "\nPI = " << Example::PI << "\nE = "
<< Example::E << "\ninteger1 = "
<< Example::integer1 << "\nFISCAL3 = "
<< Example::Inner::FISCAL3 << endl;
Example::printValues();
return 0;
}
// end main
1
1
54
55
56
57
58
59
60
61
62
63
64
Outline
fig22_03.cpp
(3 of 3)
// end printValues
1
2
Outline
doubleInUnnamed = 88.22
(global) integer1 = 98
PI = 3.14159
E = 2.71828
integer1 = 8
FISCAL3 = 1992
fig22_03.cpp
output (1 of 1)
In printValues:
integer1 = 8
PI = 3.14159
E = 2.71828
doubleInUnnamed = 88.22
(global) integer1 = 98
FISCAL3 = 1992
1
3
14
Operator keyword
Description
and
or
not
logical AND
logical OR
logical NOT
not_eq
Inequality
bitand
bitor
xor
compl
Bitwise
Bitwise
Bitwise
Bitwise
and_eq
or_eq
^=
xor_eq
AND
inclusive OR
exclusive OR
complement
15
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
Outline
fig22_05.cpp
(1 of 2)
using std::cout;
using std::endl;
using std::boolalpha;
#include <iso646.h>
int main()
{
int a = 2;
int b = 3;
cout <<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
boolalpha
"
a and
"\n
a or
"\n
not
"\na not_eq
"\na bitand
"\na bit_or
"\n
a xor
"\n
compl
"\na and_eq
"\n a or_eq
"\na xor_eq
"
"
"
"
"
"
"
"
"
"
"
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
(
(
(
(
(
(
(
(
(
(
(
a and b )
a or b )
not a )
a not_eq b )
a bitand b )
a bitor b )
a xor b )
compl a )
a and_eq b )
a or_eq b )
a xor_eq b ) << endl;
1
6
28
29
30
31
Outline
return 0;
}
// end main
a and b: true
a or b: true
not a: false
a not_eq b: false
a bitand b: 3
a bit_or b: 3
a xor b: 0
compl a: -4
a and_eq b: 3
a or_eq b: 3
a xor_eq b: 1
fig22_05.cpp
(2 of 2)
fig22_05.cpp
output (1 of 1)
1
7
Define function
myFunction( myClass y )
18
Example
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
array.h (1 of 1)
#include <iostream>
using std::ostream;
// class Array definition
Without the explicit
class Array {
constructor
friend ostream &operator<<( ostreamkeyword,
&, constthis
Array
& );
be used for implicit
public:
conversions.
Array( int = 10 ); // default/conversion
constructor
~Array();
// destructor
private:
int size; // size of the array
int *ptr; // pointer to first element of array
};
can
#endif
// ARRAY_H
2
0
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
Outline
array.cpp (1 of 2)
using std::cout;
using std::ostream;
#include <new>
#include "array.h"
// default constructor for class Array (default size 10)
Array::Array( int arraySize )
{
size = ( arraySize < 0 ? 10 : arraySize );
cout << "Array constructor called for "
<< size << " elements\n";
// create space for array
ptr = new int[ size ];
// initialize array elements to zeroes
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0;
}
// end constructor
2
1
27
28
29
30
31
32
33
34
35
36
37
38
39
Outline
array.cpp (2 of 2)
// end operator<<
2
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
fig22_08.cpp
(1 of 2)
using std::cout;
#include "array.h"
void outputArray( const Array & );
int main()
{
Array integers1( 7 );
outputArray( integers1 );
outputArray( 15 );
return 0;
}
//
// convert
// end main
2
3
22
23
24
25
26
27
28
29
Outline
fig22_08.cpp
(2 of 2)
fig22_08.cpp
output (1 of 1)
// end outputArray
2
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
array.h (1 of 1)
#include <iostream>
using std::ostream;
// class Array definition
This time, declare constructor
class Array {
friend ostream &operator<<( ostream explicit.
&, const Array & );
public:
explicit Array( int = 10 ); // default constructor
~Array();
// destructor
private:
int size; // size of the array
int *ptr; // pointer to first element of array
};
#endif
// ARRAY_H
2
5
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
Outline
array.cpp (1 of 2)
using std::cout;
using std::ostream;
#include <new>
#include "array.h"
// default constructor for class Array (default size 10)
Array::Array( int arraySize )
{
size = ( arraySize < 0 ? 10 : arraySize );
cout << "Array constructor called for "
<< size << " elements\n";
// create space for array
ptr = new int[ size ];
// initialize array elements to zeroes
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0;
}
// end constructor
2
6
27
28
29
30
31
32
33
34
35
36
37
38
39
Outline
array.cpp (2 of 2)
// end operator<<
2
7
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
Outline
fig22_11.cpp
(1 of 2)
using std::cout;
#include "array.h"
void outputArray( const Array & );
int main()
{
Array integers1( 7 );
outputArray( integers1 );
// output
Array
This
call integers1
will cause an
error
when compiled.
// end main
2
8
26
27
28
29
30
31
32
Outline
fig22_11.cpp
(2 of 2)
fig22_11.cpp
output (1 of 1)
2
9
30
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
Outline
fig21_12.cpp
(1 of 2)
int main()
{
const TestMutable test( 99 );
cout << "Initial value: " << test.getValue();
test.modifyValue();
// modifies mutable member
cout << "\nModified value: " << test.getValue() << endl;
3
1
27
28
29
30
Outline
return 0;
}
// end main
Initial value: 99
Modified value: 100
fig21_12.cpp
(2 of 2)
fig21_12.cpp
output (1 of 1)
3
2
Class function
void *memPtr ()
Regular function pointer, to function that returns void and
takes no arguments
To call function
33
int Test::*vPtr
Pointer to an int member of class Test
To access
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
fig22_13.cpp
(1 of 2)
using std::cout;
using std::endl;
// class Test definition
class Test {
public:
void function() { cout << "function\n"; }
int value; // public data member
}; // end class Test
void arrowStar( Test * );
void dotStar( Test * );
int main()
{
Test test;
test.value = 8;
arrowStar( &test );
dotStar( &test );
// assign value 8
// pass address to arrowStar
// pass address to dotStar
3
5
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Outline
return 0;
}
// end main
fig22_13.cpp
(2 of 2)
// end arrowStar
Create
// access members of Test object data member using
.* pointer to data member
value. Then, access the data.
void dotStar( Test *testPtr2 )
{
int Test::*vPtr = &Test::value;
// declare pointer
cout << ( *testPtr2 ).*vPtr << endl;
}
// access value
// end dotStar
3
6
Outline
function
8
fig22_13.cpp
output (1 of 1)
3
7
Format
Use comma-separated list
38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Outline
base1.h (1 of 1)
#endif
// BASE1_H
3
9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Outline
base2.h (1 of 1)
#endif
// BASE2_H
4
0
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
Outline
derived.h (1 of 1)
#include <iostream>
using std::ostream;
#include "base1.h"
#include "base2.h"
#endif
// DERIVED_H
4
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
Note use of base-class
constructors in derived
class
derived.cpp
(1 of 1)
constructor.
// end operator<<
4
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
fig22_18.cpp
(1 of 2)
using std::cout;
using std::endl;
#include "base1.h"
#include "base2.h"
#include "derived.h"
int main()
{
Base1 base1( 10 ), *base1Ptr = 0; // create Base1 object
Base2 base2( 'Z' ), *base2Ptr = 0; // create Base2 object
Derived derived( 7, 'A', 3.5 );
// create Derived object
// print data members of base-class objects
cout << "Object base1 contains integer "
<< base1.getData()
<< "\nObject base2 contains character "
<< base2.getData()
<< "\nObject derived contains:\n" << derived << "\n\n";
4
3
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Outline
fig22_18.cpp
(2 of 2)
return 0;
}
// end main
4
4
Outline
fig22_18.cpp
output (1 of 1)
4
5
istream
iostream
46
virtual
inheritance
First Derived Class
Multiply-Derived Class
47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
fig22_20.cpp
(1 of 3)
// pure virtual
4
8
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Outline
fig22_20.cpp
(2 of 3)
4
9
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
int main()
{
Multiple both;
DerivedOne one;
DerivedTwo two;
Outline
// instantiate Multiple object
// instantiate DerivedOne object
// instantiate DerivedTwo object
fig22_20.cpp
(3 of 3)
used?
array[ 0 ] = &both;
array[ 1 ] = &one;
array[ 2 ] = &two;
// ERROR--ambiguous
// end main
5
0
Outline
fig22_20.cpp
output (1 of 1)
5
1
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
Outline
fig22_21.cpp
(1 of 3)
using std::cout;
using std::endl;
// class Base definition
class Base {
public:
// implicit default constructor
virtual void print() const = 0; // pure virtual
};
5
2
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Outline
fig22_21.cpp
(2 of 3)
5
3
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int main()
{
Multiple both;
DerivedOne one;
DerivedTwo two;
Outline
// instantiate Multiple object
// instantiate DerivedOne object
// instantiate DerivedTwo object
fig22_21.cpp
(3 of 3)
// end main
5
4
Outline
DerivedTwo
DerivedOne
DerivedTwo
fig22_21.cpp
output (1 of 1)
5
5