PF Lab 03 2k21
PF Lab 03 2k21
PROGRAMMING
FUNDAMENTALS
Experiment 3
C++ Basic Data Types and Variables
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
This experiment provides an introduction to the C++ Basic Data Types and Variables.
Students will run and check different but related programs consisting of different C++ data
Objectives:
1) The different data types and variables available in C++: how to use them in a C++
program.
2) Able to run and check different but related programs consisting of different C++ data
3) Implementation of C++ program using the right data type according to the required
logic.
5) Code::Blocks IDE16.01
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
C++ Output
In C++, cout sends formatted output to standard output devices, such as the screen. We use the
cout object along with the << operator for displaying output.
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}
Output
We first include the iostream header file that allows us to display output.
The cout object is defined inside the std namespace. To use the std namespace, we used
the using namespace std; statement.
Every C++ program starts with the main() function. The code execution begins from the
start of the main() function.
cout is an object that prints the string inside quotation marks " ". It is followed by the <<
operator.
return 0; is the "exit status" of the main() function. The program ends with this
statement, however, this statement is not mandatory.
Note: If we don't include the using namespace std; statement, we need to use std::cout
instead of cout.
#include <iostream>
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}
To print the numbers and character variables, we use the same cout object but without using
quotation marks.
#include <iostream>
using namespace std;
int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
Output
70
256.783
character: A
Notes:
The endl manipulator (that is used to change the formatting of output) is used to insert a
new line. That's why each output is displayed in a new line.
The << operator can be used more than once if we want to print different variables, strings
and so on in a single statement. For example:
C++ Input
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
In C++, cin object takes formatted input from standard input devices such as the keyboard. We
use the cin object along with the >> operator for taking input.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
Output
Enter an integer: 70
The number is: 70
to take input from the user. The input is stored in the variable num. We use the >> operator with
cin to take input.
int main() {
char a;
int num;
5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
return 0;
}
Output
In C++, there are different types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
string - stores text, such as "Hello World". String values are surrounded by double
quotes
bool - stores values with two states: true or false
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Syntax
Where type is one of C++ types (such as int), and variable_Name is the name of the variable
(such as x or myName). The “=” equal sign is used to assign values to the variable.
To create a variable that should store a number, look at the following example:
Example
Create a variable called myNum of type int and assign it the value 15:
You can also declare a variable without assigning the value, and assign the value later:
Example
int myNum;
myNum = 15;
cout << myNum;
Example
//Demonstrate the use of different Types of Variables
#include <iostream>
using namespace std;
int main()
{
int myNum = 5; // Integer (whole number without decimals)
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
return 0;
}
C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const
keyword. constant", which means unchangeable and read-only. Here's an example:
Example
return 0;
}
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;// a has updated value which is 6 now
b= a+2; // b has updated value which is 6+2 = 8
result = a - b; // result now contains 6-8= -2 value
// print out the result:
cout << result;
// terminate the program:
return 0;
}
Sizeof() Function:
Example:
#include <iostream>
using namespace std;
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;
return 0;
}
We are also using sizeof() operator to get size of various data
types mean number of bytes used to store each data type.
Example:
#include <iostream>
using namespace std;
int main()
{
const int SIDE = 50;
int area;
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
area = SIDE*SIDE;
cout<<"The area of the square with side: " << SIDE <<" is: " << area <<
endl;
return 0;
}
Task 1:
Create a C++ Program to Generate the Following Output. (10 Marks)
Task 02:
Write a C++ program that
takes length, width of a rectangle of specific data type from user as input
to calculate perimeter and area of rectangle.
display the results.
Task 03:
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
1. Use concepts as already taught in the class and produce output as follows.
11