0% found this document useful (0 votes)
17 views

Lecture 5 C++

Uploaded by

Saif Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 5 C++

Uploaded by

Saif Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Programming Fundamentals

Lecture 5
By:
Faiz Ur Rehman
Lecturer in Computer Science
[email protected]

1
Contents
• How C++ program works
• Identifiers (User Defined Words)
• Keywords (Reserved Words)
• Data types in C++
• Variables in C++
• Variable declaration in C++
• Variable definition in C++
• Constants in C++
• Variable as memory locations
• Inputting with cin statement
• Problem Examples
• Programming Tasks
2

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


How C++ program works?
• Step 01: Write source code
Editor or IDE • Source code (.cpp), Header files (.h)

• Step 02: Preprocessor directives


Preprocessor • #include, #define

• Step 03: Compile the code


Build Compiler • Object Codes (.obj, .o)

• Step 04: Link Edit


Linker • Static Libraries (.lib, .a)

• Step 05: Load program in to memory


Loader • Shared Libraries (.dll, .so)
Run
• Step 06: Execute the program
CPU • Run program
3

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


How C++ program works?
• First you will write source code in any of the text editor or IDE (e.g.
Code::Blocks, Dev C++)
• After that all the preprocessor directives will be executed by the
preprocessor (e.g. #include, #define)
• Then the compiler will compile the source code and will generate object
code that will be stored in the file with extension .obj or .o.
• The linker then links all the object files with the static libraries. It can also
link multiple object files with each other as well and produce one
executable file.

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


How C++ program works?
• Then the loader loads the executable file in to the memory and makes it
ready to be executed.
• Finally, the CPU executes all the instruction of the program loaded in to the
memory.
• Once program is executed, you will input the data and program gives you
the desired output.

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Identifier (User Defined Words)
Rules for naming Variables and Constants
• The name of any variable, constant, types, functions or labels is called as identifier.
• There are certain rules to follow when setting the identifier.
• Rule 1: May contain letters (A-Z, a-z), digits (0-9) or an underscore sign (_).
• Rule 2: It must start with letter or underscore sign.
• Rule 3: Should not contain any space in between.
• Rule 4: Should not be like any keyword of language like, int, cout, float.
• Rule 5: It is case sensitive i.e. radius is not same as Radius or RADIUS.
• Rule 6: Must be unique i.e. no any two variables have same identifier.
• Rule 7: Must be relevant i.e. var1 is inappropriate identifier to store the area of circle, use area of
area_circle instead.
• Rule 8: The identifier for constant is written with all capital letters. i.e. PI, SPEEDOFLIGHT, E etc.
• Rule 9: A variable can be up to 31 characters long for many compilers.

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Rules for naming Variables and Constants
Identifier Valid/Invalid Remarks
first_number Valid
first number Invalid Must not contain space in between
number_1 Valid
1st_number Invalid Must start with a letter or underscore sign
first# Invalid Must not contain invalid character $
int Invalid Must not be like keyword
firstNumber Valid
int_first_number Valid
first_number_$ Invalid Must not contain invalid character #
1st_# Invalid Must start with a letter or underscore sign
Must not contain invalid character #

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Do it yourself
Rules for naming Variables and Constants
Identifier Valid/Invalid Remarks
number of items
#_of_items
price/item
number_items
itemCount
item#Price
itemPrice_1
5_itemPrice
ITEM_PRICE
$_per_item
8

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Keywords
• Keywords is a word in C++ language that has a predefined meaning and
purpose.
• The meaning and purpose of a keyword is defined by the developer of the
language.
• It cannot be changed or redefined by the user.
• Keywords can be used for the same purpose for which it is defined.
• Keywords are also known as reserved words. There are different types of
keywords in C++ language.
• The total number of keywords is 63 or above.
9

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Keywords in C++

10

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Data Types
• Every problem involves some sort of data in it.
• The data can be:
• Number
• Integers (e.g. 24, 5874, -547)
• Floating (e.g. 45.214, 0.2547, -658.748)
• Characters (e.g. ‘f’, ‘a’, ‘#’, ‘?’, ‘!’, ‘z’)
• String (e.g. “faiz”, “computer”, “GPGC Kohat”)
• Logical (e.g. True and False, Yes and No, 0 and 1)
• The type of data is known as data type of that data item.
11

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Data Types
Data Item Example Value Data Type
Age of a person 35 Integer
Current year 2024 Integer
Radius of circle 27.58 Floating
Value of PI 3.14159 Floating
A vowel e Character
A key on keyboard ? Character
Person’s name Faiz Ur Rehman String
Address of an office Department of CS String
Is 1st number greater than 2nd ? True or T or Yes or 1 Logical
Is 7 less than 5 ? False or F or No or 0 Logical

12

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Do it yourself
Data Types
Data Item Example Value Data Type
Number of students
Brand name of smartphone
Is 5 an even number?
Count of cars in parking
Speed of a car
Your grade in result
Is 13 a prime number ?
Title of book chapter
Percentage of marks
Option in MCQ

13

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Data Literals
• A fixed value that any data type can take is called as literal.
• A number literal is always written without quotes. (15, 68, 25.14, 578.14)
• A character literal is always written in single quotes. (‘f’, ‘a’, ‘#’, ‘?’, ‘!’)
• A string literal is always written in double quotes. (“faiz”, “computer”)
• A logical literal is always written without quotes. (True, False, T, F, Yes, No, 1, 0)

14

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Data Literals
Data Literal Type of Literal
“Faiz” String
‘b’ Character
25.2 Floating
“87.5” String
‘4’ Character
4 Integer
4.0 Floating
“true” String
false Logical
“GPGC” String

15

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Data Types in C++
Data Type Memory Range Start Range End
char 1 Byte -128 127
unsigned char 1 Byte 0 255
short 2 Bytes -32,768 32,767
unsigned short 2 Bytes 0 65,535
int 4 Bytes -2,147,483,648 2,147,483,647
unsigned int 4 Bytes 0 4,294,967,295
long 8 Bytes -9,223,372,036,854,775,807 9,223,372,036,854,775,807
unsigned long 8 Bytes 0 18,446,744,073,709,551,615
float 4 Bytes 3.4 × 10−38 3.4 × 1038
double 8 Bytes 1.7 × 10−308 1.7 × 10308
bool 1 Byte true and false
16

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables
• In programming, a variable is the memory (RAM) location that can store
the data temporary.
• Every program uses some sort of data and each data item is stored
temporarily in the variables.
• Every variable has:
• Name
• Data type
• Value

17

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables
• The name of the variable is called as the identifier.
radius
• Consider two of the examples.
• In first example, we have a variable whose name is radius, 25.5
its data types is floating and its value is 25.5.

option

• In second example, we have a variable whose name is option,


its data type is character and its value is b. b

18

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables in C++
• Variables are used to stored the data temporarily.
• A variable is a named piece of memory location.
• A single variable can store single value at a time.
• The value of the variable is changeable.
• In order to create a variable, we need to specify three things:
Name
Data Type
Value (optional)

19

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables in C++
• The name of the variable is called as the identifier.
• The data type of the variable specifies the type of the value which will be
stored in it.
• The value is the actual content which will be stored in it. It is optional
because some time we know the exact value which will be stored,
sometimes we do not know.

20

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Creating Variables in C++
• There are two ways to create a variable:

21

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variable Declaration in C++
• A variable is declared when we do not know the value to be stored in it.
• To declare the variable, we need to specify two things: name and data
type.
data_type variable_name;

float radius; radius

22

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variable Declaration in C++
char v1 ; long v7 ;
unsigned char v2 ; unsigned long v8 ;
short v3 ; float v9 ;
unsigned short v4 ; double v10 ;
int v5 ; bool v11 ;
unsigned int v6 ;

23

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variable Definition in C++
• A variable is defined when we know the exact value to be stored in it.
• To define the variable, we need to specify three things: name, data type
and value.
data_type variable_name = value;
float radius = 56.214F;

radius

56.214

24

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variable Definition in C++
char v1 = ‘w’ ; long v7 = 25486655L ;
unsigned char v2 = ‘w’ ; unsigned long v8 = 58622455UL ;
short v3 = -24 ; float v9 = 25.4856F ;
unsigned short v4 = 64 ; double v10 = 5685.2154785 ;
int v5 = 5847 ;
bool v11 = true ;
unsigned int v6 = 8552U ;

To Initialization all variables a, b, c and d to the same value i.e. 5


int a, b, c, d ;
a=b=c=d= 5847 ; 25

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Declaration & initialization of variable

26

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Compiler View

Error
Message

27

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variable Program Example

28

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program: Adding two numbers

29

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program: Adding two numbers

30

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program: Adding two numbers

31

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program: Adding two numbers

32

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program: Adding two numbers

33

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Constants
• In programming, a constant is a variable whose value remains fixed
through out the program.
• In certain programs some sort of data remains unchangeable through out
the program, hence we use constants to stored that data so that it can not
be altered.
• Consider three of the examples: PI
• In first example, we have a constant whose name is PI,
its data types is floating and its value is 3.141.
3.141

34

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Constants
E
• In second example, we have a constant whose name is E
(Euler’s number), its data type is floating and its value is 2.718.
2.718

SPEEDOFLIGHT
• In second example, we have a constant whose name is
SPEEDOFLIGHT, its data types is floating and its value
is 3 x 108. 3E8

35

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Constants in C++
• A constant is a variable whose value can not be altered during the
execution of the program.
• A constant is given a single value at the time of creation and it will hold
that value till the entire execution of the program.
• In C++, a variable is declared as constant by just prefixing the variable
definition statement with the keyword const.
const data_type variable_name = constant_value;
const float PI = 3.1415F ;
const int MAX = 100 ;
36

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Constants Program Example
#include<iostream>
using namespace std;
int main()
{
const float PI=3.14;
float radius;
float areacircle;

cout<<"Enter Radius:";
cin>>radius;
areacircle =PI*radius*radius;
cout<<areacircle;
return 0;
}
37

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables as Memory Locations
• A variable is defined as a named piece of memory location.
• When we create a variable, some bytes of memory are reserved according
to the data type, and are given a name of the variable.
• Entire memory is divided in to pieces of 1 byte and each of the byte has
address defined in hexadecimal number. MEMORY

2000H
2001H
int num = 26 ; 2002H
num
2003H
26 2004H
2005H
2006H 38

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Variables as Memory Locations
MEMORY
• Consider following empty memory map: 2000H
2001H
• Execute the following variable definition statements: 2002H
2003H
short V1 = 69 ; 2004H
2005H
char V2 = ‘w’ ; 2006H
2007H
int V3 = 5478 ; 2008H

float V4 = 87.245F ; 2009H


200AH
200BH
200CH
200DH
200EH
200FH
2010H 39
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
2002H

short V1 = 69 ; 2003H
2004H
2005H
2006H
2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 40
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

char V2 = ‘w’ ; 2003H


2004H
2005H
2006H
2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 41

2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

int V3 = 5478 ; V3

5478
2003H
2004H
2005H
2006H
2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 42
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H

V2 w 2002H

float V4 = 87.245F ; V3

5478
2003H
2004H
2005H
2006H
V4 2007H
2008H
87.245
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 43

2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
• Consider following memory map: V1
2000H
2001H

• Write down the variable definition statements 69.258


2002H
2003H
for the variables created in the memory. 2004H
2005H
float V4 = 62.258F ; 2006H
V2 2007H
658
short V2 = 658 ; 2008H
2009H
200AH

int V3 = 1453 ; V3
200BH
200CH
1453
200DH
char V4 = ‘b’ ; 200EH
V4 b 200FH
2010H
44
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
• Execute the following variables definition V1
2000H
2001H
statements in the same memory map: 2002H
69.258
2003H
2004H

short V5 = 985 ; 2005H


2006H
V2 2007H
658
char V6 = ‘r’ ; 2008H
2009H

short V7 = 74 ; V3
200AH
200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 45
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
2000H
V1 2001H
2002H
69.258
2003H
2004H

short V5 = 985 ; V5
985
2005H
2006H
V2 2007H
658
2008H
2009H
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 46
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V6 r 2000H

V1 2001H
2002H
69.258
2003H
2004H
V5 2005H
985
char V6 = ‘r’ ; V2
2006H
2007H
658
2008H
2009H
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 47
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V6 r 2000H

V1 2001H
2002H
69.258
2003H
2004H

short V7 = 74 ;
V5 2005H
985
2006H
V2 2007H
658
2008H
V7 2009H
74
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 48
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Inputting with cin

cin >> radius;

49

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Inputting with cin
• In C++, cin statement is used to get input from the keyboard.
• Cinis the Standard Console Input, which is the keyboard.
• Cin is used in conjunction with the extraction operator ( >> ).
• Anything inputted from keyboard will go to the variable to the right side of
>> extraction operator.

50

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 01
• Problem Statement:
• Write a program in C++ to take class rollno, grade, and gpa from the user
and print these values on screen.
• Sample output:

51

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 01

52

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 02
• Problem Statement:
• Write a computer program in C++ that accepts the base and height of a
right-angle triangle from the user and displays the area of the triangle.
• Sample output:

53

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 02

54

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 03
• Problem Statement:
• Write a program in C++ that asks the user to enter two integer numbers,
stores them in variables a and b respectively. The program swaps the
values of two variables with each other and displays the values of both the
variables after swapping.
• Sample output:

55

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Program Example 03

56

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Programming Task 01
• Problem Statement:
• Write a program in C++ that asks the user to enter two integer numbers,
stores them in variables num1 and num2 respectively. The program swaps
the values of two variables with each other without using a third variable
and displays the values of both the variables after swapping.
• Sample output:
Input: Output:
num1 = 45 num1 = 94
num2 = 94 num2 = 45

57

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Programming Task 02
• Problem Statement:
• Write a C++ program that declares the following variables, assigns
appropriate values to them, and then prints their values on the screen:
• age
• gpa
• grade
• salary

58

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


Programming Task 03
• Problem Statement:
• Write a C++ program that declares two integer variables, num1 and num2,
and assign them values. Calculate the sum, difference, and product of
these two numbers and display the results.

59

Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat


60

You might also like