UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
COMPUTER PROGRAMMING
Experiment 4
C++ Basic Data Types and their Conversion
(Cont.…)
CLO 2. Use modern tools and languages for solving problems of
varying complexities
CLO 3. Construct the experiments/projects of varying complexities.
CLO 4. Demonstrate unique solution of problem under discussion
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Purpose:
This experiment is a continuity of preceding experiment 4 i.e. “Introduction to the C++
Basic Data Types and Variables”. Students will run and check more advanced programs
consisting of different C++ data types and variables.
Objectives:
At the end of this experiment you will know:
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
types and variables.
3) Implementation of C++ program using the right data type according to the required
logic.
Equipment and Components:
4) Dev-C++ 5.0 Beta 9.2 (4.9.9.2) 9.0 MB with Minge/GCC 3.4.2
5) Code :: Blocks IDE16.01
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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.
C++ basic operators a
Examples of C++ expression:
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
SOFTWARE ENGINEERING DEPARTMENT
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;
}
Bool data type in C++
A boolean data type is declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
Important Points:
The default numeric value of true is 1 and false is 0.
We can use bool type variables or values true and false in mathematical expressions
also.
Example 1:
#include<iostream>
using namespace std;
int main()
{
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun<<endl; // Outputs 1 (true)
cout << isFishTasty<<endl; // Outputs 0 (false)
return 0;
}
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.
Expression Containing Boolean Values
// CPP program to illustrate bool
// data type in C++
#include<iostream>
using namespace std;
int main()
{
int x1 = 10, x2 = 20, m = 2;
bool b1, b2;
b1 = false; // false=0
5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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;
}
C++ Type Conversion
C++ allows us to convert data of one type to that of another. This is known as type conversion.
There are two types of type conversion in C++.
1. Implicit Conversion (automatic Conversion)
2. Explicit Conversion (also known as Type Casting)
Implicit Type Conversion
The type conversion that is done automatically done by the compiler is known as implicit type
conversion. This type of conversion is also known as automatic conversion.
Example 1: Conversion from “int to double”
// Working of implicit type-conversion
#include <iostream>
using namespace std;
int main()
{
// assigning an int value to num_int
int num_int = 9;
// declaring a double type variable
double num_double;
// implicit conversion
// assigning int value to a double variable
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
num_double = num_int;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return 0;
}
Output
num_int = 9
num_double = 9
Example 2: Automatic Conversion from double to int
//Working of Implicit type-conversion
#include <iostream>
using namespace std;
int main() {
int num_int;
double num_double = 9.99;
// implicit conversion
// assigning a double value to an int variable
num_int = num_double; cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return 0;}
Output
num_int = 9
num_double = 9.99
In the program, we have assigned a double data to an int variable.
num_double = num_int;
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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.
Data Loss During Conversion (Narrowing Conversion)
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.
C++ Explicit Conversion
When the user manually changes data from one type to another, this is known as explicit
conversion. This type of conversion is also known as type casting.
There are three major ways in which we can use explicit conversion in C++. They are:
1. C-style type casting (also known as cast notation)
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
2. Type conversion operators (Static Cast)
C-style Type Casting
As the name suggests, this type of casting is favored by the C programming language. It is also
known as cast notation.
The syntax for this style is:
(data_type)expression;// (Target_Type)Current_Type
Example:
//Working of Implicit type-conversion
#include <iostream>
using namespace std;
int main() {
// initializing int variable
double num1 = 26.5;
// declaring double variable
int num2;
// converting from int to double
num2 = (int)num1;
cout << "int = " << num2 << endl;
cout << "double = " << num1 << endl;
return 0;
}
2. static_cast <target type> (expression)
The static_cast keyword can be used for any normal conversion between types. Conversions that
rely on static (compile-time) type information.
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
SOFTWARE ENGINEERING DEPARTMENT
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;
cout<<"Enter length and breadth of rectangle in double or
float: ";
cin>>length>>breadth;
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
SOFTWARE ENGINEERING DEPARTMENT
// C++ code to demonstrate the working of setw() function
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
// Initializing the integer
int num = 50;
cout << "Before setting the width: \n"
<< num << endl;
// Using setw()
cout << "Setting the width"
<< " using setw to 5: \n"
<< setw(5);
cout << num << endl;
return 0;
}
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
SOFTWARE ENGINEERING DEPARTMENT
Dec = Decimal Value
Char = Character
'5' has the int value 53
if we write '5'-'0' it evaluates to 53-48, or the int 5
if we write char c = 'B'+32; then c stores 'b' as ‘B’ has int
value=66
so 66+32=97 which asci value for character ‘b’.
12
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
Conversation from int to char
Example:
#include <iostream>
using namespace std;
main()
{
char var1=97; // var1 is not assigned a character constant value, for cout
// computer will check the corresponding character from
// ASCII character set and print it.
// In terms of memory, integer value 97 will be stored
cout<<var1<<endl;
char var2='9'; // var2 is assigned a character constant value so for cout
// computer will simply print this value
// In terms of memory, integer value corresponding to
// this character will be stored that is 57, since char is
// used to store characters as numbers
cout<<var2<<endl;
char character1 = '9' , character2 ;
cout << character1 << 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 */
cout << character2 << endl ;
return 0;
}
13
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
SOFTWARE ENGINEERING DEPARTMENT
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;
char var3='a'; // 'a' has corresponding Ascci integer 97 in memeory
int var4=var3; // 97 is assigned to Var4
var4=var4+1; // 97+1=98
cout<<var4<<endl; //98
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
SOFTWARE ENGINEERING DEPARTMENT
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