C++ Templates: Generic Programming and Function Templates

1
Chapter 1
C++ Templates
Readings: Sections 1.6 and 1.7
2
Templates
Type-independent patterns that can work with
multiple data types.
Generic programming
Code reusable
Function Templates
These define logic behind the algorithms that work for
multiple data types.
Class Templates
These define generic class patterns into which specific data
types can be plugged in to produce new classes.
3
Function Templates Example
Generic function
 to find a
maximum value in a given
vector
If argument is a 
vector<int>
type, then compiler generates a
corresponding function
where 
Comparable
 is replaced
by 
int
 type.
Similarly for
vector<double>
,
vector<string>
 etc.
Assumption in this example:
Operator < is defined in the
Comparable.
Hence assumptions made by
the template must be clearly
stated.
t
e
m
p
l
a
t
e
 
<
c
l
a
s
s
 
C
o
m
p
a
r
a
b
l
e
>
4
Function Templates Usage
Each call to 
findMax()
 on
a different data type forces
the compiler to generate a
different function
 using the
template.
Compiler will complain about
findMax(v4)
because IntCell class does
not defined 
operator<
c
l
a
s
s
 
S
q
u
a
r
e
{
 
 
p
u
b
l
i
c
:
 
 
 
 
e
x
p
l
i
c
i
t
 
S
q
u
a
r
e
(
 
d
o
u
b
l
e
 
s
 
=
 
0
.
0
 
)
 
:
 
s
i
d
e
{
 
s
 
}
 
 
 
 
 
 
{
 
}
 
 
 
 
d
o
u
b
l
e
 
g
e
t
S
i
d
e
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
s
i
d
e
;
 
}
 
 
 
 
d
o
u
b
l
e
 
g
e
t
A
r
e
a
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
s
i
d
e
 
*
 
s
i
d
e
;
 
}
 
 
 
 
d
o
u
b
l
e
 
g
e
t
P
e
r
i
m
e
t
e
r
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
4
 
*
 
s
i
d
e
;
 
}
 
 
 
 
v
o
i
d
 
p
r
i
n
t
(
 
o
s
t
r
e
a
m
 
&
 
o
u
t
 
=
 
c
o
u
t
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
o
u
t
 
<
<
 
"
(
s
q
u
a
r
e
 
"
 
<
<
 
g
e
t
S
i
d
e
(
 
)
 
<
<
 
"
)
"
;
 
}
 
 
 
 
b
o
o
l
 
o
p
e
r
a
t
o
r
<
 
(
 
c
o
n
s
t
 
S
q
u
a
r
e
 
&
 
r
h
s
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
g
e
t
S
i
d
e
(
 
)
 
<
 
r
h
s
.
g
e
t
S
i
d
e
(
 
)
;
 
}
 
 
p
r
i
v
a
t
e
:
 
 
 
 
d
o
u
b
l
e
 
s
i
d
e
;
}
;
 
 
 
 
/
/
 
D
e
f
i
n
e
 
a
n
 
o
u
t
p
u
t
 
o
p
e
r
a
t
o
r
 
f
o
r
 
S
q
u
a
r
e
o
s
t
r
e
a
m
 
&
 
o
p
e
r
a
t
o
r
<
<
 
(
 
o
s
t
r
e
a
m
 
&
 
o
u
t
,
 
c
o
n
s
t
 
S
q
u
a
r
e
 
&
 
r
h
s
 
)
{
 
 
 
 
r
h
s
.
p
r
i
n
t
(
 
o
u
t
 
)
;
 
 
 
 
r
e
t
u
r
n
 
o
u
t
;
}
5
An example
Operator Overloading
Comparison 
operator<
Defines the meaning of
operator<
 for Employee
class.
Output 
operator<<
Define a global nonclass
function 
operator<<
 that
calls 
print(…)
Define a public member
function
  print( ostream & out)
t
e
m
p
l
a
t
e
 
<
t
y
p
e
n
a
m
e
 
O
b
j
e
c
t
,
 
t
y
p
e
n
a
m
e
 
