chapter Two
chapter Two
Programming I
Chapter Two
Introduction to C++
Programming
What is C++?
It is general purpose programming language. It
is also known as the mother of all programming
languages.
It Is a high level programming language
It is derived from the C program
Each statement or expression of C++ ends with
semicolon(;)
It Is case sensitive, execution begin from main
function
It has:
• Key words (+, cout, if….)
Showing Sample
program
Any meaningful program written in
C++ has to contain a number of
components:
the main function
some variable declarations and
some executable statements
Basics of C++
Structure of a program
// my first program in C+
+
#include <iostream>
using namespace std;
int main ()
{
cout<<"Hello World!";
return 0;
// my first program in C++
This is a comment line. All lines
beginning with two slash signs (//) are
considered comments and do not have
any effect on the behavior of the
program. The programmer can use them
to include short explanations or
observations within the source code
itself.
#include <iostream>
Lines beginning with a hash sign (#)
are directives for the preprocessor. They
are not regular code lines with
expressions but indications for the
compiler's preprocessor. In this case the
directive #include<iostream>tells the
preprocessor to include the iostream
standard file. This specific file (iostream)
includes the declarations of the basic
standard input output library in C++
using namespace std;
All the elements of the standard
C++ library are declared within
what is called a namespace, the
namespace with the name std. So
in order to access its functionality
we declare with this expression
that we will be using these entities.
Int main ()
This line corresponds to the beginning
of the definition of the main function.
The main function is the point by
where all C++ programs start their
execution, independently of its
location within the source code.
The c++ program must have a main
function
The word main is followed in the code
by a pair of parentheses (()). That is
because it is a function declaration:
the main function enclosed in braces
({}). What is contained within these
braces is what the function does when it
is executed.
Open brace ( { )- shows the start of a
program and close brace ( } )- shows
the end of a program.
cout<< "Hello World!";
In C++ this line is statement. A
statement is a simple or compound
expression that can actually
produce some effect. In fact, this
statement performs the only action
that generates a visible effect in
our first program
Cout represents the standard
output stream in C++ and it
declared the iostream standard
file within the std namespace
• Notice that the statement ends with
a semicolon character (;). This
character is used to mark the end of
the statement.
return 0;
The return statement causes the
main function to finish. return may
be followed by a return code (in our
example is followed by the return
code 0).
A return code of 0 for the main
function is generally interpreted as
the program worked as expected
without any errors during its
execution.
Comments
• Comments are parts of the source code
disregarded by the compiler. They simply
do nothing. Their purpose is only to allow
the programmer to insert notes or
descriptions embedded within the source
code. C++ supports two ways to insert
comments:
• // line comment
• /* block comment */
• The first of them, known as line
comment, discards everything from
where the pair of slash signs (//) is
found up to the end of that same line.
/* my second program in C++
with more comments */
#include <iostream>
using namespace std;
int main ()
{
cout<<"Hello World! "; // prints
Hello World!
cout<<"I'm a C++ programmer"; //
prints I'm a C++
programmer
Variables
In C++ a variable is a place to store
information. A variable is a location
in your computer's memory in
which you can store a value and
from which you can later.
A variable is a symbolic name for
a memory location in which data
can be stored.
All variables have two important attributes:
A type, which is, established when the variable is
defined (e.g., integer, float, character).
A value, which can be changed by assigning a new
value to the variable. The kind of values a variable
can assume depends on its type. For example, an
integer variable can only take integer values (e.g., 2,
100, -12) not float numbers like 0.123.
Declaration of variable
In order to use a variable in C++, we
must first declare it specifying which
data type we want it to be. The
syntax to declare a new variable is to
write the desired data type (like int,
bool, float...) followed by a valid
variable identifier.
Syntax:-
<Data type><identifier>;
For example:
int a;
// operating with variables
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << result;
// terminate the program:
return 0;
}
Output= 4
// initialization of variables
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a - b;
cout<< result;
return0;
}
Output=6
Operators
operators in C++ are mostly made of signs
that are not part of the alphabet but are
available in all keyboards
Assignment (=)
The assignment operator assigns a value to a
variable.
Example a = 5;
This statement assigns the integer value 5 to
the variable a
Example
// assignment operator
#include <iostream>
using namespace std;
int main ()
{
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
cout << "a:"<<a<<‘\n’;
cout << " b:" <<b;
return 0;
}
Arithmetic operators ( +, -, *, /,
%)
The five arithmetical operations
supported by the C++ language are:
+ addition /
division
- subtraction %
modulo
* multiplication
Operations of addition, subtraction,
multiplication and division literally
correspond with their respective
mathematical operators. The only one that
you might not be so used to see is modulo;
whose operator is the percentage sign (%).
Modulo is the operation that gives the
remainder of a division of two values. For
example, if we write:
a = 11 % 3; the variable a will contain the
value 2, since 2 is the remainder from
dividing 11 between 3.
Compound assignment (+=, -=,
*=, /=, %=, >>=, <<=, &=,^=, |
=)
When we want to modify the value
of a variable by performing an
operation on the value currently
stored in that variable we can use
compound assignment operators:
expression is equivalent to
value += increase; value = value +
increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
example:
// compound assignment operators
#include <iostream>
using namespace std;
intmain ()
{
inta, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout<< a;
return0;
}
Increase and decrease
Operators (++, --)
The auto increment (++) and auto
decrement (--) operators provide
adding and subtracting 1 from a
numeric variable.
Both operators can be used in prefix
and postfix form. The difference is
significant.
1. When used in prefix form, the
operator is first applied and the
outcome is then used in the
expression.
2. When used in the postfix form, the
For example a++;
a+=1;
a=a+1;
All of are equivalent in its
functionality: the three of them
increase by one the value of a.
Example 2
B=3;
A=++B;
// A contains 4, B contains 4
Example 3
B=3;
A=B++;
// A contains 3, B contains 4
Thank U!!!