c++
DESCRIPTION
Computer Programming lectureTRANSCRIPT
![Page 1: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/1.jpg)
University of South Asia, Lahore
1Lecturer: Engr. Tariq Sadiq
![Page 2: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/2.jpg)
Today’s Menu History of C++.
Writing C++ Program.
Structure of C++ Program.
C++ Statements.
Keywords.
Tokens.
Variables and rules for writing variables names.
Data Types in C++.
Declaration of variables.
Initialization of variables.
Constants.
Arithmetic Operates.
Arithmetic Expressions.
Order of Precedence of Operators.2Lecturer: Engr. Tariq Sadiq
![Page 3: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/3.jpg)
Introduction C plus plus is a powerful computer language.
Advance version of C language.
C is a procedural language (e.g. get some input, add these numbers, divide by 6, display the output)
C++ is a object-oriented programming.
Brief History In 1967 BCPL was developed by Martin Richards.
In 1969 B language was developed by Ken Thomson.
Both B and BCPL were type-less languages.
In 1972 C language was developed by Dennis Ritche. (data type)
In early 1980’s C++ language was developed by Bjarne(Unix systems)
3Lecturer: Engr. Tariq Sadiq
![Page 4: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/4.jpg)
Writing C++ Program Different compliers (Turbo C++, Borland C++, Dev-C++)
First.cpp, First.obj, First.exe
Structure of C++ ProgramPreprocessor Directives (The instructions that are given to the complier before the beginning of any program)
Header File (is a C++ source code that contains the definition of functions/objects presents in library)
The main() Function (indicates the beginning of C++ program)
Example:
#include <iostream.h>
main()
{ cout<<“This is my First Program”;
}4Lecturer: Engr. Tariq Sadiq
![Page 5: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/5.jpg)
C++ Statements
main()
{
program statements……
}
Key Words The words that are used by the language for special purposes are called
as keywords (i.e. reserved words)
These words cant be used as variable names in a program.
Example:
Include, main, int
5Lecturer: Engr. Tariq Sadiq
![Page 6: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/6.jpg)
Tokens In C++ the elements of a statement are called tokens
Example:
#include <iostream.h>
main()
{ int a, b, c;
}
Variables A quantity whose value may change during the execution of program is
called as variable.
Rules for writing variable
names6Lecturer: Engr. Tariq Sadiq
![Page 7: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/7.jpg)
The first character of variable name must be alphabetic character.
Underscore can be used as first character of variable name.
Special characters, such as arithmetic operators, #,^, cant be used in variable name.
Reserved words cant be used as variable names.
A variable name used for one data type cant be used for another data type
C++ is case sensitive language so Pay and pay are two different variables.
Some examples of the valid and invalid variable names are given as:
7Lecturer: Engr. Tariq Sadiq
![Page 8: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/8.jpg)
Variable Name Valid/Invalid Remarks
Hassan valid
perform valid
double invalid C++ reserved word
foxpro valid
switch invalid C++ reserved word
Tania valid
int invalid C++ reserved word
3taq invalid Starts with numeric
unsigned invalid C++ reserved word
x-y invalid Special character not allowed
Taq Ahd invalid Space is not allowed
Lecturer: Engr. Tariq Sadiq 8
![Page 9: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/9.jpg)
Data Types in C++ int
(The int represents the integer data)
float
(The float represents the real or floating type data)
double
(The double represents the real or floating type data)
Char
(The char is used to declare character type variables)
Bool
(Boolean is used to represent true or false)
Lecturer: Engr. Tariq Sadiq 9
![Page 10: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/10.jpg)
Lecturer: Engr. Tariq Sadiq 10
![Page 11: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/11.jpg)
Lecturer: Engr. Tariq Sadiq 11
![Page 12: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/12.jpg)
Declaration of variables Assigning the name and data type that a variable can hold is called
declaration of the variable.
type list of variables;
Example:
int a, xy;
float b;
char nm [15];
double sum;
Initialization of Variables Assigning a known value to a variable at the time of its declaration is
called as initialization of the variable. (int a=4, b=1997)
Lecturer: Engr. Tariq Sadiq 12
![Page 13: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/13.jpg)
Lecturer: Engr. Tariq Sadiq 13
Example:
#include <iostream.h>
#include <conio.h>
main ()
{
int a=2,b=3;
float p=3.14;
char name[8]=“tariq”;
cout<<a<<endl;
cout<<b<<endl;
cout<<p<<endl;
cout<<name<<endl;
getch();
}
![Page 14: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/14.jpg)
Constants A quantity that cant change its value during execution of program.
Integer constants
A numerical value without a decimal part is called integer constants.
Example:
cout<<1234;
cout<< 176;
Floating point constants
Numeric value that have an integer as well as a decimal part are called floating-point values.
Example:
123.5E2
Character constants
A single character enclosed in single quotation marks is called character constant.
Lecturer: Engr. Tariq Sadiq 14
![Page 15: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/15.jpg)
Example:
‘a’ , ‘/’ , ‘+’
String constants
A sequence of characters consisting of alphabets, digits and/or special characters enclosed in double quotation marks is called string constant
Example:
“Pakistan” , “Lahore-54500”
Const QualifierThe data item that follows the keyword “const” cant change its valude
during execution of the program.
Example:
Lecturer: Engr. Tariq Sadiq 15
![Page 16: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/16.jpg)
#include <iostream.h>
#include <conio.h>
main ()
{
int r=2;
Const float p=3.14;
float peri;
peri=2*p*r;
cout<<“Result is=“<<peri;
getch();
}
Lecturer: Engr. Tariq Sadiq 16
![Page 17: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/17.jpg)
The “define” Directive It is preprocessor directive. It is used to define a constant quantity. It is
used at the beginning of the program.
#define identifier constant
Example:
#include <iostream.h>
#define p 3.14
main ()
{
int r=2;
float peri;
peri=2*p*r;
cout<<“Result is=“<<peri;
getch();
}
Lecturer: Engr. Tariq Sadiq17
![Page 18: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/18.jpg)
Arithmetic OperatorsOperator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% For remaninder
Lecturer: Engr. Tariq Sadiq 18
![Page 19: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/19.jpg)
#include <iostream.h>
main ()
{
int sum,subtract,multiplication,division,remainder;
sum=5+2;
Subtract=5-2;
Multiplication=5*2;
Division=5/2;
Remainder=5%2;
Cout<<“Addition of 5&2 is=“<<sum<<endl;
Cout<<“Subtraction of 5&2 is=“<<subtraction<<endl;
Cout<<“Multiplication of 5&2 is=“<<multiplication<<endl;
Cout<<“Divison of 5&2 is=“<<divison<<endl;
Cout<<“Remainder of 5/2 is=“<<remainder<<endl;
getch();
}
Lecturer: Engr. Tariq Sadiq 19
![Page 20: C++](https://reader033.vdocuments.net/reader033/viewer/2022042702/563db862550346aa9a932bd6/html5/thumbnails/20.jpg)
Arithmetic ExpressionIf m=10, x=5
res=m*x+100
Order of Precedence of Operation All multiplications and divisions are performed first from left to right.
All additions and subtractions are then performed from left to right.
If parentheses are used in an expression, the expression within parentheses are first computed from left to right.
When parentheses are used within parentheses, the expression within innermost parentheses is evaluated first.
Example:
(4-(3*5))+2
Lecturer: Engr. Tariq Sadiq 20