Chapter Two
Chapter Two
Chapter Two
C++ Basics
1
4/13/2023
Cont’d….
▪ C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New
Jersey, as an enhancement to the C language and originally named C with Classes but later it
was renamed C++ in 1983.
▪ C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
▪ The most important facilities that C++ adds on C language are C++ fully supports
object-oriented programming, including the four pillars of object-oriented development:
❖ Encapsulation
❖Data hiding
❖Inheritance
❖Polymorphism
❖Addition of namespaces and STL (Standard Template Library) and Many new keywords.
• The C++ programming language became popular soon after it was officially
launched.
(iii) Customer service software such as electronic ticketing, car and flight reservations, etc.
2
4/13/2023
3
4/13/2023
• These functions are included in the program by including the header files
which contain these functions
• There are a number of files called header files and functions in C++ Standard
Library which help in the execution of the programs.
• Therefore, every program has to include the header files which are required
by the program.
• C++ program should necessarily contain the header file <iostream> which
stands for input and output stream used to take input with the help of
“cin>>” function and display the output using “cout<<” function.
7
4
4/13/2023
▪ Note that C++ provides the flexibility of writing a program with or without a class
and its member functions definitions.
▪ A simple C++ program (without a class) includes comments, headers, namespace,
main() and input/output statements.
The parts of a C++ Program
#include <iostream>
1. Using namespace std;
2. int main ()
3. {
4. cout << "Hello World\n";
5. return 0;
6. }
Any C++ program file should be saved with file name extension .cpp
10
5
4/13/2023
▪ The first line of the program is #include <iostream.h> in which the symbol
# is a pre-processor directive.
▪ The statement directs the compiler to include the header file<iostream.h> (input/output
stream) from C++ Standard Library in this program.
▪ The cin is used along with extraction operator(>>) for input and cout is used along with
insertion operator(<<) for output.
▪ The name of a header file when included in a program is enclosed between a pair of
angular brackets < >
int main( )
▪ The third line of the above program (int main()) indicates the beginning of the
program.
▪ In C++ the parentheses ( ) are used to indicate that the identifier or name on its
left is a function.
▪ In the present case the last line of the program reads return 0;
6
4/13/2023
Cont’d..
• the body of program enclosed in the curly braces{}.
• These braces generally contain expressions, statements and operators with a purpose to
carry out some action on the data presented to the program.
• Expressions are the conditions if fulfilled the statement following the expression is carried
out.
• In the program sample you will observe that all the statements in a C++ program end with
a semicolon ;
• The forth line is a statement. A statement is a computation step which may produce a
value.
• This brace ( } ) marks the end of the body of main.
13
• Program comments are text totally ignored by the compiler and are only intended to inform the
reader how the source code is working at any particular point in the program.
• Single Line Comment: Anything after // {double forward slash} (until the end of the line on which it
appears) is considered a comment. Eg:
• Multiple Line Comment: Anything enclosed by the pair /* and */ is considered a comment. Eg:
/*this is a kind of comment where
Multiple lines can be enclosed in
one C++ program */
14
7
4/13/2023
• Division by zero
• Syntax errors
• C++ has the following tokens: Variables, Identifiers, Keywords, Constants, function,
Strings, and operators.
• A variable is an identifier that denotes a storage location used to store a data value. A
variable can be defined anywhere in the program. All variables have three important
properties:
• Data Type: a type which is established when the variable is defined. (e.g. integer, real,
character etc).
• Data type describes the property of the data and the size of the reserved memory.
• Name: a name which will be used to refer to the value in the variable. A unique identifier
for the reserved memory location
• Value: a value which can be changed by assigning a new value to the variable.
16
8
4/13/2023
Variables
• A variable is a name given to a memory location. It is the basic unit of storage in a
program.
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done on the
variable effects that memory location.
• In C++, all the variables must be declared before use.
Declaration of variables
• Certain words are reserved by C++ for specific purposes and cannot be used as
identifiers.
• These are called reserved words or keywords and are summarized in the following
table.
These symbols, the reserved words, must not be used for any other purposes.
18
9
4/13/2023
19
Data types
• The data type specify the size, and type of values that can be stored.
• Data type specifies the size and type of values that can be stored.
10
4/13/2023
Boolean bool
Character char
Integer int
Valueless void
➢ Both the numeric data types offer modifiers that are used to vary the nature of the data
to be stored. The modifiers used can be short, long, signed and unsigned.
➢ Several of the basic types can be modified using one or more of these type modifiers −
• signed
• unsigned
• short
• long
• The following table shows the variable type, how much memory it takes to store the
value in memory, and what is maximum and minimum value which can be stored in such
type of variables.
22
11
4/13/2023
23
24
12
4/13/2023
13
4/13/2023
27
Identifiers
▪ Identifiers refer to a unique combination of letters and digits, either short or descriptive,
that are used to identify a variable, method, class or any other object in a programming
language uniquely.
▪ The identifiers in C++ can have identifiers with any length of characters.
▪ These characters can be alphanumeric i.e. can contain letters, digits and underscores and
whitespaces and special characters such as @,#,!etc are not allowed.
▪ These identifiers must start with a letter or an underscore and are case sensitive thus
keywords must not be used as an identifier.
• Identifiers are used to represent various objects such as:-
1.Constants
2.Variables
3.Functions
4.Labels
5.Defined Data Types
28
14
4/13/2023
✓Valid Identifier
Invalid Identifiers:
30
15
4/13/2023
Initializing Variables
• When a variable is assigned a value at the time of declaration, it is called variable initialization.
• This is identical with declaring a variable and then assigning a value to the variable
immediately after declaration.
• Direct initialization:
Datatype var_name;
var_name=value;
Datatype var_name;
cin>> var_name;
31
16
4/13/2023
Scope of Variables:
Scope of a variable is the boundary or block in a program where a
variable can be accessed.
The boundary or block is identified by the left and right French brackets
{}.
Global variables: are variables that can be referred/accessed anywhere
in the code, within any function, as long as it is declared first. A variable
declared before any function immediately after the include statements
are global variables.
Local Variables: the scope of the local variable is limited to the code
level or block within which they are declared.
33
• When the compiler finds such values (characters), it translates back the value
to the ASCII values. E.g. ‘a’ has a value 97 in ASCII.
• Special Printing characters.
• In C++, there are some special characters used for formatting.
34
17
4/13/2023
35
• E.g: x = a + b;
36
18
4/13/2023
Operators
• An operator is a symbol that makes the machine to take an action.
• Different Operators act on one or more operands and can also have different
kinds of operators.
• Comma operator, The size of operator, casting operators, bitwise operator etc.
37
Assignment Operators
• The assignment operator causes the operand on the left side of the assignment statement
to have its value changed to the value on the right side of the statement.
• Syntax: Operand1=Operand2;
38
19
4/13/2023
▪ Generally, if both operands are integers then, the result will be an integer.
▪ However, if one or both operands are real then the result will be real.
▪ When both operands of the division operator (/) are integers, then the division is
performed as an integer division and not the normal division we are used to.
▪ Division of integer by integer will not round off to the next integer
39
40
20
4/13/2023
Relational Operators
▪ Relational operators evaluate to 1 (representing the true outcome) or 0 (representing the
false outcome). ‘A’ < ‘F’ would return true or 1. it is like (65 < 70)
41
Logical Operators
• The relational operators, logical operators evaluate to 1 or 0.
• N.B. In general, any non-zero value can be used to represent the logical true, whereas
only zero represents the logical false.
42
21
4/13/2023
43
44
22
4/13/2023
Bitwise Operators
In C++, bitwise operators perform operations on integer data at the individual bit-level. These
operations include testing, setting, or shifting the actual bits
Example:
a&b; Operator Description
a|b; & Bitwise AND Operator
Here is a list of 6 bitwise | Bitwise OR Operator
operators included in C++
^ Bitwise XOR Operator
45
46
23
4/13/2023
47
48
24
4/13/2023
When we shift any number to the right, the least significant bits are
discarded, while the most significant bits are replaced by zeroes.
49
For example:
50
25
4/13/2023
• If the result of the evaluation is non zero (which means true), then operand2 will be the
final result. Otherwise, operand3 is the final result.
• E.g.: General Example Z=(X<Y? X : Y) This expression means that if X is less than Y the
value of X will be assigned to Z otherwise (if X>=Y) the value of Y will be assigned to Z.
• E.g.: int m=1,n=2,min; min = (m < n ? m : n); The value stored in min is 1.
• E.g.: a = sizeof(char)
• b = sizeof(int)
• c = sizeof(1.55)
52
26
4/13/2023
Operator precedence
• The order in which operators are evaluated in an expression is significant and
is determined by precedence rules.
• Precedence Table:
53
54
27
4/13/2023
1. Write a C++ program that outputs the following text on screen: Hint use endl or
‘\n’
Oh what
a happy day!
Oh yes,
2. Write a C++ program to prompt the user to input her/his name and print this
name on the screen, as shown below.
The text from keyboard can be read by using cin>> and to display the text on the
screen you can use cout<<Hello Programmers!.
55
56
28
4/13/2023
57
58
29
4/13/2023
End
59
30