Professional Documents
Culture Documents
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Operator overloading ( )" Does not help for stack that is
automatic variable.
• Attach different meaning to expressions such as a + b
Programmer could
References
)+*, -/. inadvertently create
Pass-by-reference function arguments
•
)0*1" uninitialized stack.
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
1
C++ Stack Class C++ Stack Class
Natural to use Members (functions, data) can be public, protected,
or private
)?=@"
x A
- xx.#"
,-R.'" class Stack {
char s[SIZE];
A=, +2
x7-/.@" public:
char pop();
};
)+*
A)32 % =F A)"
);=<
- .#" ) ;9<A -x.'" Stack st;
st.s[0] = ‘a’; // Error: sp is private
A=, +2
);
<78-/.'" st.pop(); // OK
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
2
Complex Number Type Complex Number Class
Member functions including operators can be defined Operators can also be defined outside classes
inside or outside the class definition
75 6 C7,/78HN#-/7 % = 7 56 J'K
75 6 7 % = 7 56 .
75 6
7,97/N 2-/7 %
J
75 6 A
. 7 5 6
5?2? "
0N2 '" // Copy constructor
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
3
Using a template Display-list example
Stack<char> cs; // Instantiates the specialized code Say you want to draw a graphical scene
cs.push(‘a’); List of objects
char c = cs.pop(); • lines, arcs, circles, squares, etc.
Inheritance Inheritance
Inheritance lets you build derived classes from base class Shape {
classes double x, y; // Base coordinates of shape
public:
void translate(double dx, double dy) {
class Shape { /* … */ }; x += dx; y += dy;
class Line : public Shape { /* … */ }; // Also a Shape }
Line inherits both the
}; representation and
class Arc : public Shape { /* … */ }; // Also a Shape
member functions of
class Line : public Shape { the Shape class
};
Shape *dlist[10];
Line l;
l.translate(1,3); // Invoke Shape::translate()
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
to change “draw
6A 97
L
97
Shape *dl[10];
dl[0] = new Line;
everything” code
761K}F " 76 @KZ " dl[1] = new Arc;
( " 7
6K}F " dl[0]->draw(); // invoke Line::draw()
( " dl[1]->draw(); // invoke Arc::draw()
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
4
Implementing Virtual Functions Cfront
Involves some overhead Virtual table How the language was first compiled
Object of
for class Virt
type Virt Full compiler that produced C as output
class Virt { &Virt::foo
vptr C++ semantics therefore expressible in C
int a, b; &Virt::bar
a
virtual void foo();
b
virtual void bar(); C++ model of computation ultimately the same
};
C++ syntax substantial extension of C
C++ semantics refer to the same model as C
$
C++
I 7 ?
- $ /?* I . $
Equivalent
I 7 $ C implementation
?
- ?* I .
I ;9< -9.#" -*,- I ;9< I ,
,..,- I .@" So why use C++?
( ( • Specifications are clearer, easier to write and maintain
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
5
Multiple Inheritance Ambiguities Duplicate Base Classes
Ambiguity can be resolved explicitly A class may be inherited more than once
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
vptr &C::f 0 c
B *b = &c;
&C::f
x B in C’s vtbl vptr
b->f(); // C::f()
vptr &C::f –2 x B in C’s vtbl
y &B::g 0 vptr &C::f_in_B
1. b is a B*: vptr has f(), g() z y &B::g
z
!"$#&%'( )&* (+,.-
9 /10 )2 / !3$% )* +3465,87
2. Call C::f( this – 2 )
3. First argument now points to a C
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
6
Namespaces Namespaces
Namespace pollution Scope for enclosing otherwise global declarations
• Occurs when building large systems from pieces
• Identical globally-visible names clash
• How many programs have a “print” function?
! "
• Very difficult to fix
%$'&)(+*,-*/.102-.43/&.
#
Classes suggest a solution
65 %
(
(
/
#7 8 69/:
(
%;$69< 4>== >
4"== ! ?4.1
Two f’s are separate
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Namespaces Namespaces
'@ 1A directive brings namespaces or objects into
Namespaces are open: declarations can be added
scope
# %$'&)(+*,-*/.1023!.&.
)
"
"
!
@
4 A 4"== B
A " // Add Mine::g() to Mine
/ (
?CD6E ."F // invoke Mine::print
@
4A:1 4 /% 4
G-/ H$D2I (
// Mine::pi
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Namespaces Exceptions
Declarations and definitions can be separated A high-level replacement for C’s setjmp/longjmp
! @ :L; '?
)
"
/(
#7 CD3-+GLC; "
(
/J
J 4"== D /(
?CD
I6KDI +9M
!
7 C>
: 4L; D
! )1NC O
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
7
Standard Template Library C++ IO Facilities
I/O Facilities: iostream C’s printing facility is clever but unsafe
I?
> A
@ 7 %A A
Garbage-collected String class
)1N
O
Containers
• vector, list, queue, stack, map, set
Hard for compiler to typecheck argument types
Numerical against format string
• complex, valarray
General algorithms C++ IO overloads the << and >> operators
• search, sort
.
@ A
Type safe
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
vector<int> v;
vector<int> v; for ( vector<int>::iterator i = v.begin();
v.push_back(3); // vector can behave as a stack i != v.end() ; i++ ) {
v.push_back(2); int entry = *i;
int j = v[0]; // operator[] defined for vector }
…
v.begin() v.end()
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
8
Other Containers Associative Containers
Insert/Delete from Keys must be totally ordered
front mid. end random access
Implemented with trees
vector O(n) O(n) O(1) O(1) set
list O(1) O(1) O(1) O(n) • Set of objects
set<int, less<int> > s;
deque O(1) O(n) O(1) O(n)
s.insert(5);
set<int, less<int> >::iterator i = s.find(3);
map
•Associative Array
map<int, char*> m;
m[3] = “example”;
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
• Slower program execution
• Unoptimized C++ code often much larger, slower than
equivalent C
Function name overloading
• Completely resolved at compile time
Namespaces
• Completely resolved at compile time
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
• Long argument lists costly in C and C++ anyway
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
9
High-cost Features High-cost Features
Multiple inheritance Exceptions
• Makes objects much larger (multiple virtual pointers) • Typical implementation:
• Virtual tables larger, more complicated
• Calling virtual functions even slower • When exception is thrown, look up stack until handler
is found and destroy automatic objects on the way
Templates
• Mere presence of exceptions does not slow program
• Compiler generates separate code for each copy
• Often requires extra tables or code to direct clean-up
• Can greatly increase code sizes
• Throwing and exception often very slow
• No performance penalty
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
10