C
o
m
p
a
r
a
t
o
r
>
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
f
i
n
d
M
a
x
(
 
c
o
n
s
t
 
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
&
 
a
r
r
,
 
C
o
m
p
a
r
a
t
o
r
 
c
m
p
 
)
{
 
 
 
 
i
n
t
 
m
a
x
I
n
d
e
x
 
=
 
0
;
 
 
 
 
f
o
r
(
 
i
n
t
 
i
 
=
 
1
;
 
i
 
<
 
a
r
r
.
s
i
z
e
(
 
)
;
 
+
+
i
 
)
 
 
 
 
 
 
 
 
i
f
(
 
c
m
p
.
i
s
L
e
s
s
T
h
a
n
(
 
a
r
r
[
 
m
a
x
I
n
d
e
x
 
]
,
 
a
r
r
[
 
i
 
]
 
)
 
)
 
 
 
 
 
 
 
 
 
 
 
 
m
a
x
I
n
d
e
x
 
=
 
i
;
 
 
 
 
r
e
t
u
r
n
 
a
r
r
[
 
m
a
x
I
n
d
e
x
 
]
;
}
c
l
a
s
s
 
C
a
s
e
I
n
s
e
n
s
i
t
i
v
e
C
o
m
p
a
r
e
{
 
 
p
u
b
l
i
c
:
 
 
 
 
b
o
o
l
 
i
s
L
e
s
s
T
h
a
n
(
 
c
o
n
s
t
 
s
t
r
i
n
g
 
&
 
l
h
s
,
 
c
o
n
s
t
 
s
t
r
i
n
g
 
&
 
r
h
s
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
s
t
r
c
a
s
e
c
m
p
(
 
l
h
s
.
c
_
s
t
r
(
 
)
,
 
r
h
s
.
c
_
s
t
r
(
 
)
 
)
 
<
 
0
;
 
}
}
;
i
n
t
 
m
a
i
n
(
 
)
{
 
 
 
 
v
e
c
t
o
r
<
s
t
r
i
n
g
>
 
a
r
r
 
=
 
{
 
"
Z
E
B
R
A
"
,
 
"
a
l
l
i
g
a
t
o
r
"
,
 
"
c
r
o
c
o
d
i
l
e
"
 
}
;
 
 
 
 
c
o
u
t
 
<
<
 
f
i
n
d
M
a
x
(
 
a
r
r
,
 
C
a
s
e
I
n
s
e
n
s
i
t
i
v
e
C
o
m
p
a
r
e
{
 
}
 
)
 
<
<
 
e
n
d
l
;
 
 
 
 
r
e
t
u
r
n
 
0
;
}
6
Function Objects
Objects whose primary purpose is
to define a function
Here 
findMax
 accepts a
Comparator
 parameter as a
function object.
Comparator assumed to define
the isLessThan(…) function.
There is a more formal way to
define function objects in C++
(next slide).
t
e
m
p
l
a
t
e
 
<
t
y
p
e
n
a
m
e
 
O
b
j
e
c
t
,
 
t
y
p
e
n
a
m
e
 
C
o
m
p
a
r
a
t
o
r
>
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
f
i
n
d
M
a
x
(
 
c
o
n
s
t
 
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
&
 
a
r
r
,
C
o
m
p
a
r
a
t
o
r
 
i
s
L
e
s
s
T
h
a
n
 
)
{
 
 
 
 
i
n
t
 
m
a
x
I
n
d
e
x
 
=
 
0
;
 
 
 
 
f
o
r
(
 
i
n
t
 
i
 
=
 
1
;
 
i
 
<
 
a
r
r
.
s
i
z
e
(
 
)
;
 
+
+
i
 
)
 
 
 
 
 
 
 
 
i
f
(
 
i
s
L
e
s
s
T
h
a
n
(
 
a
r
r
[
 
m
a
x
I
n
d
e
x
 
]
,
 
a
r
r
[
 
i
 
]
 
)
 
)
 
 
 
 
 
 
 
 
 
 
 
 
m
a
x
I
n
d
e
x
 
=
 
i
;
 
 
 
 
r
e
t
u
r
n
 
a
r
r
[
 
m
a
x
I
n
d
e
x
 
]
;
}
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
f
i
n
d
M
a
x
(
 
c
o
n
s
t
 
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
&
 
a
r
r
 
)
{
 
 
 
 
r
e
t
u
r
n
 
f
i
n
d
M
a
x
(
 
a
r
r
,
 
l
e
s
s
<
O
b
j
e
c
t
>
{
 
}
 
)
;
}
c
l
a
s
s
 
