PF Week 4
PF Week 4
OBJECT:
• To understand some library functions.
• To demonstrate setw() manipulator
• Data type automatic conversion and casting.
• The bool Data Type
LIBRARY FUNCTIONS
Library functions which are also called as “built-in” functions are already available and implemented in C++. We
can directly call these in our program any time. Library functions in C++ are declared and defined in special files
called “Header Files” which we can reference in our C++ programs using the “include” directive. For Example, to
include all the built-in functions related to math, we should include <cmath> header as follows:
#include <cmath>
HEADER FILES
As with cout and other such objects, you must #include a header file that contains the declaration of any library
functions you use. To use the sqrt() function, the required header file is CMATH. If you don’t include the
appropriate header file when you use a library function, you’ll get an error message.
LIBRARY FILES
Files containing library functions and objects will be linked to your program to create an executable file. These
files contain the actual machine-executable code for the functions. Such library files often have the extension
“.LIB”. The sqrt() function is found in such a file. It is automatically extracted from the file by the linker, and the
proper connections are made so that it can be called (that is, invoked or accessed). Your compiler takes care of
all these details for you, so ordinarily you do not need to worry about the process.
CASTING
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. Type casting is a way to convert a variable from one data
type to another data type. For example, if you want to store a 'long' value into a simple integer then you can
type cast 'long' to 'int'. You can convert the values from one type to another explicitly.
There are several kinds of casts in Standard C++: static casts, dynamic casts, reinterpret casts, and const casts.
Here we’ll be concerned only with static casts.
There are three major ways in which we can use explicit conversion in C++. They are:
• C-style type casting (also known as cast notation)
• Function notation (also known as old C++ style type casting)
• Type conversion operators
(type_name) expression
FUNCTION-STYLE CASTING
We can also use the function like notation to cast data from one type to another.
The syntax for this style is:
type_name(expression);
aCharVar = static_cast<char>(anIntVar);
#include <iostream>
using namespace std;
int main() {
return 0;
}
AUTOMATIC CONVERSION
Type conversions can be implicit which is performed by the compiler automatically, When two operands of
different types are encountered in the same expression, the lower-type variable is converted to the type of
the higher-type variable. Types are considered “higher” or “lower,” based roughly on the order shown below:
Highest Lowest
long double Double Float long int short char
The problem we had in the previous exercise was due to the Automatic Conversion. Consider the following
code:
int a = 11;
int b = 2;
float ans = a / b;
The value in ans we were getting was 5 instead of 5.5
This is because when 2 int data types are involved in a expression the result in automatically converted in to
int which in the only datatype in the expression.
The problem can be solved by casting one of the data type into float by using the statement like this:
float ans = (float)a / b;
#include <iostream>
int main() {
int a = 10;
int b = 3;
int x = 10;
float y = 3;
#include <iostream>
int main() {
cout<<"2/3="<<2/3<<endl;
cout<<"2/3="<<(float)(2/3)<<endl;
cout<<"2/3="<<(float)2 /3 <<endl;
return 0;
}
DECLARATION
Printing the bool value on screen will result in 0 for false and 1 for true. For Example:
Cout<<var;
EXERCISE 1:
An isosceles triangle is a triangle that has two sides of equal length.
• Write a program which has two variable a and b, where a is the value of two equal sides of triangle
and b is the value of base. You can use float data type for variable a and b.
• Now create another variable area with double data type.
• Now take the input of variable a and b from user by printing some proper messages and using cin>>.
Also warn user not to give zero or negative value in those messages.
• Now by using sqrt() function and pow() function calculate the Area of Isosceles triangle by using the
above given formula. In last print the value of area which is calculated by the program.
• For Example, sif user give a=3 and b=4 then Area=4.47214
EXERCISE 2:
Write a program which takes the radius of a circle and calculates the area and circumference of the circle. Use
pre define PI constant from cmath library (Search on internet for it). Find the formula of both radius and area
from internet or any book.
Input the radius(1/2 of diameter) of a circle : 5
The area of the circle is : 78.5397
The circumference of the circle is : 31.4159