Intro To C++
Intro To C++
Introduction to C++
Introduction
• The language was developed by Bjarne
Strousstrup in 1979 at Bell Labs.
• C++ is a general-purpose programming
language that was developed as an
enhancement of the C language to include
object-oriented paradigm.
• It is a compiled language.
• C++ is a middle-level language rendering it the
advantage of programming low-level (drivers,
kernels) and even higher-level applications
(games, GUI, desktop apps etc.).
• The basic syntax and code structure of both C
and C++ are the same.
Features of C++
• Simple: It is a simple language in the sense that programs can be
broken down into logical units and parts, has a rich library support
and a variety of data-types.
• Machine Independent but Platform Dependent: A C++ executable
is not platform-independent (compiled programs on Linux won’t
run on Windows), however they are machine independent.
• Mid-level language: It is a mid-level language as we can do both
systems-programming (drivers, kernels, networking etc.) and build
large-scale user applications (Media Players, Photoshop, Game
Engines etc.)
• Rich library support: Has a rich library support (Both standard ~
built-in data structures, algorithms etc.) as well 3rd party libraries
(e.g. Boost libraries) for fast and rapid development.
Features of C++
• Speed of execution: Since, it is a compiled language, and also hugely
procedural. Newer languages have extra in-built default features such as
garbage-collection, dynamic typing etc. which slow the execution of the
program overall. Since there is no additional processing overhead like this
in C++, it is blazing fast.
• Pointer and direct Memory-Access: C++ provides pointer support which
aids users to directly manipulate storage address. This helps in doing low-
level programming (where one might need to have explicit control on the
storage of variables).
• Object-Oriented: One of the strongest points of the language which sets it
apart from C. Object-Oriented support helps C++ to make maintainable
and extensible programs. i.e. Large-scale applications can be built.
Procedural code becomes difficult to maintain as code-size grows.
• Compiled Language: C++ is a compiled language, contributing to its speed.
Applications
• C++ finds varied usage in applications such as:
– Operating Systems & Systems Programming. e.g. Linux-
based OS (Ubuntu etc.)
– Browsers (Chrome & Firefox)
– Graphics & Game engines (Photoshop, Blender, Unreal-
Engine)
– Database Engines (MySQL, MongoDB, Redis etc.)
– Cloud/Distributed Systems
Interesting Facts
• The name of C++ signifies the evolutionary nature of the
changes from C. “++” is the C increment operator.
• C++ introduces Object-Oriented Programming, not present in
C. Like other things, C++ supports the four primary features of
OOP: encapsulation, polymorphism, abstraction, and
inheritance.
• A function is a minimum requirement for a C++ program to
run.(at least main() function)
C++ Syntax
C++ Syntax
• Line 1: #include <iostream> is a header file library that
lets us work with input and output objects, such
as cout (used in line 5). Header files add functionality
to C++ programs.
• Line 2: using namespace std means that we can use
names for objects and variables from the standard
library.
• Line 3: Another thing that always appear in a C++
program, is int main(). This is called a function. Any
code inside its curly brackets {} will be executed.
• Line 4: ’cout’ is used with the insertion operator (<<)
to output/print text. In our example it will output
"Hello World".
C++ Syntax
• Note: Every C++ statement ends with a
semicolon ;
• Note: The body of int main() could also been
written as:
int main () { cout << "Hello World! "; return 0; }
• Remember: The compiler ignores white spaces.
However, multiple lines makes the code more
readable.
• Line 6: return 0 ends the main function.
• Line 7: Do not forget to add the closing curly
bracket } to actually end the main function.
Lets start
• Let's create our first C++ file.
• Open Codeblocks and go to File > New >
Empty File.
• Write the following C++ code and save the file
as myfirstprogram.cpp (File > Save File as):
C++ output
• The cout object, together with
the << operator, is used to output values/print
text:
Output:
Hello World!
I am learning C++
Comments
• Comments can be used to explain C++ code, and
to make it more readable. It can also be used to
prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined.
• Single-line comments start with two forward
slashes (//).
• Any text between // and the end of the line is
ignored by the compiler (will not be executed).
Comments
Output:
Hello World!
Multi line comments
• Multi-line comments start with /* and ends
with */.
• Any text between /* and */ will be ignored by
the compiler:
C++ variables
• Variables are containers for storing data values.
• In C++, there are different types of variables (defined
with different keywords), for example:
• int - stores integers (whole numbers), without
decimals, such as 123 or -123
• double - stores floating point numbers, with decimals,
such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char
values are surrounded by single quotes
• string - stores text, such as "Hello World". String values
are surrounded by double quotes
• bool - stores values with two states: true or false
Declaring (Creating) Variables
For all above three programs output is
15
Output:
10
OUTPUT:
11
C++ User Input
• we will use cin to get user input.
• cin is a predefined variable that reads data
from the keyboard with the extraction
operator (>>).
• In the following example, the user can input a
number, which is stored in the variable x.
Then we print the value of x:
C++ User Input
Output:
Remember !!!
• cout is pronounced "see-out". Used
for output, and uses the insertion operator
(<<)
• cin is pronounced "see-in". Used for input,
and uses the extraction operator (>>)
Activities
• Write a C++ program to create simple
calculator doing the following operations
– Addition
– Subtraction
C++ data types
• As explained in the Variables, a variable in C++
must be a specified data type:
Basic Data types
Activity
Numeric Data type
float vs. double
• The precision of a floating point value
indicates how many digits the value can have
after the decimal point. The precision
of float is only six or seven decimal digits,
while double variables have a precision of
about 15 digits. Therefore it is safer to
use double for most calculations.
• Scientific Numbers
• A floating point number can also be a
scientific number with an "e" to indicate the
power of 10:
Boolean data type
• A boolean data type is declared with
the bool keyword and can only take the
values true or false. When the value is
returned, true = 1 and false = 0.
Character Data type
• The char data type is used to store
a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
ASCII values