0% found this document useful (0 votes)
35 views26 pages

8.5.1 Standard Header File 8.5.2 Standard Functions

The document discusses header files and standard functions in C++. It explains that header files contain declarations of functions, classes, and constants, and that including the appropriate header files allows a program to use those declarations. Specifically: 1) The <iostream> header file contains functions like cout and cin for input/output. 2) The <cmath> header file contains common math functions like pow() and sqrt(). 3) Header files are included in a program using the #include preprocessor directive followed by the header file name enclosed in angle brackets. This makes the declarations in the header file available.

Uploaded by

pengjeong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views26 pages

8.5.1 Standard Header File 8.5.2 Standard Functions

The document discusses header files and standard functions in C++. It explains that header files contain declarations of functions, classes, and constants, and that including the appropriate header files allows a program to use those declarations. Specifically: 1) The <iostream> header file contains functions like cout and cin for input/output. 2) The <cmath> header file contains common math functions like pow() and sqrt(). 3) Header files are included in a program using the #include preprocessor directive followed by the header file name enclosed in angle brackets. This makes the declarations in the header file available.

Uploaded by

pengjeong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

8.

5 Header File and Functions

8.5.1 Standard Header File

8.5.2 Standard Functions


Learning Outcomes:

At the end of this lesson, students


should be able to :

 use standard header file in a program

 use appropriate functions in a program


Let’s Recall…

Have u seen header files


before?

Where???
i.

ii.

iii.

iv.

v.
Header File

 Header file is a text file containing small bits of


program code, which is used to describe some of
the objects or functions used in a program

 Header file will be included in the program using


#include (one of the preprocessor directives)
Standard Header File

#include <iostream>
#include <cstdlib>
#include <cmath>

Preprocessor Directive Header File


Header File

 Each header file contains specific functions that


we can use to accomplish certain programming
tasks.

 Example:
Header file iostream contains functions cin and
cout.
Header file & function

#include <iostream>

int main()
{
cout<<“I Love Computer Science.”;
return 0;
}
Let’s Recall…

 The #include directive causes a copy of a specified header


file to be included in the program being compiled.

 Example:
 #include <iostream>
 means it will include the header file iostream so that
the program can use related functions (i.e cin and
cout)
Examples of Standard Header Files

 iostream  cstring  cerrno


 cmath  climits  cfloat
 csignal  clocale
 cassert  ctime
 cctype  cstdlib
Declaring Header Files

General Form:
#include <Header File>

Examples:
#include <iostream>
#include <cmath>
Header file <iostream>

 This header file contains functions to


perform input and output

 2 common functions in <iostream> :


• cin
• cout
cin>>

 stands for console input


 received input/data entry from the user via
the keyboard
 use with the insertion operator >>

 Example :
cin>>radius;
cout<<

 stands for console output


 to display output to the screen
 use with the extraction operator <<

 Example :
cout<<“Area = \n”;
cout<<area;
Usage of cin>> and cout<<

#include <iostream>
int main()
{
int number;
cout<<"Enter your favorite number: ";
cin>>number;
cout<<"Number entered: " <<number ;
return 0;
} **masukkan print screen programming
codeblock
Header file <cmath>

 cmath declares a set of functions to compute


common mathematical operations and
transformations.
 <cmath> provides functions such as:
 pow( ) – power root
 sqrt( ) – square
 cos
 sin
 tan
pow( )

pow(base,exponent)

 Means raise to power


 Return base raised to the power exponent

 Example:
cout<<pow(7,3); //means 73
pow : Example
/* pow example */

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout<<"3 ^ 2 = "<<pow(3,2)<< endl;
cout<<"7 ^ 3 = "<<pow(7,3)<< endl;
cout<<"4.73 ^ 12 = "<<pow(4.73,12)<<endl;
cout<<"32.1 ^ 1.54 = "<<pow(32.1,1.54)<< endl;
return 0;
}
pow : Output
sqrt( )

sqrt(x)
 Returns the square root of x.

 Example:
Y = sqrt(49);
cout<<Y;
sqrt : Example
/* sqrt example */
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
double param, result;
param = 1024.0;
result = sqrt(param);
cout << "sqrt(1024) = " << result;
return 0;
}
sqrt : Output
Summary

 Header files are files that contain certains functions


that performs specific tasks.

 Programmers need to include the appropriate header


files in order to use specific library functions.
Header File & Functions

Header File Object/Function


iostream cin, cout

cmath pow, sqrt


Sample Question 1:

1. List two (2) functions that are provided by each of the


standard header file below:

<iostream> <cmath>

i) _____________ i) ______________
ii) _____________ ii) ______________
Sample Question 2:
2. Given the program below
#include <iostream>
int main()
{
double a, b, c, root1, root2;
cout<<"Please enter values for a, b and c: ";
cin>>a>>b>>c;
root1 = -b + sqrt(pow(b,2)- 4 *a *c))/(2*a);
root2 = -b - sqrt(pow(b,2)- 4 *a *c))/(2*a);
cout<<“The roots are: "<<root1<<root2;
return 0;
}
Write one preprocessor directive that should be included in the
program above.
________________________________________________

You might also like