0% found this document useful (0 votes)
7 views33 pages

PF Lecture 03

This document is a lecture outline for a Programming Fundamentals course focusing on C++ basics, including data types, variables, operators, and manipulators. It provides examples of C++ code for input/output operations and variable declarations. The lecture also covers implicit and explicit data type conversions and includes a class exercise to calculate the area of a circle.

Uploaded by

ABBASIDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views33 pages

PF Lecture 03

This document is a lecture outline for a Programming Fundamentals course focusing on C++ basics, including data types, variables, operators, and manipulators. It provides examples of C++ code for input/output operations and variable declarations. The lecture also covers implicit and explicit data type conversions and includes a class exercise to calculate the area of a circle.

Uploaded by

ABBASIDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Spring-2025

Programming
Fundamentals
Lecture 03, 024
Mr. Noman Al Hassan
Lecturer
Week 02
Computing and Technology Department
Iqra University H-9 Campus, Islamabad

email: [email protected]
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

to print text to the screen

tells the program that it


has finished successfully
Common Escape Sequences in C++
Operators in C++
Operators allow performing operations on variables and values. Some
common operators include:
 Arithmetic Operators (+, -, *, /, %)
 Comparison Operators (==, !=, >, <, >=, <=)
 Logical Operators (&&, ||, !)
 Assignment Operators (=, +=, -=, *=, /=)
Manipulators in C++
 Manipulators are functions that are used to format or modify the output stream
in various ways.
 They have a special characteristic that they are used along with insertion (<<)
operator to change the format of the data.
 There are many manipulators in C++. Some are as follows

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

2. using namespace std; Hello BS(SE)


Welcome to PF Class!
3. int main() will print in the next line on the output
screen.
4. {
5. cout<<"Hello BS(SE)"<<endl;
6. cout<<"Welcome to PF Class!";
7. return 0;
8. }

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(){

string name = “Ahmed”;


int age = 20;
double salary = 50000.00;
char grade = 'A';
bool isPassed = true;

cout<<name<<" "<<age<<" "<<salary<<" "<<grade<<" "<<isPassed;

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

10. cout<<"character: "<<c<<endl;


11. cout<<"integer: "<<i<<endl;
12. cout<<"float: "<<f<<endl;
13. cout<<"double: "<<d<<endl;
14. cout<<"bool: "<<b;
15. return 0;
16. }
Assigning values according to data type 22
Char Data Type
Strings: What it is?
 Use the string class (not a primitive data type).
 #include <string> is required.
 Define string objects (variables).
 Use double quotes for string literals.
 Output strings with cout.
Strings: Example
#include <iostream>
#include <string>

int main() {
string movieTitle;
movieTitle = "Avenger";
cout << "My favorite movie is " << movieTitle << endl;
return 0;
}
Bool Data Type

 Boolean expressions evaluate to true or false.

 The bool data type stores these values.

 True and false are represented by 1 and 0.

 Crucial for conditional logic (discussed later).


Data Type Conversion

 Implicit conversion
 Explicit conversion

27
Implicit Conversion
1. // mixed.cpp 28

2. // shows mixed expressions


3. #include <iostream>
4. using namespace std;
5. int main()
6. {
7. int count = 7;
8. float avgWeight = 155.5;
9. double totalWeight = count * avgWeight;
10. cout << "totalWeight=" << totalWeight << endl;
11. return 0;
12. }
Implicit Conversion
29
Explicit Conversion
30
// tests ASCCI character’s decimal value
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char c='A';
6. cout << "ASCII value of " << c << " is :"<< int(c);
7. return 0;
8. }
Explicit Conversion Example 2
31
Class Exercise
Write a program to calculate the area of a circle.
THANK YOU

You might also like