C
a
s
e
I
n
s
e
n
s
i
t
i
v
e
C
o
m
p
a
r
e
{
 
 
p
u
b
l
i
c
:
 
 
 
 
b
o
o
l
 
o
p
e
r
a
t
o
r
(
 
)
(
 
c
o
n
s
t
 
s
t
r
i
n
g
 
&
 
l
h
s
,
 
c
o
n
s
t
 
s
t
r
i
n
g
 
&
 
r
h
s
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
s
t
r
c
a
s
e
c
m
p
(
 
l
h
s
.
c
_
s
t
r
(
 
)
,
 
r
h
s
.
c
_
s
t
r
(
 
)
 
)
 
<
 
0
;
 
}
}
;
i
n
t
 
m
a
i
n
(
 
)
{
 
 
 
 
v
e
c
t
o
r
<
s
t
r
i
n
g
>
 
a
r
r
 
=
 
{
 
"
Z
E
B
R
A
"
,
 
"
a
l
l
i
g
a
t
o
r
"
,
 
"
c
r
o
c
o
d
i
l
e
"
 
}
;
 
 
 
 
c
o
u
t
 
<
<
 
f
i
n
d
M
a
x
(
 
a
r
r
,
 
C
a
s
e
I
n
s
e
n
s
i
t
i
v
e
C
o
m
p
a
r
e
{
 
}
 
)
 
<
<
 
e
n
d
l
;
 
 
 
 
c
o
u
t
 
<
<
 
f
i
n
d
M
a
x
(
 
a
r
r
 
)
 
<
<
 
e
n
d
l
;
 
 
 
 
r
e
t
u
r
n
 
0
;
}
7
Function objects
in C++ style
Using operator overloading
Define 
operator ()
 for
CaseInsensitiveCompare  class
Instead of
cmp.operator()(x,y)
  
we can use
 
cmp(x,y)
Case-sensitive comparison can
also be performed using STL
function object
less<Object>(...)
/
*
*
*
 
A
 
c
l
a
s
s
 
f
o
r
 
s
i
m
u
l
a
t
i
n
g
 
a
 
m
e
m
o
r
y
 
c
e
l
l
.
*
/
t
e
m
p
l
a
t
e
 
<
t
y
p
e
n
a
m
e
 
O
b
j
e
c
t
>
c
l
a
s
s
 
M
e
m
o
r
y
C
e
l
l
{
 
 
p
u
b
l
i
c
:
 
 
 
 
e
x
p
l
i
c
i
t
 
M
e
m
o
r
y
C
e
l
l
(
 
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
i
n
i
t
i
a
l
V
a
l
u
e
 
=
 
O
b
j
e
c
t
{
 
}
 
)
 
 
 
 
 
 
:
 
s
t
o
r
e
d
V
a
l
u
e
{
 
i
n
i
t
i
a
l
V
a
l
u
e
 
}
 
{
 
}
 
 
 
 
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
r
e
a
d
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
s
t
o
r
e
d
V
a
l
u
e
;
 
}
 
 
 
 
v
o
i
d
 
w
r
i
t
e
(
 
c
o
n
s
t
 
O
b
j
e
c
t
 
&
 
x
 
)
 
 
 
 
 
 
{
 
s
t
o
r
e
d
V
a
l
u
e
 
=
 
x
;
 
}
 
 
p
r
i
v
a
t
e
:
 
 
 
 
O
b
j
e
c
t
 
