In the Name of Allah The Most Merciful The Most Compassionate
Programming In
C++
History of Programming
1st Generation Languages
Machine Language
2nd Generation Languages
Assembly Language
3rd Generation Languages
Procedural Programming Languages like
Basic, Pascal, Fortran, C etc.
4th Generation Languages
OOP e.g. Java ,C++, VB.
C++ History
C was developed at AT & T‟s Bell
Laboratories of USA in 1972 by Dennis
Ritchie
C++ an extension of C was developed
by Bjarne Stroustrup in early 1980s at
Bell Laboratories. C++ provides
capabilities for object-oriented
programming.
Getting Started
Alphabets Words Sentences Paragraph
English Language
Alphabets, Constants,
Digits, Program/
Variables, Instructions
Special- Code
Keywords
Symbols
C++ Language
Structure of C Program
# include < iostream.h > Preprocessor Directives
int main ( ) Main Function
{
cout << “Welcome to C++” ;
Function Body
return 0;
}
Preprocessor Directives
The first line of the program
# include < iostream.h >
is called a Preprocessor Directive,
The instruction that are given to the compiler
before beginning of the actual program.
It begins with the # sign and a keyword
(reserve word) “include” followed by
< name of header file>.
Header Files
Header files are a part of the C++ Compiler and
contain definitions of standard library functions.
Each header file has an extension “.h”. The
preprocessor “include” is used to add a header file
into the source code. The name of the file is written
in angle brackets ”< >”. Syntax is
# include < name of header file>
# include “ name of header file”
For example;
iostream.h :The source file that contains
declarations needed for input and output
statements like cout/cin.
math.h : This header file contains definitions for
mathematical functions
Functions
Functions are the fundamental building
blocks of C++. The first program consists of a
single function called main ( ) .
The Main() Function
The main function indicates the beginning of a
C++ program. It must be included in every
program.When a C++ program is executed, the
control goes directly to main () function.The
statements within this function are the main
body of a program.If the main function is not
included, the program is not compiled and an
error is generated.
Syntax of the main() function is:
int / void main ()
{
body of main function
(program statements) ;/*instruction to do
something*/
Terminated by a semi colon “;”
}
/*Program statements ends with
semicolon and always are a part of
function*/
The Function Body
Braces: The braces ( Curly brackets “{ }“ )
indicate Begin and End of a function. Every
function must use this pair of braces.
Program Statements: The basic unit of a C++
program. In our program we‟ve only one
program statement i.e.
cout << “Welcome to C++” ;
This statement tells the compiler to display the
quoted phrase. A semicolon (;) indicates the
end of the statement. Each statement has
to be followed by a semicolon otherwise, a
compilation error will occur.
OUTPUT USING cout
cout ( pronounced as “ C out” ) is
predefined in C++, called as Standard
Output Stream. It follows data to be
displayed.
The operator << is called the Insertion or
Put to operator.
In our example, the phrase “Welcome to C++”
is an example of a String Constant or String
Literal, is directed to cout by the insertion
operator << and is to be displayed on the
output.
Whitespaces
Tabs,spaces,linefeeds
Ignored by compiler
Invisible to compiler
The endl Manipulator
Manipulators are instructions to the output
stream that modify the output in various ways.
This causes a linefeed to be inserted into the
stream, so that the subsequent text is
displayed on the next line. It has the same
effect as the „\n‟ character.
cout<<"PAKISTAN”<<endl;
cout<<“OUR HOMELAND"<<endl;
cout<<“PAKISTAN\nOUR HOMELAND\n”;
Comments
A comment statement is a non-executable
statement. It is used to add remarks in a
program.The compiler ignores the comments
given in the program.Comments are used to
explain, what a program is doing. Comments
can be written anywhere in a program.
Syntax:
// comments for single-line comments
/* comments…… for multi-line comments
……..*/
# include<iostream.h>
int main() //beginning of main function
{
cout<<“NU ”; //printing outputs
return 0;
}
Output ???
NU
# include<iostream.h>
void main()
{
/*This is a program to find errors
cout<<“\n NUST ;
cout<<“\n PAKISTAN”
return 0;
}
//end of program
Errors ???
Variable
The variable is the basic unit of storage
in a program.
A variable is defined by the combination
of an identifier, a data type, and an
optional initializer.
Identifier
An identifier is a sequence of characters that
consist of letters, digits, underscores (_).
An identifier must start with a letter, an
underscore (_). It cannot start with a digit.
An identifier cannot be a reserved word.
An identifier can be of any length.
Data type
– Integers: This group includes short(2b), int,(4b) and long,
(8b)which are for whole valued signed numbers.
– Floating-point numbers: This group includes float(4b) and
double,(8b) which represent numbers with fractional
precision (decimal numbers).
– Characters: This group includes char, which represents
symbols in a characterset, like letters and numbers(1)
– Boolean: This group includes boolean, which is a special
type for representing true/false values.
Integer
Floating point number
Declaring Variables
type identifier ;
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Assignment Statements &
(=)operator
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
Declaring and Initializing
in One Step
int x = 1;
double d = 1.4;
float f = 1.4;
# include<iostream.h>
int main() //beginning of main function
{
int age;
cout<<“enter ur age plz”;
cin>>age;//extraction operator
cout<<“ur age is”<<age<<end1;
return 0;
}
Output ???
ur age is 22
Discussion Topics
• Operator Types
• Arithmetic Operators
+, -, *, /, and %
• Shortcut Assignment Operators
+= , -=, *=, /=, %=
• Increment Decrement Operators
++, --
• Comparison Operators or relational operators
<, <=, >, >=,==,!=
• Boolean or logical Operators
• &&, ||, !,
• Operator Precedence
Arithmetic Operators
+, -, *, /, and %
5/2 yields an integer 2.
5.0/2 yields a double value 2.5
5 % 2 yields 1 (the remainder of the division)
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
Operator Example Equivalent
+= i+=8 i = i+8
-= f-=8.0 f = f-8.0
*= i*=8 i = i*8
/= i/=8 i = i/8
%= i%=8 i = i%8
suffix
x++; // Same as x = x + 1;
prefix
++x; // Same as x = x + 1;
suffix
x––; // Same as x = x - 1;
prefix
––x; // Same as x = x - 1;
int i=10; Equivalent to
int newNum = 10*i;
int newNum = 10*i++;
i = i + 1;
int i=10; Equivalent to
i = i + 1;
int newNum = 10*(++i);
int newNum = 10*i;
Operator Name
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Operator Name
! not
&& and
|| or
Truth Table for Operator !
Operand !Operand
true false
false true
Operand1 Operand2 Operand1 && Operand2
false false false
false true false
true false false
true true true
Operand1 Operand2 Operand1 || Operand2
false false false
false true true
true false true
true true true
var++, var--
*, /, % (Multiplication, division, and modulus)
+, - (addition and subtraction)
<, <=, >, >= (Comparison)
==, !=; (Equality)
&& ( AND)
|| ( OR)
=, +=, -=, *=, /=, %= (Assignment operator)
Thanks
Any Question???
Mail ur assignment
sadiaamir69@gmail.com