PF Lecture 03
PF Lecture 03
Programming
Fundamentals
Lecture 03, 024
Mr. Noman Al Hassan
Lecturer
Week 02
Computing and Technology Department
Iqra University H-9 Campus, Islamabad
email: noman.hassan@iqraisb.edu.pk
Reference
C++ How to Program
Book By Harvey Deitel and Paul Deitel
10th Edition
C++
Programming
Basics
includes the necessary allows you to use names
libraries for input and from the standard library
output
7
Example : manipulator in C++
There are some manipulator outputs the subsequent data
or text in the next line.
8
endl manipulators in C++
1. #include<iostream> After this statement, the string
9
cout stream object
int x = 50;
cout << x;
(<<) known as an insertion operator
10
cin stream object
int x;
cin >> x;
(>>) known as an extraction Operator
11
Declare Variables
a and Datatypes
variable
Initialize a
variable
12
Variables and Datatypes
Variables store data, and each variable has a type that defines the kind
of data it can hold. C++ provides several built-in data types:
int: Stores integers (without decimal).
float and double: Store floating-point numbers (with decimal.
char: Stores single characters such as ‘a’.
bool: Stores two states(true or false).
string: Stores text values such as “ HELLO”. (requires <string> library)
The table below shows the fundamental data types, their meaning, and
their sizes (in bytes):
#include <iostream> 15
#include <conio.h>
using namespace std; Program to display datatype
size
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
getch();
return 0;
}
#include<iostream>
using namespace std;
int main(){
return 0;
}
1. #include<iostream>
2. using namespace std;
3. int main() Program without Variable
4. {
5. cout<<“How many books I have?"<<endl;
6. cout<<“I have”<< 3<< “books”;
7. return 0;
8. }
17
1. #include<iostream>
2. using namespace std;
3. int main() Program with Variable
4. {
5. int books=5;
6. cout<<“How many books I have?"<<endl;
7. cout<<“I have ” <<books<<“ books”;
8. return 0;
9. }
18
Variable
Variable is the name of a location in the memory that hold a value
For example: x = 2;
In any program, a variable has:
Name
Type
Size
Value
19
1. #include<iostream>
2. using namespace std;
3. int main() Assigning value to variable from
4. { user
5. int numberOfBooks=2;
6. numberOfBooks=3;
7. cout<<“How many books I have?"<<endl;
8. cin>>numberOfBooks;
9. cout<<“I have” <<numberOfBooks<<“ books”;
10. return 0;
11. }
20
Assigning values according to data type 21
1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. char c='A'; // declaration and initialization of variable c
6. int i=2;
7. float f=1.2;
8. double d=23.5;
9. bool b=false; //it can also hold true as a value
int main() {
string movieTitle;
movieTitle = "Avenger";
cout << "My favorite movie is " << movieTitle << endl;
return 0;
}
Bool Data Type
Implicit conversion
Explicit conversion
27
Implicit Conversion
1. // mixed.cpp 28