s
t
o
r
e
d
V
a
l
u
e
;
}
;
8
Class Template Example
MemoryCell template can be used for any
type 
Object
.
Assumptions
Object has a zero parameter constructor
Object has a copy constructor
Copy-assignment operator
Convention
Class templates declaration and
implementation usually combined in a
single file.
It is not easy to separate them in
independent files due to complex c++
syntax.
This is different from the convention of
separating class interface and
implementation in different files.
Another Way
(Implementation outside of declaration)
9
template <typename T>
class MemoryCell {
public:
 
explicit MemoryCell(const T& initialVale = T{});
 
const T& read() const;
 
void write(const T& x);
private:
 
T storedValue;
};
template <typename T>
MemoryCell<T>::MemoryCell(const T& initialValue): storedValue{initialValue} {}
template <typename T>
const T& MemoryCell<T>::read() const {
 
return storedValue;
}
template <typename T>
void MemoryCell<T>::write(const T& x) {
 
storedValue = x;
}
i
n
t
 
m
a
i
n
(
 
)
{
 
 
 
 
M
e
m
o
r
y
C
e
l
l
<
i
n
t
>
 
 
 
 
m
1
;
 
 
 
 
M
e
m
o
r
y
C
e
l
l
<
s
t
r
i
n
g
>
 
m
2
{
 
"
h
e
l
l
o
"
 
}
;
 
 
 
 
m
1
.
w
r
i
t
e
(
 
3
7
 
)
;
 
 
 
 
m
2
.
w
r
i
t
e
(
 
m
2
.
r
e
a
d
(
 
)
 
+
 
"
 
w
o
r
l
d
"
 
)
;
 
 
 
 
c
o
u
t
 
<
<
 
m
1
.
r
e
a
d
(
 
)
 
<
<
 
e
n
d
l
 
<
<
 
m
2
.
r
e
a
d
(
 
)
 
<
<
 
e
n
d
l
;
 
 
 
 
r
e
t
u
r
n
 
0
;
}
10
Class Template Usage Example
MemoryCell can be used to
store both primitive and class
types.
Remember
MemoryCell
 is not a class.
It’s a class template.
MemoryCell<int>,
MemoryCell<string>
 etc
are classes.
#
i
n
c
l
u
d
e
 
<
v
e
c
t
o
r
>
u
s
i
n
g
 
n
a
m
e
s
p
a
c
e
 
s
t
d
;
t
e
m
p
l
a
t
e
 
<
t
y
p
e
n
a
m
e
 
O
b
j
e
c
t
>
c
l
a
s
s
 
m
a
t
r
i
x
{
 
 
p
u
b
l
i
c
:
 
 
 
 
m
a
t
r
i
x
(
 
i
n
t
 
r
o
w
s
,
 
i
n
t
 
c
o
l
s
 
)
 
:
 
a
r
r
a
y
(
 
r
o
w
s
 
)
 
 
 
 
{
 
 
 
 
 
 
 
 
f
o
r
(
 
a
u
t
o
 
&
 
t
h
i
s
R
o
w
 
:
 
a
r
r
a
y
 
)
 
 
 
 
 
 
 
 
 
 
 
 
t
h
i
s
R
o
w
.
r
e
s
i
z
e
(
 
c
o
l
s
 
)
;
 
 
 
 
}
 
 
 
 
m
a
t
r
i
x
(
 
c
o
n
s
t
 
v
e
c
t
o
r
<
v
e
c
t
o
r
<
O
b
j
e
c
t
>
>
 
&
 
v
 
)
 
:
 
a
r
r
a
y
{
 
v
 
}
 
 
 
 
 
 
{
 
}
 
 
 
 
m
a
t
r
i
x
(
 
v
e
c
t
o
r
<
v
e
c
t
o
r
<
O
b
j
e
c
t
>
>
 
&
&
 
v
 
)
 
:
 
a
r
r
a
y
{
 
s
t
d
:
:
m
o
v
e
(
 
v
 
)
 
}
 
 
 
 
 
 
{
 
}
 
 
 
 
c
o
n
s
t
 
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
&
 
o
p
e
r
a
t
o
r
[
]
(
 
i
n
t
 
r
o
w
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
a
r
r
a
y
[
 
r
o
w
 
]
;
 
}
 
 
 
 
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
&
 
o
p
e
r
a
t
o
r
[
]
(
 
i
n
t
 
r
o
w
 
)
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
a
r
r
a
y
[
 
r
o
w
 
]
;
 
}
 
 
 
 
i
n
t
 
