Pamir Us First Semester 1
Pamir Us First Semester 1
C/C++
By: Ashiqullah “DANISH”
Pamir University First Semester
Introduction to programming
Introduction
You know that computer is one of the most advanced and fast growing technologies in the world. It is an electronic
device that receives, stores, retrieves and process information. Different computer languages are used for
communication between users and computers. It defines the rules under which programs are written to make use of
computers. Hundreds of computers languages are in use today, C is one of them, which is an excellent choice for
any data processing task.
Languages
Language is a source of communication among people/ among human beings.
Programming languages
These are the languages through which the human beings communicate with the computer.
Programming language is a vocabulary and set of grammatical rules for instructing a computer to perform
specific tasks.
For writing programs, programmers use special languages called programming languages.
Examples are C, C++, COBOL, PASCAL, BASIC, FORTRAN etc.
Programmers
Those who make/write programs are known as programmers or coders.
System expert
Those who know machine language is called system expert.
Assembly language
It is also called Second Generation Language 2GL.
The computer professionals developed one step higher language than the machine
language which is called Assembly language. The binary codes of machine language
(0’s and 1’s) were replaced by some special codes called mnemonics. These
mnemonics are understandable by the human but not understandable by the
machine. To make it understandable for the machine, a program called assembler is
developed its function is to translate a program written in assembly language into
machine language
It is machine dependent. A program created on one computer cannot run on another
type of computer.
High level languages
These languages are near to human and far from computers.
We can learn and understand these languages very easily. Special
programs like compilers are used to convert these languages into
machine languages and enable the programmer to write instructions
easily using English words and familiar mathematical symbols such as
if else, for, goto, +, - etc.
Examples are BASIC, FORTAN, C, C++, COBOL, ADA, and PASCAL
The high level languages are of two types:
Procedural languages/Structured languages.
OOP (Object Oriented Programming) languages
Procedural language
A Procedural language is that in which each statement tells the computer to do
something. Get some input and process the input and provide the output to the user.
OOP
In this language different objects are created from each other and then used in
different functions.
Some common high level languages detail is given below
FORTRAN
FOR for formula
TRAN for translation
It is used to solve formula having problems i.e. Math's, Physics, Chemistry etc.
It is developed by John Backus and his team at IBM (International Business Machine).
It is a language that’s been around for nearly 40 years and is still used extensively for scientific and engineering
calculations, although C++ and other languages have eroded much of its usage.
COBOL
Common business oriented language.
It was released in 1960. The leader of the team that developed COBOL was will naval officers Grace Hopper. It
was used for solving business problems.
CPL
Combined programming language
It solves both formula+ business problem
BCPL
Basic Combined Programming Languages
BCPL was developed by Martin Richards at Cambridge University in 1967, for writing operating systems and
compilers.
It both solve formula problems (Math, Physics etc.) and as well as Business problem.
B-languages
It is designed by Kenneth Thompson. It is developed in AT and TS Bell LAB in
USA.
(AT and TS = American Telephone and Telegraphs). Therefore, it is called B-
Language. Bell starts from B. B programming language had advance features as
compared with BCPL. The UNIX operating system was first developed in B in
1970 at Bell Laboratory. Both BCPL and B were “type-less” languages. Thus each
data item in these languages occupied one “word” in the memory.
It is more advanced than other language like FORTRAN, COBOL) etc.
C-language
C is a programming language developed BY Dennis Ritchie at AT and T's Bell Laboratories of USA in 1972.
The C language was the advanced version of B language. It had many important concepts of BCPL and B. New
features, like data typing and many other functions were also included in C.
The next version of UNIX was revised and written in C. C language was developed as a computer independent
language. A program written in C can be run on any computer.
Source code
A computer program written in a high level language is called the source code or
source program. It is a human understandable form not computer understandable
form.
Source files, which have the filename extension.cpp/C, contain function definitions.
Any definitions for data type you have created yourself that are required for the code
in a .cpp/C file are added by including the contents of one or more .h files at the
beginning of the.cpp file.
Object code
A program that is created by the language processor in the form of codes is called Object program.
A program that is converted in to object language (Machine) is called object program.
A computer program in the machine language is called the object code or object program. A
program written in machine language runs directly on the computer and no need for translator
(compiler, interpreter).
Executable Program/code
A program that is created by language processor to show or provide the output of a
source program.
A program that is directly run or executes to provide results of the input.
When source and object program are linked that’s create a new executable program
is called exe program.
Brief history of C++ Language
History of C++
In early 1980, C++ was developed by Bjarne Stroustrup. It was an extension of C. it was designed for
the UNIX system environment. C++ enabled programmers to improve the quality of code. It included
object oriented programming.
Its name was changed to C++ later in the same year. The C++ name was suggested by Rick
Mascitti. It was used in December of 1983 when it was edited into the final copies of
[Stroustrup 1984] and [Stroustrup 1984c]. C++ is pronounced C plus plus.
The language is not called D, because it is an extension of C. it is not arranged alphabetically, as C-
Language after B-language
++ is the increment operator of C, it means added a new feature to C called classes, therefore, given a
name C++. (i.e. C + Classes means classes=+ (C++)), this ‘C+’ is wrong syntax. C++ is correct.
C++ was not commercially until the late part of 1985.
C++ implements data abstraction using a concept of called classes, along with other features to allow
object-oriented programming and is considered a High level Language.
Classes help programmers with the organization of their code. They can also be beneficial in assisting
programmers to avoid mistakes.
The original C++ compiler, called Cfront, was written in the C++ programming language.
History
• C is a programming Language which is developed during 1970 by Dennis Ritchie . In
the case of this language the data was not secured.
• C Language is not an object oriented language it is a procedural oriented language.
Now what is Object Oriented Language it will be discuss later.
• In C programming, a program can be divided into smaller programs called functions.
And it doesn't support Function overloading.
• C++ is also called as C with classes or we can say C++ is a C language with more
features.
History
•C++ is a High-level programming language developed by
BJARNE STROUSTRUP starting in 1979 at Bell Labs.
originally named C with Classes but later it was renamed C++
in 1983.
C++ runs on a variety of platforms, such as Windows, Mac
OS, and the various versions of UNIX
What is compiler?
• Computers understand only one language and that language consists
of sets of instructions made of ones and zeros. This computer
language is appropriately called machine language.
• A single instruction to a computer could look like this: 00011101010101
• IDEs are some tools which are use to write code for C++ for
example
DEV C++, Turbo C++, Visual Studio, Eclipse and
etc.
• The Dev C++ is much famous for learning C++ that is why we
use the Dev-C++ IDE (Integrated Development Environment)
• Dev-C++ has its own compiler which is responsible for the
conversion of your code into machine language.
First Program In C++
• Each and every language will provide this great feature which is used to
document source code. We can create more readable and eye catching
program structure using comments. We should use as many as comments
in C++ program. Comment is non executable Statement in the C++.
• We can have two types of comment in Programming
Single Line Comment
Multiple Line Comment
#include<iostream>
using namespace std;
//Ashiqullah Danish Computer Programmer
main(){
}
C++ supports the following data types:
• 1. Primitive Data Types : These data types are built-in or predefined data
types and can be used directly by the user to declare variables. example: int, char,
float, bool, etc. Primitive data types available in C++ are:
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
Derived Data Types
• Derived data types: that are derived from the primitive or built-in datatypes
are referred to as Derived Data Types. These can be of four types namely:
Function
Array
Pointer
Reference
Abstract or User-Defined Data Types
• Abstract or User-Defined Data Types: are defined by the user itself. Like, defining a class
in C++ or .a structure. C++ provides the following user-defined datatypes:
Class
Structure
Union
Enumeration
Data Types & Variables
• While doing programming, you need to use variables to store information.
Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in memory.
• You may like to store information of various data types like character,
wide character, integer, floating point, double floating point, Boolean
etc. Based on the data type of a variable, the operating system allocates
memory and decides what can be stored in the reserved memory.
Data Types & Variables
• Data Types means characteristic of a data. Bellow are some of the data types
in C++,
Type Keyword Size reservation
Boolean Bool 1 byte
Character char 1 byte
Short integer short int 2 bytes
Integer int 4 bytes
Floating point float 4 byte s
Double floating point double 8 byte s
Variable and Assignment
Assignment is a process through which we store some values to a variables. Assigning
value to a variable need two process
1. Variable Declaration
2. Variable initialization
What is variable Declaration?
Variable Declaration tell the system you have allocate a space by a specific name.
To declare a variable you need two things. Data Type and variable name (identifier).
For example int var1; char var2; double x; string y;
Variable and Assignment
What is variable initialization?
Variable initialization means assigning values to variable, it is the way how you
can insert values to the variable. For example
int x; //variable declaration
x=90; // variable initialization
Or
int x=80; //variable declaration + variable initialization
Examples
We can declare and initialize a variable through different ways
Constant are like variable, but once you assign a value to a constant variable it is not
changeable. Where the variables are changing their values during the execution of a
program.
Example
const int x=90;
x=80; // this is an error
cout<< x;
Character
Character is a single alphabet which takes only one byte in computer memory and it
is always define as bellow.
Syntax char variable_name;
Example char x;
x=‘Z’;
cout<<x;
Strings
String is the collection of characters, it is also called non-primitive data type
and it assume storage size depends on the character of string.
Syntax string variable_name;
Example string student_name;
student_name=“Kamal”;
cout<<student_name;
cin/cout
cin and cout are not keywords, as you are beginner
now in this class just consider them as a keyword.
cout means console out. It is use to print something
on the screen of your system. We use the extraction
operator(<<) with cout. Reverse cin is use for input
when you want to take something from the user
through keyboard. We use the insertion operator
(>>) with cin .
Example
Operator
Operator is a special symbol that tells the compiler to perform specific
mathematical or logical Operation.
The increment operator (++) adds 1 to its operand, and the decrement
operator (--)subtracts 1 from its operand.
x = x+1; is the same as x++
x = x-1; is the same as x--
Both the increment and decrement operators can either Prefix or Postfix.
There is an important difference in prefix and postfix forms which will be
discuss later
The if, if...else and nested if...else statement are used to make one-time
decisions in C++ Programming, use to execute some codes and ignore some
codes depending upon the test condition.
if Statement
The if statement checks
whether the test condition is
true or not. If the test
condition is true, it executes
the code/s inside the body of
The if keyword is followed by test condition inside
if statement. But if the test parenthesis ( ). If the test condition is true, the codes
condition is false, it skips the inside curly bracket is executed but if test condition is
false, the codes inside curly bracket { } is skipped and
code/s inside the body of if control of program goes just below the body of if as
statement. shown in figure above
Flow Chart of if Statement
Else if Statement
This statement is executed when if
statements become fail. So the compiler
checks for the else if statement. Same if the
first else if statement become fail again it
will check for the third else if statement,
same action will be perform. If none of the
if and else if statement become true then the
last bock (else block) will be execute.
Else if flow Chart
Nested if Statement
When ever an if statement comes
inside of an if statement, it is
called Nested if statement. It
means this has two test condition
for execution of a program.
See the example
Nested if Flow Chart
C++ Switch Statements
• Use the switch statement to select one of many code blocks to be executed.
• Syntax
• switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Switch
A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked
for each case.
Switch Rules
• The expression used in a switch statement must have an integral or
enumerated type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
Switch Rules
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow
of control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed
in the default case.
Example
case 'f':
cout<<"I am Female ";
break; //if here not write a break then the default statement also execute
default:
cout<<"You have selected a wrong choice";
}
cout<<"thank you";
}
• This is how it works:
• The switch expression is evaluated once
• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
• The break and default keywords are optional, and will be described
later in this chapter
• The example below uses the weekday number to calculate the
weekday name:
• Example
• int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
Loops
• When you need to execute a block of code several Body of loop
number of times we use some statements which are
called loops statement. In fact loop statement
allows you to execute a statement or a group of
statement multiple time. True
Condition
• following is the general form of a loop statement in
most of the programming language. False
While loop
• A while loop statement repeatedly
executes a target statement as long
as a given condition is true
• Syntax:
While (condition ){
Statements
}
Program
main(){
int a=0;
while(a<10){
Cout<<“Welcome to Pamir University”;
a++;
}
Flow Chart
For Loop
• A for loop is a repetition control structure
that allows you to efficiently write a loop
that needs to execute a specific number of
time.
• Syntax:
for(initialization ; condition ; increment){
Statements;
}
Program
main(){
int i;
for(i=0; i<10; i++){
cout<<“ Welcome to First Semester”;
}
Do… While loop Flow Chart
main(){