You are on page 1of 30

C++ & Standard Template Library

Ganesan C
Lead Engineer
Agenda

 The Aim of this session is to move towards STL (Standard Template


Library).
 Anyone who knows OOP under C++ can able to understand this
session.
 The Environment used here is Visual Studio 2013. But you can also
use Visual Studio 6.0 with necessary code changes.
 Mastering STL requires good knowledge in Templates and Virtual
functions.

2
C & C++

 C follows the procedural programming paradigm while C++ is a procedural


as well as object oriented.
 C uses the top-down approach while C++ uses the bottom-up approach.
 C++ supports overloading while C does not.
 We can use functions inside structures in C++ but not in C.
 The NAMESPACE feature in C++ is absent in case of C.
 C++ allows the use of reference variables while C does not.

3
C++ Intro

 Supports the following OOP concepts,


 Inheritance
 Encapsulation
 Polymorphism

 Inheritance is deriving the properties and methods from one class to


another or many to one.
 Encapsulation is binding and exposing the data to object and also to
outer to maintain data integrity and safety.
 Polymorphism is creating multiple definition for one declaration in
order to maintain uniformity.

4
Inheritance

 C++ supports Inheritance of one or


more classes to one or many.
 We have the following Inheritances
 Multiple Inheritance
 Multi-Level Inheritance

 It is the first step of Code


reusability.
 Also Inheritance involves in
Encapsulation.

5
Mode of Inheritance

 We have 3 modes of inheritance as


follows
 Public Inheritance
 Protected Inheritance
 Private Inheritance

 Note that, In any mode Private


Members of a class cannot be
inherited.

6
Encapsulation

 Binding the data with objects


 C++ uses 3 access specifies to
perform encapsulation.
 Public
 Protected
 Private

 The data is secured, but from


whom? From other object.

7
Polymorphism

 Creation of multiple definitions for


single declaration
 Helps to create generic
Programming
 Two methods to achieve this,
 Ad-hoc Polymorphism
 Runtime Polymorphism

 Ad-hoc polymorphism is achieved


by overloading
 Runtime polymorphism is achieved
by overriding or virtualization.

8
Classes in C++

 Classes are the basic root of OOP  Class members are separated by its
in C++ access keywords given below,
 Class are nothing but a frame or a  Public – members that can be

model of user design to the data accessed both in and out of class via
one of its objects and can be inherited.
processing
 Protected– members that can be
 Creation of class will not allocate
accessed only inside class via one of
memory to its members but the its objects and can be inherited.
declaration of objects does.  Private – members that can be
 Classes supports Inheritance of accessed both in and out of class via
members to other classes. one of its objects and can not be
inherited.
 Classes can have both variables
 By default class members are
and functions as members
Private.

9
Public declaration of Class members

10
Private declaration of Class members

11
Protected declaration of Class members

12
Constructors & Destructors

 Constructors & Destructors are  If a class created without user


special type members looks like defined constructor, default
functions but without return type. constructor will be provided by the
 Constructors & Destructors must be complier itself.
named with same class name
otherwise that will be considered as  The above is also applicable for
one of the normal member function destructors, copy constructor and
with ‘int’ as return type. assignment operator.
 Destructor various with constructor  The default constructor is also
by having a tidal symbol in front of called as non parameterized
the declaration. constructor as it does not take any
 Along with constructor two more parameter.
members are essential for a class.
 Copy constructor.
 Assignment Operator.
13
Constructors

 A class may have ‘n’ number of


constructors.
 A class cannot have more than one
non parameterized constructor as it
does not support virtual.
 If user provides any parameterized
constructor, the default constructor
will not be provided by complier
and the user has to provide one.

14
Copy Constructor

 When an object is passed a named object of


its own type as argument, its copy
constructor is invoked in order to construct a
copy.
 A copy constructor is a constructor whose
first parameter is of type reference to the
class itself (possibly const qualified)
 Copy constructor can contain more
parameters until the first parameter is the
reference of a object.
 Line::Line (const Line&, int a, char c)
 Copy Constructor can perform Deep Copy
which prevents the pointer to share same
memory while coping via objects.

15
Polymorphism

 One to Many form  Overriding is achieved by virtual


 Can be achieved by two ways concept.
 Overloading  Classes can have different
 Overriding definitions for single function with
 Overloading is done by varying same argument list, if the base
either no of arguments or type of class function declaration contains
arguments in a function. ‘virtual’ keyword in prefix.

 Variation in Return type cannot be  If the virtual function in a class


considered as Overloading does not have definition (=0), that
function is called Pure virtual
function and the class contains at
least one pure virtual function is
called Abstract class.

16
Templates

 Generic Programming is also


achieved by Templates.
 We have both function & class
templates.
 The common declaration of a
function template is
template <template-parameters> function-
declaration

 The common declaration of a class


template is
template <template-parameters>
Class { }

17
What is STL?

 Standard Template Library (aka STL) is a collection of Template


Classes which makes the Data handling easer in both storing and
processing.
 C based Data structures are widely replaced by STL, like linked list is
replaced by List.
 The advantage of using STL is easy manipulation and future focused.
 But STL is costlier to both programming and debugging.

18
STL That we are going to see…