n
u
m
r
o
w
s
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
a
r
r
a
y
.
s
i
z
e
(
 
)
;
 
}
 
 
 
 
i
n
t
 
n
u
m
c
o
l
s
(
 
)
 
c
o
n
s
t
 
 
 
 
 
 
{
 
r
e
t
u
r
n
 
n
u
m
r
o
w
s
(
 
)
 
?
 
a
r
r
a
y
[
 
0
 
]
.
s
i
z
e
(
 
)
 
:
 
0
;
 
}
 
 
p
r
i
v
a
t
e
:
 
 
 
 
v
e
c
t
o
r
<
v
e
c
t
o
r
<
O
b
j
e
c
t
>
>
 
a
r
r
a
y
;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/
/
 
C
+
+
1
1
/
/
 
v
e
c
t
o
r
<
v
e
c
t
o
r
<
O
b
j
e
c
t
>
 
>
 
a
r
r
a
y
;
 
 
/
/
 
p
r
i
o
r
 
t
o
 
c
+
+
1
1
}
;
11
Matrices
C++ library does not provide a
matrix
 
class
Constructor
Creates 
rows
 number of zero-
sized vectors
Resizes each vector to 
col
elements
Two types of [] operators
One for LHS that returns by
reference
Another for RHS that returns by
constant reference
So we have two very identical
functions
What makes their signatures
different?
12
Reading Assignment
2.1, 2.2, 2.3, 2.4.1, 2.4.2, 2.4.3
Slide Note
Embed
Share

Exploring the concepts of C++ templates including type-independent patterns for working with various data types, generic class patterns, function templates, and their usage. The reading covers examples of function templates for finding maximum values in vectors and highlights the importance of clearly defining assumptions. It also delves into class templates, operator overloading, and comparison methods in C++. Additionally, function objects and their usage in defining custom functions for specific data types are discussed.

  • C++
  • Templates
  • Generic Programming
  • Function Templates
  • Operator Overloading

