Sunteți pe pagina 1din 20

University of South Asia, Lahore

Lecturer: Engr. Tariq Sadiq

Todays 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.
Lecturer: Engr. Tariq Sadiq

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 1980s C++ language was developed by Bjarne(Unix systems)

Lecturer: Engr. Tariq Sadiq

Writing C++ Program

Different compliers (Turbo C++, Borland C++, Dev-C++)


First.cpp, First.obj, First.exe

Structure of C++ Program


Preprocessor 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;
}
Lecturer: Engr. Tariq Sadiq

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

Lecturer: Engr. Tariq Sadiq

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


names
Lecturer: Engr. Tariq Sadiq

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:

Lecturer: Engr. Tariq Sadiq

Variable Name

Valid/Invalid

Hassan

valid

perform

valid

double

invalid

foxpro

valid

switch

invalid

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

Remarks

C++ reserved word


C++ reserved word

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

Lecturer: Engr. Tariq Sadiq

10

Lecturer: Engr. Tariq Sadiq

11

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

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();
}
Lecturer: Engr. Tariq Sadiq

13

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

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 Qualifier
The data item that follows the keyword const cant change its valude
during execution of the program.
Example:

Lecturer: Engr. Tariq Sadiq

15

#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

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 Sadiq

17

Arithmetic Operators
Operator

Meaning

Addition

Subtraction

Multiplication

Division

For remaninder

Lecturer: Engr. Tariq Sadiq

18

#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

Arithmetic Expression
If 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

S-ar putea să vă placă și