Container
Sequences
Associative Containers Algorithms
String package Non-mutating algorithms
Rope Mutating algorithms
Container adaptors Sorting
Bitset Generalized numeric algorithms
Iterators Function Objects
Trivial Iterator Adopter
Input Iterator Memory Allocation
Output Iterator Allocators
Forward Iterator raw_storage_iterator
Bidirectional Iterator
Random Access Iterator

19
Container

 Classes whose purpose is to contain other objects


 Sequence containers - maintain the ordering of inserted elements that we
specify. Eg: vector
 Associative containers - elements are inserted in a pre-defined order. Eg:
map
 Container adapter - variation of a sequence or associative container that
restricts the interface for simplicity and clarity. Container adapters do not
support iterators.

20
More about Containers

 There is no guarantee that the elements of a Container are stored in any definite
order
 The lifetime of an element stored in a container cannot exceed that of the Container
itself
 The size of a container is the number of elements it contains. The size is a
nonnegative number.
 The area of a container is the total number of bytes that it occupies.
 A variable sized container is one that provides methods for inserting and/or removing
elements; its size may vary during a container's lifetime. A fixed size container is one
where the size is constant throughout the container's lifetime. In some fixed-size
container types, the size is determined at compile time.

21
Value type X::value_type The type of the object stored in a container. The value type must be Assignable, but need not
be DefaultConstructible.

Iterator type X::iterator The type of iterator used to iterate through a container's elements. The iterator's value type is expected
to be the container's value type. A conversion from the iterator type to the const iterator type must
exist. The iterator type must be an input iterator.

Const iterator type X::const_iterator A type of iterator that may be used to examine, but not to modify, a container's elements.

Reference type X::reference A type that behaves as a reference to the container's value type.

Const reference type X::const_reference A type that behaves as a const reference to the container's value type.

Pointer type X::pointer A type that behaves as a pointer to the container's value type.

Distance type X::difference_type A signed integral type used to represent the distance between two of the container's iterators. This
type must be the same as the iterator's distance type.

Size type X::size_type An unsigned integral type that can represent any nonnegative value of the container's distance type.

22
Valid expressions

Name Expression Type requirements Return type


Beginning of range a.begin() iterator if a is mutable, const_iterator otherwise
End of range a.end() iterator if a is mutable, const_iterator otherwise
Size a.size() size_type
Maximum size a.max_size() size_type
Empty container a.empty() Convertible to bool
Swap a.swap(b) void

23
Forward Container

 A Forward Container is
Name Expression Type requirements Return type
a Container whose elements are Equality a == b T is EqualityComparable Convertible
to bool
arranged in a definite order. The Inequality a != b T is EqualityComparable Convertible
to bool
ordering will not change spontaneously Less a<b T is LessThanComparable Convertible
to bool
from iteration to iteration Greater a>b T is LessThanComparable Convertible
to bool
Less or equal a <= b T is LessThanComparable Convertible
 Iterators into a Forward Container to bool
Greater or equal a >= b T is LessThanComparable Convertible
satisfy the forward to bool

iterator requirements.
 Forward Containers support multi-pass
algorithms and allow multiple iterators
into the same container to be active at
the same time.

24
Iterators

 An iterator is any object that, pointing to some element in a range of


elements
 A Trivial Iterator is an object that may be dereferenced to refer to some
other object
 An Input Iterator is an iterator that may be dereferenced to refer to some
object, and that may be incremented to obtain the next iterator in a
sequence
 An Output Iterator is a type that provides a mechanism for storing (but not
necessarily accessing) a sequence of values
 Forward Iterator and etc.

25
Vector [ vector<T, Alloc> ]

 A vector is a Sequence that supports


random access to elements, constant
time insertion and removal of elements
at the end, and linear time insertion
and removal of elements at the
beginning or in the middle. The
number of elements in a vector may
vary dynamically; memory
management is automatic. Vector is
the simplest of the STL container
classes, and in many cases the most
efficient.

26
Deque [ deque<T, Alloc> ]

 A deque is very much like a vector:


like vector, it is a sequence that
supports random access to elements,
constant time insertion and removal of
elements at the end of the sequence,
and linear time insertion and removal
of elements in the middle.

27
List [ list<T, Alloc>]

 A list is a doubly linked list. That is, it


is a Sequence that supports both
forward and backward traversal, and
(amortized) constant time insertion
and removal of elements at the
beginning or the end, or in the
middle. Lists have the important
property that insertion and splicing do
not invalidate iterators to list elements,
and that even removal invalidates only
the iterators that point to the elements
that are removed..

28
Container adaptors – stack, Queue

 A stack is an adaptor that provides a restricted subset of Container functionality: it provides


insertion, removal, and inspection of the element at the top of the stack. Stack is a "last in first
out" (LIFO) data structure: the element at the top of a stack is the one that was most recently
added.
 A queue is an adaptor that provides a restricted subset of Container functionality A queue is a
"first in first out" (FIFO) data structure. That is, elements are added to the back of the queue and
may be removed from the front; Q.front() is the element that was added to the queue least
recently.
 A priority_queue is an adaptor that provides a restricted subset of Container functionality: it
provides insertion of elements, and inspection and removal of the top element. It is guaranteed
that the top element is the largest element in the priority_queue, where the function
object Compare is used for comparisons.
 They do not allow iteration through its elements.

29
Thank you!
http://wf13.myhcl.com/sites/techceed/index.html

30

You might also like