Chapter 3- Fundamentals of C++ programming Languages(4)
Chapter 3- Fundamentals of C++ programming Languages(4)
FUNDAMENTALS OF C++
PROGRAMMING LANGUAGES
TOPICS COVERED UNDER THIS CHAPTER
2.1 - A brief history of C++
2.2 - The structure of C++ program
2.3 - Input/output in C++
2.4 - Comments in C++
2.5 - C++ tokens (Keywords, Identifiers, Constants, and Operators)
2.5 Basic data types
2.6 - What is a variable?
2.0 - A BRIEF HISTORY OF C++
• HOME READING !!!
• Computer languages have undergone dramatic evolution since the
first electronic computers were built to assist in telemetry
calculations during World War II.
• Early on, programmers worked with the most primitive computer
instructions: machine language.
• These instructions were represented by long strings of ones and
zeros.
CONT…
• Soon, assemblers were invented to map machine instructions to
human-readable and manageable mnemonics such as ADD and MOV.
• In time, higher-level languages evolved, such as BASIC and
COBOL.
• These languages let people work with something approximating
words and sentences such as Let I = 100.
• These instructions were translated back into machine language by
interpreters and compilers.
CONT…
How C++ evolved
• Bjarne Stroustrup took the most popular language for commercial
software development, C, and extended it to provide the features
needed to facilitate object-oriented programming.
• He created C++, and in less than a decade it has gone from being
used by only a handful of developers at AT&T to being the
programming language of choice for an estimated one million
developers worldwide.
CONT…
How C++ evolved
• It is expected that by the end of the decade, C++ will be the
predominant language for commercial software development.
• While it is true that C++ is a superset of C, and that virtually any
legal C program is a legal C++ program, the leap from C to C++ is
very significant.
• C++ benefited from its relationship to C for many years, as C
programmers could ease into their use of C++.
CONT…
How C++ evolved
• As you may know, C++ was built upon the foundation of C. In fact,
C++ includes the entire C language, and (with minor exceptions) all
C programs are also C++ programs.
• When C++ was invented, the C language was used as the starting
point
What is C++?
C++ is a language used to program computers to perform specific tasks.
C++ Is not purely Object-oriented language, But A hybrid the
functionality of C programming language.
While it is true that C++ is a superset of C, and that virtually any legal
C program is a legal C++ program.
C++ is fun and easy to learn high level programming language.
C++ is used to create computer programs, and one of the most used
language in game development, today's operating systems,…
2.1 - BASIC STRUCTURE OF C++ PROGRAM
Here is our first program:
name of program LAB1.cpp. output
#include<iostream.h> Hello World!
int main()
{
cout<<“Hello World!";
return 0;
}
The left side shows the source code for our first program, which we
can name, for example, LAB1.cpp. The right side shows the result of
the program once compiled and executed.
CONT…
• It is one of the simpler programs that can be written in C++, but it
already includes the basic components that every C++ program has.
In the above code we used ‘\n’ and ‘endl’ to display strings on new lines.
CONT…
Given below is the additional program written in C++ which when
executed displays the letters “Hello World” on the display unit.
// First C+ + Program -----------------------Comment Line
#include<iostream.h> -----------------------Inclusion of header files
void main ( ) --------- Name of the main function or start of main () function.
{ ------------------------------------------------- Indicates beginning of main ()
cout<< “Hello World”; ------------- Executable output statement.
} ------------------------------------------------- End of main ()
Generally......
• C++ program is a collection of functions.
• The above example contains only one function, main ().
• As usual, execution begins at main ().
• Every c++ program must have a main ().
• The c++ statements terminate with semicolon.
2.2 Process of Compiling and Running programs
4.Linking
C++ programs contain references to functions defined elsewhere such as libraries.
Linker combines the object code compiled from your source code with the imported
library functions to produce an executable file.
Linker: a program that combines the object program with other programs in the library
and is used in the program to create executable code.
Contt…
5. Loading
Before a program is executed, it must be loaded from the disk into main
memory.
Loader takes executable file from the storage media and loads it into main
memory.
In this step, load the executable program into main memory for execution,
program called a loader accomplishes this task.
• Multi line comment: Multi line comment symbols are /*, */ the
following comment is allowed,
/* this is an example of C++ program to
illustrate some of its features */
Note :Comment lines in the program are ignored by the compiler.
CONT…
• All the lines beginning with two slash signs (//) are considered
comments and do not have any effect on the behavior of the
program.
• They can be used by the programmer to include short explanations
or observations within the source itself.
• In this case, the line is a brief description of what our program
does.
C++ IDES
IDEs (integrated development environments) is a tools available to developers.
These tools enhance developer’s workflows, reduce debugging time, and make
them more productive.
IDEs go beyond typical text editors by integrating compiling, code completion,
syntax highlighting, debugging, profiling, and testing and much more in one
comprehensive user interface.
There is a number of choices in IDEs, so focusing the best C++ IDEs good
practices.
E.g: Visual studio, Code::Blocks, Dev C++, Codelite, Clion, and Eclipse….
2.5 - C++ TOKENS
• C++ instructions are formed using definite symbols and words
according to some rigid rules known as syntax rules.
• Every program instruction must confirm correctly the syntax rules of
the programming language.
• The smallest individual unit of a program written in any language is
called a token(smallest piece of code understood by C++ compiler).
• keywords, identifiers, constants, variables, strings, comments, data
types, expressions, statements, operators ,brackets [ ], braces { }, and
parentheses ( ) are examples of basic elements of c++ programming/
tokens.
CONT…
Keywords
Syntax rules (or grammar) of C++ define certain symbols to have a
unique meaning within a C++ program.
These symbols (reserved words) can't used for any other purposes.
Keywords are pre-defined or reserved words in a programming language.
Each keyword is designed to perform a specific function in a program.
CONT…
Reserved word is a special word in a programming language that
cannot be used as a name.
Programmer cannot usually change the meaning of these words.
Since keywords are referred names for a compiler, they can’t be used
as variable names ,so trying to assign a new meaning to the keyword
which is not allowed.
All reserved words are in lower-case letters.
CONT…
For example, the following expressions are always considered
keywords according to the ANSI-C++ standard.
CONT…
Identifiers
• A valid identifier is a sequence of one or more letters, digits or
underscores symbols.
C++ identifier is a name used to identify a variable, function, class,
module, or any other user-defined item.
Identifiers in C++ are names of functions, names of arrays, names of
variables, and names of classes.
Valid variable identifiers should always begin with a letter. They can
also begin with an underscore character ( _ ) and can never begin with a
digit.
CONT…
• Neither spaces nor marked letters can not be part of an identifier.
Only letters, digits and underscore characters are valid.
• They cannot match from any keyword of the C++ language.
• Very important: the C++ language is "case sensitive", that means an
identifier written in capital letters is not equivalent to another one
with the same name but written in small letters. For example the
variable result is not the same as the variable RESULT or Result.
CONT…
The rules of naming identifiers in C++:
C++ is case-sensitive so that uppercase letters and lower case letters are different.
Ex Man and man are two different identifiers in C++.
The name of identifier cannot begin with a digit. however, underscore ( _ ) can be
used as first character while declaring the identifier.
Only alphabetic characters, digits and underscore ( _ ) are permitted in C++
language for declaring identifier.
Other special characters are not allowed for naming a identifier.
Keywords cannot be used as identifier.
EXAMPLES..
DATA TYPES
All variables use data-type during declaration, to restrict the type of data to be
stored. i.e. , we can say that data types are used to tell the variables the type of
data it can store in memory.
Whenever a variable is defined in C++, the compiler allocates some memory
spaces for that variable based on the data-type size.
Every data type requires/has a different amount of memory.
Data types in C++ is mainly divided into three types:
1. Primitive / fundamental data types
2. Derived data types
3. Abstract or user-defined data types
CONT…
Primitive data types: This data types are built-in or predefined data
types and can be used directly by the user to declare variables.
Example of basic fundamental data types are:
• Integer: They are the numbers without decimal part. ex: 69, 360, 32330.
• Character: Any letter enclosed within single quotes comes under character.
• Boolean : Used to store one of the two values. True/false
• floating point: They are the numbers with decimal point. ex: 69.65.
• double floating point
• valueless or void
• wide character: Character with 2/4 byte.
CONT….
The modifiers signed, unsigned, long, and short may be applied to character and
integer basic data types. However, the modifier long may also be applied to double.
Derived data types: are derived from the primitive or built-in data types are
referred to as derived data types. These can be four types namely:
• Function, Array, Pointer ,Reference
Abstract or user-defined data types: These data types are defined by user itself,
like, defining a class in C++ or a structure. EX:
• Class ,Structure ,Union ,Enumeration
CONT…
Summarize the basic fundamental data types in C++, as well as the range/size.
CONT…
Operators
• An operator is a symbol that tells the computer to perform certain
mathematical (or) logical manipulations.
• Operators used in programs to manipulate data and variables.
• They include:
1. Arithmetic operators. 5. Increment / decrement operators.
2.Relational operators –
• These are operators which relate the operands on either side of them
like less than (<), less than or equal (<=), equal to (==), greater
than (>), greater than or equal (>=) and not equal (! =).
• Used for comparing two or more numerical values.
== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of operator.
> returns true if left side is greater than right.
< returns true if left side is less than right side.
CONT…
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
CONT…
3. Logical operators – They are mainly used in conditional statements and
loops for evaluating a condition.
Like && (Meaning logical and), || (logical or), ! (logical not).
let’s say we have two Boolean variables b1 and b2.
b1&&b2 will return true if both b1 and b2 are true else false.
b1||b2 will return false if both b1 and b2 are false else true.
!b1 would return the opposite of b1, that means it would be true if b1 is
false and it would return false if b1 is true.
EXAMPLE
CONT…
4. Assignment operators: used to assign the result of an expression to a
variable and the symbol used is “=“ it is 3 types.
i. Simple assignment------------a = 9;
ii. Multiple assignment----------a = b = c = 6;
iii. Compound assignment------
a + = 15; (equal to a = a +15 ;)
b - = 5; (equal to b = b-5).
c * = 6; (equal to c = c * 6).
d / = 5; (equal to d = d/5;).
e % = 10; (divide e by 10 & store remainder
in e).
CONT…
Operators Output
//c++ code for assignment operators a=11
#include<iostream.h> b=1
int main() c=18
{ d=2
int a=9, b=4, c=6, d=8, e= 10; e=1
a+=2; b-=3; c*=3; d/=4; e%=3;
cout<<"a="<<a<<endl<<"b="<<b<<endl<<"c="<<c<<endl<<"d="<<d
<<endl<<"e="<<e;
}
CONT…
5. Auto increment / decrement (+ + / - -)
• Used to automatically increment and decrement the value of a
variable by 1. There are 2 types.
i. Prefix auto increment / decrement --- adds/subtracts 1 to the
operand & result is assigned to the variable on the left.
CONT…
ii. Postfix auto increment /decrement --- This first assigns the value to
the variable on the left & then increments/decrements the operand.
• When you define a variable in C++, you must tell the compiler what
kind of variable it is: an integer, a character, and soon.
• This declaration tells the compiler how much room to set aside and
what kind of value you want to store in your variable.
CONT…
Declaration of variables
• In order to use a variable in C++, we must first declare it i.e.
specifying the data types and name(the identifier).
Syntax: data type valid variable/ identifier;
•For example:
int a; int c,d,e; float mynumber;
double b_c;
Global variables are defined outside of all the functions, usually on top of the
program.
It hold their value throughout the lifetime of your program.
It can be accessed by any function.
CONT…
Initialization of variables: done by appending an equal sign
followed by value to which variable will be initialized (known as c-
like): Syntax: data type identifier = initial_value;
For example: int a = 5;
The other way to initialize variables, known as constructor
initialization, is done by enclosing the initial value between
parentheses (()): Syntax: data type identifier (initial_value) ;
For example: int a (5) ;
Both ways are valid and equivalent in C++.
N D
E