CP Lab 04
CP Lab 04
COMPUTER PROGRAMMING
Experiment 4
C++ Basic Data Types and their Conversion
(Cont.…)
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
Basic Data Types and Variables”. Students will run and check more advanced programs
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.
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
C++ Expressions
C++ expression consists of operators, constants, and variables which are arranged according to
the rules of the language. It can also contain function calls which return values. An expression can
consist of one or more operands, zero or more operators to compute a value.
1. (a+b) - c
2. (x/y) -z
3. 4a2 - 5b +c
4. (a+b) * (x+y)
Example 1:
#include <iostream>
using namespace std;
int main()
{
int x; // variable declaration.
x=(3/2) + 2; // constant expression
cout<<"Value of x is : "<<x; // displaying the value of x.
return 0;
}
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Example 2:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
double x,y,z;
a = 13;
b = 4;
x = 3.3;
y = 15.78;
c = a + b;
cout<<"a + b is "<<c<<endl;
z = x + y;
cout <<"x + y is "<<z<<endl;
c = a / b;
cout<<"a / b is "<<c<<endl;
c = a % b;
cout<<"a % b is "<<c<<endl;
return 0;
}
Example 1:
#include<iostream>
using namespace std;
int main()
{
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Example 2:
#include<iostream>
using namespace std;
int main()
{
bool x = 0; // false
bool y = 100; // true
bool z = 15.75;
int a= x+y+z; // true
cout << "bool x :" <<x<<endl; // Outputs 1 (true)
cout << "bool y :" <<y<<endl;
cout << "bool z :" <<z<<endl;
cout << "int a :" <<a<<endl;
return 0;}
For instance,
int x = false + true + 6;
is valid and the expression on right will evaluate to 7 as false has value 0
and true will have value 1.
5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
b2 = true; // true=1
bool b3 = true;//true=1
cout << "b1 is = " << b1 << "\n";
cout << "b2 is = " << b2 << "\n";
cout << "b3 is = " << b3 << "\n";
int x3 = false + 5 * m - b3;
cout << x3;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
// assigning an int value to num_int
int num_int = 9;
// implicit conversion
// assigning int value to a double variable
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
num_double = num_int;
return 0;
}
Output
num_int = 9
num_double = 9
Output
num_int = 9
num_double = 9.99
num_double = num_int;
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Note: The double value is automatically converted to int by the compiler before it is assigned to
the num_int variable. Since int cannot have a decimal part, the digits after the decimal point is
truncated in the above example.
As we have seen from the above example, conversion from one data type to another is prone to
data loss. This happens when data of a larger type is converted to data of a smaller type.
There are three major ways in which we can use explicit conversion in C++. They are:
(data_type)expression;// (Target_Type)Current_Type
Example:
#include <iostream>
using namespace std;
int main() {
return 0;
}
Division between floating point numbers, or even between one floating point number and
an integer, is sufficient to keep the result as a floating-point number. So, if we were
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
performing some kind of fancy division where we didn't want truncated values, we'd have
to cast one of the variables to a floating-point type. For instance, static_cast<float>(3)/5
comes out to .6, as you would expect!
Example:
#include<iostream>
#include<ctype.h>
#include<iomanip>
using namespace std;
main()
{
double length,breadth;
int area, perimeter;
area=static_cast<int>(length) * static_cast<int>(breadth);
perimeter=2*(static_cast<int>(length) +
static_cast<int>(breadth));
cout<<endl;
cout<<setw(20)<<"Area is: "<<setw(5)<<area<<endl
<<setw(20)<<"Perimeter is: "<<setw(5)<<perimeter;
return 0;
setw()
Setw() Sets the field width to be used on output operations. This manipulator is declared
in header <iomanip>. setw() sets the number of characters to be used as the field width
for the next insertion operation.
Syntax:
setw(int n)
int n Parameter: This method accepts n as a parameter which is the integer argument
corresponding to which the field width is to be set.
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Example:
#include<iostream>
#include<ctype.h>
#include<iomanip>
using namespace std;
main()
{
float n=static_cast<float>(1)/3;
cout<<setw(20)<<"with casting :"<<setw(15)<<n <<endl;
cout<<setw(23)<<"without casting :"<<setw(5)<<(1)/3;
return 0;
}
ASCII Table
ASCII stands for American Standard Code for Information Interchange. It is a code for
representing 128 English characters as numbers, with each letter assigned a number from 0
to 127.
11
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
12
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
cout<<var2<<endl;
character2 = character1 + 2 ;
/* In memory, space corresponding to character1 will be holding 57 and adding 2
in it equals to 59 and assigning character2 a value of 59 means computer will
display character corresponding to this ASCII code */
13
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Example 2:
#include <iostream>
using namespace std;
main()
{
int var1=97;
cout<<var1<<endl;
char c='9';
int var2=c;// assign char value to int so its corresponding Ascci integer will be assigned
to int
var2=var2+1;// 57+1=58
cout<<var2<<endl;
return 0;}
Task 01:
Write a program that asks the user to enter 3 double type numbers and then prints the
sum, difference, product and average of these integers after type casting of these
numbers into integers at the time of operation. Use setw () manipulator to display the
output properly.
14
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Task 02:
Write a program that reads in the radius of a circle and prints the circle's diameter,
circumference and area. Use the constant value 3.14159 for PI. Do these calculations in
output statements and format using setw(). Use following formulas:
Diameter = 2𝑟
Circumference = 2𝜋𝑟
Area = 𝜋𝑟 2
Output
Diameter --------
Circumference --------
Area -------
15