This presentation provides an introduction to the C++ programming language, covering topics such as the C++ Standard Library, data types, variables, control structures, functions, and the class template vector. Participants will learn about the different types of data and how to use them in C++ programming, as well as the various control structures used to manage program flow. We will also explore the C++ Standard Library, which includes a variety of built-in functions and classes that can be used to simplify and speed up programming tasks. Specifically, we will cover the class template vector, which is a container class that allows for the creation and manipulation of dynamic arrays in C++. We will discuss the syntax and usage of the vector class template, including how to add and remove elements, how to access individual elements, and how to iterate through the entire vector. By the end of the presentation, participants will have a basic understanding of C++ programming, as well as an understanding of how to use the vector class template to work with dynamic arrays.
3. C++ Programming Language
C++ is a general-purpose programming language created by Bjarne
Stroustrup in Bell Labs as an extension of the C programming
language, or "C with Classes". The language has expanded
significantly over time, and modern C++ now has object-oriented,
generic, and functional features in addition to facilities for low-level
memory manipulation. It is almost always implemented as a
compiled language, and many vendors provide C++ compilers,
including the Free Software Foundation, Microsoft, Intel, Oracle,
and IBM, so it is available on many platforms.
4. C++ has also been found useful in many other contexts,
with key strengths being software infrastructure and
resource-constrained applications, including desktop
applications, video games, servers (web search, or SQL
servers), and performance-critical applications (telephone
switches or space probes).
C++ Programming Language
5. The C++ language has two main components: a direct mapping
of hardware features provided primarily by the C subset, and
zero-overhead abstractions based on those mappings.
Stroustrup describes C++ as "a light-weight abstraction
programming language designed for building and using
efficient and elegant abstractions"; and "offering both
hardware access and abstraction is the basis of C++.
Doing it efficiently is what distinguishes it from other
languages."
C++ Programming Language
6. #include <iostream>
using namespace std;
int main()
{
cout<< "Hello, world!n";
return 0;
}
C++ inherits most of C's syntax.
Hello world program that uses the C++ Standard Library to write a message to standard output:
7. The C++ standard consists of two parts: the core language and the standard
library.
C++ programmers expect the latter on every major implementation of C++; it
includes aggregate types (vectors, lists, maps, sets, queues, stacks, arrays, ),
algorithms (find, for_each, binary_search, random_shuffle, etc.), input/output
facilities (iostream, for reading from and writing to the console and files),
filesystem library, localization support, smart pointers for automatic memory
management, regular expression support, time utilities (measurement, getting
current time, etc.), a random number generator and a slightly modified version
of the C standard library (to make it comply with the C++ type system).
C++ Standard Library
8. A large part of the C++ library is based on the Standard Template Library
(STL). Useful tools provided by the STL include containers as the collections
of objects (such as vectors and lists), iterators that provide array-like access to
containers, and algorithms that perform operations such as searching and
sorting.
As in C, the features of the library are accessed by using the #include directive
to include a standard header.
The C++ Standard Library provides 105 standard headers, of which 27 are
deprecated.
C++ Standard Library
9. Class Template vector
Templates are the mechanism by which C++ implements
the generic concept. Simply, they allow you to pass data type as
a parameter so that you don’t need to write the same code for different
data types.
Sometimes, you need a class implementation that is same for all classes,
only the data types used are different.
Normally, you would need to create a different class for each data type
OR create different member variables and functions within a single
class.This will unnecessarily bloat your code base and will be hard to
maintain, as a change is one class/function should be performed on all
classes/functions.
However, class templates make it easy to reuse the same code for all
data types.
10. The C++ Standard Library contains many class templates, in particular
the containers adapted from the Standard Template Library, such
as vector.
The elements of a vector are stored contiguously. Like all dynamic
array implementations, vectors have low memory usage. Unlike other
STL containers, such as lists, vectors allow the user to denote an initial
capacity for the container.
Vectors allow random access; that is, an element of a vector may be
referenced in the same manner as elements of arrays (by array indices).
A typical vector implementation consists, internally, of a pointer to a
dynamically allocated array, and possibly data members holding the
capacity and size of the vector. The size of the vector refers to the
actual number of elements, while the capacity refers to the size of the
internal array.
Class Template vector
11. The template class that has defined
above serves to store elements of any
valid type.
So as seen, if we wanted to declare an object of
this class to store integer values of type int we
would write:
This same class would also be used to create an
object to store any other type:
Uses of Class Templates
• Remove code duplication
• Generic callback
• Re-use source code as opposed to inheritance and composition, which provides a way to reuse object code