An Overview of C++
An Overview of C++
An Overview of C++
Douglas C. Schmidt
C++ Overview
An Overview of C++
Douglas C. Schmidt
Professor [email protected] www.dre.vanderbilt.edu/schmidt/ Department of EECS Vanderbilt University (615) 343-8197
C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80s The original cfront translated C++ into C for portability However, this was dicult to debug and potentially inecient Many native host machine compilers now exist e.g., Borland, DEC, GNU, HP, IBM, Microsoft, Sun, Symantec, etc. C++ is a mostly upwardly compatible extension of C that provides: 1. 2. 3. 4. Stronger typechecking Support for data abstraction Support for object-oriented programming Support for generic programming
Vanderbilt University
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Questionable Enhancements
Default values for function parameters Operator & function overloading Variable declarations may occur anywhere statements may appear within a block Allows user-dened conversion operators Static data initializers may be arbitrary expressions
Vanderbilt University
Vanderbilt University
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Stack Example
The following slides examine several alterative methods of implementing a Stack Begin with C & evolve up to various C++ implementations First, consider the bare-bones implementation:
typedef int T; #define MAX_STACK 100 /* const int MAX_STACK = 100; */ T stack[MAX_STACK]; int top = 0; T item = 10; stack[top++] = item; // push ... item = stack[--top]; // pop
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Main problems: 1. 2. 3. 4.
12
The programmer must call create() rst & destroy() last! There is only one stack & only one type of stack Name space pollution... Non-reentrant
13
Vanderbilt University
Vanderbilt University
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
#include "stack.h" int Stack_create (Stack *s, size_t size) { s->top_ = 0; s->size_ = size; s->stack_ = malloc (size * sizeof (T)); return s->stack_ == 0 ? -1 : 0; } void Stack_destroy (Stack *s) { free ((void *) s->stack_); s->top_ = 0; s->size_ = 0; s->stack_ = 0; } void Stack_push (Stack *s, T item) { s->stack_[s->top_++] = item; } void Stack_pop (Stack *s, T *item) { *item = s->stack_[--s->top_]; } int Stack_is_empty (Stack *s) { return s->top_ == 0; }
Vanderbilt University
14
Vanderbilt University
15
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
16
Vanderbilt University
17
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Manager operations
Stack::Stack (size_t s): top_ (0), size_ (s), stack_ (new T[s]) {} Stack::Stack (const Stack &s) : top_ (s.top_), size_ (s.size_), stack_ (new T[s.size_]) { for (size_t i = 0; i < s.size_; ++i) stack_[i] = s.stack_[i]; } void Stack::operator = (const Stack &s) { if (this == &s) return; T *temp_stack = new T[s.size_]; delete [] stack_; stack_ = 0; for (size_t i = 0; i < s.size_; ++i) temp_stack[i] = s.stack_[i]; stack_ = temp_stack; top_ = s.top_; size_ = s.size_; } Stack::~Stack (void) { delete [] stack_; }
Vanderbilt University
18
Vanderbilt University
19
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
20
Vanderbilt University
21
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
22
Vanderbilt University
23
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
24
Vanderbilt University
25
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
26
Vanderbilt University
27
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
28
Vanderbilt University
29
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Use case
Vanderbilt University
30
Vanderbilt University
31
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Template Implementation in C++ A parameterized type Stack class interface using C++
template <typename T> class Stack { public: Stack (size_t size); ~Stack (void) void push (const T &item); void pop (T &item); bool is_empty (void) const; bool is_full (void) const; private: size_t top_, size_; T *stack_; };
To simplify the following examples well omit exception handling, but note that its important to ensure exception-safety guarantees!
Vanderbilt University 32
Vanderbilt University
33
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
34
Vanderbilt University
35
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
By using pure virtual methods, we can guarantee that the compiler wont allow instantiation!
Vanderbilt University
36
Vanderbilt University
37
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Note that the use of the Cheshire cat idiom allows the library writer to completely hide the representation of class V Stack...
Vanderbilt University
40
Vanderbilt University
41
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Vanderbilt University
42
Vanderbilt University
43
An Overview of C++
Douglas C. Schmidt
An Overview of C++
Douglas C. Schmidt
Summary
A major contribution of C++ is its support for dening abstract data types (ADTs) & for generic programming e.g., classes, parameterized types, & exception handling For some systems, C++s ADT support is more important than using the OO features of the language For other systems, the use of C++s OO features is essential to build highly exible & extensible software e.g., inheritance, dynamic binding, & RTTI
Note, no need to stop, modify, & restart an executing application! Naturally, this requires careful conguration management...
Vanderbilt University 44
Vanderbilt University
45