CH 2
CH 2
C++ Basics
Structure of C++ Program
A C++ program has the following structure
comments and do not have any effect on the behavior of the program.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor.
In this case the directive #include <iostream> tells the preprocessor to include the
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.
This line is very frequent in C++ programs that use the standard library
Namespaces are used to organize code into logical groups and to prevent name
collisions that can occur especially when your code base includes multiple
libraries.
int main ()
The main function is the point by where all C++ programs start their
execution, independently of its location within the source code.
The word main is followed in the code by a pair of parentheses (()). That is
because it is a function declaration
Right after these parentheses we can find the body of the main function
enclosed in braces ({}).
Cont..
cout << "Hello World!"; This line is a C++ statement.
cout is declared in the iostream standard file within the std namespace
is a software application that combines all of the features and tools
needed by a software developer.
C++ Program Development Process (PDP)
C++ programs typically go through six phases before they can be executed. These
phases are:
1. Edit: The programmer types a C++ source program, and makes correction, if
compilation
4. Linking: A linker combines the original code with library functions to produce
an executable code.
5. Loading: The loader loads the program from the disk into memory
These symbols, the reserved words, must not be used for any
other purposes
Example:- bool, case, break, auto, catch, char, delete, int, void, if,
…., float etc…
Identifiers
Program comments are totally ignored by the compiler and are only
intended for human readers
The most common way in which a program communicates with the outside world
is through simple, character-oriented Input/ Output (IO) operations.
Example
cin>>a;
cout<<“welcome to programming” ;
Variables
Variables are used for holding data values so that they can be utilized in
various computations in a program.
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
Variable Declaration
Declaring a variable means defining (creating) a variable
You create or define a variable by stating its type, followed by one or more
spaces, followed by the variable name and a semicolon.
The variable name can be virtually any combination of letters, but cannot
contain spaces and the first character must be a letter or an underscore
Example:-
int myAge;
You can create more than one variable of the same type in one statement by
writing the type and then the variable names, separated by commas.
For example:
int myAge, myWeight; // two int variables
The second line declares three individual long variables named area, width,
and length.
However keep in mind that you cannot mix types in one definition statement
Variable initialization
You assign a value to a variable by using the assignment operator (=)
int Width;
Width = 5;
You can combine these steps and initialize Width when you define it by writing
int width = 5;
Just as you can define more than one variable at a time, you can initialize more than one variable at
creation. For example:
int main ()
{
int g = 10; // Local variable declaration:
cout << g;
return 0;
}
Basic Data Types
When you define a variable in C++, you must tell the compiler what kind of variable
it is: an integer, a character, or …….
The varieties of data types allow programmers to select the type appropriate to the
needs of the applications being developed
Basic (fundamental) data types in C++ can be conveniently divided into numeric and
character types
Numeric variables can further be divided into integer variables and floating-point
variables
Integer variables will hold only integers whereas floating number variables can
accommodate real numbers
C++ data types and their ranges
Unsigned - positive
Short
Long
Type double, float
long double
Type char
#include <iostream.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0; 50
}
Arithmetic Operators
Logical operators
Conditional operator
Bitwise Operators
Assignment Operators (=)
a = 5;
The part at the left of the assignment operator (=) is known as the lvalue
(left value) and the right one as the rvalue (right value).
Modulo is the operation that gives the remainder of a division of two values.
For example, if we write:
a = 11 % 3; 2
Increase and decrease (++, --)
the increase operator (++) and the decrease operator (--) increase or
reduce by one the value stored in a variable c++;
c+=1;
c=c+1;
They are equivalent to +=1 and to -=1, respectively
Are all equivalent in its functionality: the three of them increase by one
the value of c
+)
The Operator ! is the C++ operator to perform the Boolean operation NOT
producing false if its operand is true and true if its operand is false
&& Operator
The operator && corresponds with Boolean logical operation AND
This operation results true if both its two operands are true, and false otherwise
a b A&&b
True True True
True False False
False True False
false False False
|| operator
This operation results true if either one of its two operands is true,
Thus being false only when both operands are false themselves
a b a || b
True True True
True False True
False True True
false False False
Conditional operator ( ? )
If condition is true the expression will return result1, if it is not it will return result2
Example:-
Bitwise OR ( | ) takes two numbers as operands and does OR on every bit of two numbers. The result of
OR is 1 if any of the two bits is 1.
Bitwise XOR (^ ) takes two numbers as operands and does XOR on every bit of two numbers. The result
of XOR is 1 if the two bits are different.
Left shift (<<) takes two operands, left shifts the bits of the first operand, the second operand decides the
number of places to shift.
Right shift (>>) takes two operands, right shifts the bits of the first operand, the second operand decides
the number of places to shift.