Chapter Two - Basics of C++
Chapter Two - Basics of C++
Computer Programming
Basics of C++
OUTLINE
1. Structure of C++ Program
2. C++ IDE
3. Showing Sample program
4. Keywords, Identifiers, Inputs, Outputs,
Comments, Parts of a program
5. Data Types, Variables, and Constants
6. Operators
7. Precedence of Operators
3
Basics of C++ - History of C and C++
C programming language
◦ was first introduced by Denis Ritchie at the AT &T’s Bell
Laboratories USA in 1972
◦ Was implemented for the first time in DEC PDP-11 computer.
◦ Denise Ritchie used the concepts of BCPL and B to develop C and
added data typing and some other powerful features.
C++ programming language
◦ Began in 1979 when Bjarne Stroustrup was working in his Ph.D.
thesis.
◦ During that period Stroustrup used to work with Simula (regarded as
the first language to support OOP paradigm).
◦ So he began working on C with classes i.e. he started working on a
new language which would have object-oriented paradigm mixed
with the features of C programming language.
◦ He created C++, and the first commercial edition of C++
programming language was released in October 1985. 4
1.2 C++ IDE
To create C++ program we can use different IDE
such as
◦ Visual Studio Code
It is an open-source code editor designed for Windows, macOS,
and Linux.
It is developed by Microsoft
◦ Sublime Text
Sublime Text is a closed source cross-platform source code editor
◦ Dev C++
Dev C++ is another good IDE for C and C++ programming
languages.
It is an open source IDE but supports only Windows platform and
not Linux and OS X
◦ NetBeans 8, CodeLite, Eclipse
5
Structure of C++ Program
6
Structure of C++ Program
Note :- The blue number to the left is not a part of the program. Used for
to make discussing programs and researching errors easier.
Let's examine this program line by line:
Line 1: // my first program in C++
Two slash signs specify that the rest of the line is a comment inserted by the
programmer but which has no effect on the output of the program.
Programmers use them to include short explanations or observations about
the code or program.
7
Structure of C++ Program
9
Structure of C++ Program
Not all the lines of this program perform actions when the code is
executed.
◦ There is a line containing a comment (beginning with //). There is a
line with a directive for the preprocessor (beginning with #).
◦ There is a line that defines a function (in this case, the main function).
◦ And, finally, a line with a statements ending with a semicolon (the
insertion into cout), which was within the block delimited by the
braces ( { } ) of the main function.
12
Structure of C++ Program
13
Structure of C++ Program
14
Compiled and Interpreted Programming Language
15
Compiled and Interpreted Programming Language
Complied Program
◦ The source code must be transformed into machine
readable instructions prior to execution.
16
Compiled and Interpreted Programming Language
Interpreted language
◦ is a type of programming language for which most of its
implementations execute instructions directly and freely, without
previously compiling a program into machine-language instructions
17
Compiled and Interpreted Programming Language
Complied Language
VS
Interpreted Language
Complied Language Interpreted Language
C ,C++, Fortran, COBOL are compled Java and C# are interpreted language
language
18
Basic Input/Output
19
Basic Input/Output
20
Basic Input/Output
21
Basic Input/Output
Standard input(cin)
◦ In most program environments, the standard input by default is the
keyboard, and the C++ stream object defined to access it is cin.
◦ For formatted input operations, cin is used together with the
extraction operator, which is written as >> (i.e., two "greater than"
signs). This operator is then followed by the variable where the
extracted data is stored.
◦ For example:
int age;
cin >> age;
◦ Extractions on cin can also be chained to request more than one
datum in a single statement:
cin >> a >> b;
◦ This is equivalent to:
cin >> a;
cin >> b;
22
C++ Keywords
Keywords are words that are reserved for the language
Since they are used by the language, these keywords are
not available for re-definition or overloading or used a
name for other things
List of keywords in C++
◦ alignas, alignof, and, bool, break, case, catch, char,
const, constexpr, default, delete, do, double,
dynamic_cast, else, enum, explicit, export, extern,
false, float, for, goto, if, inline, int, long, return, short,
signed, sizeof, static, static_assert, switch, template,
this, throw, true, try, union, unsigned, using, virtual,
void etc…
23
C++ Variable
Variables are the backbone of any programming
language.
A variable is simply a way to store some
information for later use.
It is a portion of memory to store a value.
We can retrieve this value or data by referring to a
"word" that will describe this information.
Once declared and defined they may be used many
times within the scope.
Each variable needs a name that identifies it and
distinguishes it from the others
24
Variable Identifier
A valid variable identifier is a sequence of one or more
letters, digits, or underscore characters (_).
Spaces, punctuation marks, and symbols cannot be part of an
identifier.
In addition, identifiers shall always begin with a letter. They
can also begin with an underline character (_), but such
identifiers are -on most cases- considered reserved for
compiler-specific keywords or external identifiers,
In no case can they begin with a digit.
Very important: The C++ language is a "case sensitive"
language.
◦ For example: Name and name are two different identifiers
25
Data type in C++
Variables are stored somewhere in an unspecified location in
the computer memory.
Our program does not need to know the exact location
where a variable is stored.
What the program needs to be aware of is the kind of data
stored in the variable.
It's not the same to store a simple integer as it is to store a
letter or a large floating-point number;
Specifying the types of information stored in the variable is
done by declaring variable with data type
Ex
◦ int minNum;
◦ char name;
26
Data type in C++
Fundamental data types are basic types implemented directly by the
language that represent the basic storage units.
They can mainly be classified into:
◦ Character types:
represent a single character, such as 'A' or '$'.
The most basic type is char, which is a one-byte character. Other types are also
provided for wider characters.
◦ Numerical integer types:
store a whole number value, such as 7 or 1024.
They exist in a variety of sizes, and can either be signed or unsigned, depending on
whether they support negative values or not.
◦ Floating-point types:
They can represent real values, such as 3.14 or 0.01, with different levels of
precision, depending on which of the three floating-point types is used.
◦ Boolean type:
The boolean type, known in C++ as bool, can only represent one of two
states, true or false.
27
Data type in C++
Group Type names* Notes on size / precision
unsigned char
unsigned short int
Integer types (unsigned) unsigned int (same size as their signed counterparts)
unsigned long int
unsigned long long int
float
◦ int a;
◦ float mynumber;
◦ int a, b, c;
29
Variable Declaration and Initialization
31
Constants
32
Constants
Floating-point Literals
◦ 3.14159 // 3.14159
◦ 6.02e23 // 6.02 x 10^23
◦ 1.6e-19 // 1.6 x 10^-19
◦ 3.0 // 3.0 .
◦ 3.14159L // long double
◦ 6.02e23f // float
Character and string literals
◦ Character and string literals are enclosed in quotes:
◦ 'z‘
◦ 'p‘
◦ "Hello world"
◦ "How do you do?"
33
Constants
Other literals
◦ Three keyword literals exist in C++: true, false and nullptr:
◦ true and false are the two possible values for variables of type bool.
◦ nullptr is the null pointer value.
bool foo = true;
bool bar = false;
int* p = nullptr;
Typed constant expressions
◦ Sometimes, it is just convenient to give a name to a
constant value:
const double pi = 3.1415926;
const char tab = '\t';
◦ We can then use these names instead of the literals they
were defined to:
35
Operator in C++
Arithmetic Operators
Assume variable X has =20 and Y=10
36
Operator in C++
Relational Operators
Assume variable A holds 10 and variable B holds 20, then
Operator Description Example
== Checks if the values of two operands are equal or not, if yes (A == B) is not
then condition becomes true. true.
!= Checks if the values of two operands are equal or not, if (A != B) is true.
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of (A > B) is not
right operand, if yes then condition becomes true. true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not
the value of right operand, if yes then condition becomes true. true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
37
Operator in C++
Logical Operators
Assume variable A holds 1 and variable B holds 0, then
Operator Description Example
38
Operator in C++
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The
truth tables for &, |, and ^ are as follows
Assignment Operators
There are following assignment operators supported by C++
language
Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left C = A + B will assign value of A + B
side operand. into C
+= Add AND assignment operator, It adds right operand to the left operand and
assign the result to left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left
operand and assign the result to left operand. C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left
operand and assign the result to left operand. C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right
operand and assign the result to left operand. C /= A is equivalent to C = C / A
41
Operator in C++
44