Custom Search

IOE, Pulchowk Campus Tutorial

IOE, Pulchowk Campus
18th Dec 2004
Prepared by: DSB and JRT
Manipulators are operators that are used to format the data display. The most commonly used manipulators are endl
and setw. The endl manipulator, when used in an output statement, causes a linefeed to be inserted. It has same effect
as using the newline character “\n” in C. For example
cout<<” LODGING “<y)
return x; else
return y;
Here the function returns reference x or y, but not the values, then the function call will yield a reference to either a
or b depending on their values. a is assigned to -1 if it is larger, otherwise –1 to b.
Structure with function
The value of structure variable can be passed as a parameter to a function. For example
void show(struct stu);
struct stu
Overloaded function
Overloaded function appears to perform different operation depending on the kind of data sent to it. It performs one
operation on one kind of data but another operation on a different kind. The reason behind using the overloaded
function is because of its convenient to use the same function name for different operation.
void convert(); //takes no argument
void convert(int n); //takes one argument of type int
IOE, Pulchowk Campus
18th Dec 2004
Prepared by: DSB and JRT
void convert(float,int); //takes one argument of type float and another of type int.
It uses the number of arguments, and their data types, to distinguish one function from another.
Inline function
We know that function save memory space but take some extra time. If the functions are short, we may put in the
function directly in the line with the code in the calling program. But the trouble with repeatedly inserting the same
code is that you lose the benefits of the program organization and clarity that come with using function. If the
function is very short, the instructions necessary to call it may take up as much space as the instructions within the
function body, so that there is not only a time penalty but a space penalty as well. The solution to this is the inline
function. This kind of function is written like a normal function in the source file but complies into inline code instead
of into a function. Beside this the source file remains well organized and easy read, since the function is shown as a
separate entity. Functions that are very short, say one or two statements are candidates to be inlined.
inline(keyword) float(retutn type) convert(int n)
inline float convert( int n)
Here all we need is the keyword inline in the function definition.
Default Arguments
The function can be called without specifying all its arguments. This won’t work on just any function that is the
function declaration must provide default values for those arguments that are not specified.
Let us take an example
void dearg(char ='?',int=25); //declaration with default arguments
dearg('<'); dearg('>',30);
void dearg(char ch,int n)
Here the function dearg( ) takes two arguments. It’s called three times from main( ) program. The first time it is
called with no arguments, second time with one argument and third time with two. The first and second provides
default arguments, which will be used if the calling function doesn’t supply them. The default argument follows an
equal sign, which is placed directly after name. It is also possible to use variable name. If one argument is missing it
assumed to the last argument as we show in second.
Use manipulators where seems to be useful.
1. Write a program to set a structure to hold a date (mm,dd and yy), assign values to the members of the
structure and print out the values in the format 11/28/2004 by function. Pass the structure to the function.
2. Write a program using the function overloaded that convert inches to feet and feet to inches. Use function
with no argument, one argument and two arguments. Decide yourself the types of arguments. Use pass by
reference in any one of the function above. Write a function that is passed two temperatures by reference and
sets the larger of the two numbers to 100 by using return by reference.
3. Assume that employee will have to pay 10 percent income tax to the government. Ask user to enter the
employee salary. Use inline function to display the net payment to the employee by the company.
4. Write a program that displays the current monthly salary of chief executive officer, information officer, and
system analyst, programmer that has been increased by 9, 10, 12, and 12 percentages respectively in year
2004. Let us assume that the salaries in year 2003 are
Chief executive officer Rs. 35000/m
Information officer Rs. 25000/m
System analyst Rs. 24000/m
Programmer Rs. 18000/m
Use proper default arguments.

No comments:

Post a Comment