Uploaded on Sep 29, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7 1

  2. Templates Type-independent patterns that can work with multiple data types. Generic programming Code reusable Function Templates These define logic behind the algorithms that work for multiple data types. Class Templates These define generic class patterns into which specific data types can be plugged in to produce new classes. 2

  3. Function Templates Example Generic function to find a maximum value in a given vector If argument is a vector<int> type, then compiler generates a corresponding function where Comparable is replaced by int type. Similarly for vector<double>, vector<string> etc. Assumption in this example: Operator < is defined in the Comparable. Hence assumptions made by the template must be clearly stated. template <class Comparable> 3

  4. Function Templates Usage Each call to findMax() on a different data type forces the compiler to generate a different function using the template. Compiler will complain about findMax(v4) because IntCell class does not defined operator< 4

  5. An example class Square { public: explicit Square( double s = 0.0 ) : side{ s } { } double getSide( ) const { return side; } double getArea( ) const { return side * side; } double getPerimeter( ) const { return 4 * side; } Operator Overloading Comparison operator< Defines the meaning of operator< for Employee class. Output operator<< Define a global nonclass function operator<< that calls print( ) Define a public member function print( ostream & out) void print( ostream & out = cout ) const { out << "(square " << getSide( ) << ")"; } bool operator< ( const Square & rhs ) const { return getSide( ) < rhs.getSide( ); } private: double side; }; // Define an output operator for Square ostream & operator<< ( ostream & out, const Square & rhs ) { rhs.print( out ); return out; } 5

  6. Function Objects template <typename Object, typename Comparator> const Object & findMax( const vector<Object> & arr, Comparator cmp ) { int maxIndex = 0; Objects whose primary purpose is to define a function for( int i = 1; i < arr.size( ); ++i ) if( cmp.isLessThan( arr[ maxIndex ], arr[ i ] ) ) maxIndex = i; Here findMax accepts a Comparator parameter as a function object. Comparator assumed to define the isLessThan( ) function. return arr[ maxIndex ]; } class CaseInsensitiveCompare { public: bool isLessThan( const string & lhs, const string & rhs ) const { return strcasecmp( lhs.c_str( ), rhs.c_str( ) ) < 0; } }; There is a more formal way to define function objects in C++ (next slide). int main( ) { vector<string> arr = { "ZEBRA", "alligator", "crocodile" }; cout << findMax( arr, CaseInsensitiveCompare{ } ) << endl; return 0; } 6

  7. Function objects in C++ style template <typename Object, typename Comparator> const Object & findMax( const vector<Object> & arr, { int maxIndex = 0; Comparator isLessThan ) for( int i = 1; i < arr.size( ); ++i ) if( isLessThan( arr[ maxIndex ], arr[ i ] ) ) maxIndex = i; Using operator overloading Define operator () for CaseInsensitiveCompare class Instead of cmp.operator()(x,y) we can use cmp(x,y) return arr[ maxIndex ]; } const Object & findMax( const vector<Object> & arr ) { return findMax( arr, less<Object>{ } ); } class CaseInsensitiveCompare { public: bool operator( )( const string & lhs, const string & rhs ) const { return strcasecmp( lhs.c_str( ), rhs.c_str( ) ) < 0; } }; Case-sensitive comparison can also be performed using STL function object less<Object>(...) int main( ) { vector<string> arr = { "ZEBRA", "alligator", "crocodile" }; cout << findMax( arr, CaseInsensitiveCompare{ } ) << endl; cout << findMax( arr ) << endl; return 0; } 7

  8. Class Template Example /** * A class for simulating a memory cell. */ template <typename Object> class MemoryCell { public: explicit MemoryCell( const Object & initialValue = Object{ } ) : storedValue{ initialValue } { } const Object & read( ) const { return storedValue; } void write( const Object & x ) { storedValue = x; } private: Object storedValue; }; MemoryCell template can be used for any type Object. Assumptions Object has a zero parameter constructor Object has a copy constructor Copy-assignment operator Convention Class templates declaration and implementation usually combined in a single file. It is not easy to separate them in independent files due to complex c++ syntax. This is different from the convention of separating class interface and implementation in different files. 8

  9. Another Way (Implementation outside of declaration) template <typename T> class MemoryCell { public: explicit MemoryCell(const T& initialVale = T{}); const T& read() const; void write(const T& x); private: T storedValue; }; template <typename T> MemoryCell<T>::MemoryCell(const T& initialValue): storedValue{initialValue} {} template <typename T> const T& MemoryCell<T>::read() const { return storedValue; } template <typename T> void MemoryCell<T>::write(const T& x) { storedValue = x; } 9

  10. Class Template Usage Example MemoryCell can be used to store both primitive and class types. int main( ) { MemoryCell<int> m1; MemoryCell<string> m2{ "hello" }; m1.write( 37 ); m2.write( m2.read( ) + " world" ); cout << m1.read( ) << endl << m2.read( ) << endl; Remember MemoryCell is not a class. It s a class template. MemoryCell<int>, MemoryCell<string> etc are classes. return 0; } 10

  11. #include <vector> using namespace std; Matrices template <typename Object> class matrix { public: matrix( int rows, int cols ) : array( rows ) { for( auto & thisRow : array ) thisRow.resize( cols ); } C++ library does not provide a matrix class Constructor Creates rows number of zero- sized vectors Resizes each vector to col elements matrix( const vector<vector<Object>> & v ) : array{ v } { } matrix( vector<vector<Object>> && v ) : array{ std::move( v ) } { } const vector<Object> & operator[]( int row ) const { return array[ row ]; } vector<Object> & operator[]( int row ) { return array[ row ]; } Two types of [] operators One for LHS that returns by reference Another for RHS that returns by constant reference So we have two very identical functions What makes their signatures different? int numrows( ) const { return array.size( ); } int numcols( ) const { return numrows( ) ? array[ 0 ].size( ) : 0; } private: vector<vector<Object>> array; // C++11 // vector<vector<Object> > array; // prior to c++11 }; 11

  12. Reading Assignment 2.1, 2.2, 2.3, 2.4.1, 2.4.2, 2.4.3 12

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#