0% found this document useful (0 votes)
10 views

PF - Lecture 8

1. The document summarizes a lecture on type conversion in C++. It discusses namespaces, using directives, implicit type conversion, and explicit type conversion through C-style casts, function-style casts, and C++ cast operators. 2. An example is given showing explicit type conversion from float to int using each method, which correctly converts the float value to an int, unlike an implicit conversion. 3. Students are given an exercise to write a simple calculator program that reads two integers and an operator character, and performs the corresponding arithmetic operation, printing the result.

Uploaded by

Mehreeen
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)
10 views

PF - Lecture 8

1. The document summarizes a lecture on type conversion in C++. It discusses namespaces, using directives, implicit type conversion, and explicit type conversion through C-style casts, function-style casts, and C++ cast operators. 2. An example is given showing explicit type conversion from float to int using each method, which correctly converts the float value to an int, unlike an implicit conversion. 3. Students are given an exercise to write a simple calculator program that reads two integers and an operator character, and performs the corresponding arithmetic operation, printing the result.

Uploaded by

Mehreeen
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/ 13

MIRPUR UNIVERSITY OF SCIENCE AND TECHNOLOGY

DEPARTMENT OF CS&IT
Type Conversion
(lecture # 8)

Miss. Mehreen Shakoor


(Lecturer)

Date: 16 May, 2020


Lecture Content

1. Namespaces
• Namespaces
• Using Directive
2. Type Conversion
• Type Conversion
• Implicit Type Conversion
• Explicit Type Conversion
• Explicit Type Conversion: Style
• Explicit Type Conversion: Example
3. Exercise

Programming Fundamental 3
Namespaces

• A C++ program can be divided into different namespaces.


• A namespace is a part of the program in which certain names (identifiers) are
recognized; outside of the namespace they are unknown.
• For further details, read the handouts or any recommended C++ book

Programming Fundamental 4
using Directive

• The keyword using is used to introduce a name from a namepace into the current
declarative region.
• For instance, the directive
using namespace std;
says that all the program statements that follow are within the std namespace.
• Files in standard C++ library declare all of their entities within the std namespace,
for instance, cout, cin.
• We generally include it in all programs that use any entity defined in iostream.

Programming Fundamental 5
Type Conversion

• Often the data needs to converted from one type to another type. This is called
type conversion.
• Two ways to convert a data type
• Implicit Type Conversion
• Explicit Type Conversion

Programming Fundamental 6
Implicit Type Conversion

• Done automatically by the compiler whenever data from different types is mixed.
• When a value from one type is assigned to another type, the compiler implicitly
converts the value into a value of the new type. For example:
int main()
{
int count = 5;
float weight = 155.5;
double totalweight = count * weight;
cout << ‘‘Total weight is ’’ << totalweight << endl ;
return 0;
}

Programming Fundamental 7
Explicit Type Conversion

• Represents a request by the programmer to do a type conversion, also called type


cast.
• Explicit type conversions can be performed using
• the C-style cast,
• the C++ function-style cast, or
• one of the C++ cast operators.

Programming Fundamental 8
Explicit Type Conversion
int main()
{
float num = 98.74;
int x1 = (int) num; // C-style cast
int x2 = int (num); // C++ function-style cast
int x3 = static cast(num); // C++ cast operator
cout << ‘‘x1 = ’’ << x1 << endl ;
cout << ‘‘x2 = ’’ << x2 << endl ;
cout << ‘‘x3 = ’’ << x3 << endl ;
return 0;
}
• The following is the output of the above example:
x1 = 98
x2 = 98
x3 = 98

Programming Fundamental 9
Explicit Type Conversion: Example
int main()
{
int intVar = 1500000000;
intVar = (intVar * 10)/10;
cout << ‘‘intVar = ’’ << intVar << endl ;
int intVar = 1500000000;
intVar = (static cast(intVar) * 10)/10;
cout << ‘‘intVar = ’’ << intVar << endl ;
return 0;
}
• Here is the program output:
intVar = 211509811 // wrong answer
intVar = 1500000000 // correct answer

Programming Fundamental 10
Excercise

Q). Write and run a program that simulates a simple calculator. It reads two integers
and a character. If the character is a ‘+’, the sum is printed; if it is a ‘-’, the
difference is printed; if it is a ‘*’, the product is printed; if it is a ‘/’, the quotient is
printed; and if it is a ‘%’, the remainder is printed.

Programming Fundamental 11
REFERENCES
1. Deitel, Paul J., and Harvey M. Deitel. "C++: how to program/Paul Deitel,
Harvey Deitel." (2012). (Chapter 4)
2. Zak, Diane. Introduction to Programming with C++. Cengage Learning, 2013.

12
Programming Fundamental
THANKS

You might also like