Anda di halaman 1dari 4

Object Oriented Programming TE-53

Lab3
CLO 1,2,3 PLO 2

Arguments pass by Reference ,Default Arguments, Inline functions, overloaded


functions
Objectives:

Pass by reference
Default Arguments
Inline Functions
Overloaded Functions

1. Arguments passed by value and by reference.

Until now, in all the functions we have seen, the arguments passed to the functions have been passed by
value. This means that when calling a function with parameters, what we have passed to the function
were copies of their values but never the variables themselves. One of the major disadvantages of pass
by value is that all arguments passed by value are copied to the parameters. When the arguments are
large structs or classes, this can take a lot of time. References provide a way to avoid this penalty. When
an argument is passed by reference, a reference is created to the actual argument (which takes minimal
time) and no copying of values takes place. This allows us to pass large structs and classes with a
minimum performance penalty. However, this also opens us up to potential trouble. References allow
the function to change the value of the argument, which in many cases is undesirable. If we know that a
function should not change the value of an argument, but dont want to pass by value, the best solution
is to pass by const reference. You already know that a const reference is a reference that does not allow
the variable being referenced to be changed. Consequently, if we use a const reference as a parameter,
we guarantee to the caller that the function will not (and cannot) change the argument!

2. Default arguments in parameters.


When declaring a function we can specify a default value for each of the last parameters. This value will
be used if the corresponding argument is left blank when calling to the function. To do that, we simply
have to use the assignment operator and a value for the arguments in the function declaration. If a
value for that parameter is not passed when the function is called, the default value is used, but if a
value is specified this default value is ignored and the passed value is used instead. For example:

1 // default values in functions


2 #include <iostream.h>
3 int divide (int a, int b=2)
4 {
5 int r;
6 r=a/b;
7 return (r);
8 }
9 void main ()
10 {
11 cout << divide (12);

1
Object Oriented Programming TE-53

12 cout << endl;


13 cout << divide (20,4);
14 }
3. Inline Function

Inline functions are not always important, but it is good to understand them. The basic idea is to save
time. Once you define an inline function, using the 'inline' keyword, whenever you call that function the
compiler will replace the function call with the actual code from the function. How does this make the
program go faster? Using inline functions can speed up your program by in-lining the right function.
Instead of calling the function every time it is invoked, the compiler will replace the function call with a
copy of the function body. If it's a small function which gets called a lot, this can sometimes speed things
up. Why not to inline everything? The reason for this is since the compiler will copy the entire function
body every time the function is called, if it is a large function (more than three or four lines), in-lining can
increase the size of your executable program significantly. You may want to try to see what kind of speed
gains you can achieve by in-lining, and also compare the increase in the size of your executable.

4. Overloading functions

You overload a function name f by declaring more than one function with the name function in the same
scope. The declarations of function must differ from each other by the types and/or the number of
arguments in the argument list. When you call an overloaded function named f, the correct function is
selected by comparing the argument list of the function call with the parameter list of each of the
overloaded candidate functions with the name f. A candidate function is a function that can be called
based on the context of the call of the overloaded function name. The function cannot be overloaded using
different return type alone.

Consider a function print, which displays an int. As shown in the following example, you can overload
the function print to display other types, for example, double and int. You can have two functions with the
same name, each performing a similar operation on a different data type:

#include <iostream.h>
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void main() {
print(10);
print(10.10);
}
The following is the output of the above example:

Here is int 10
Here is float 10.1

2
Object Oriented Programming TE-53

Lab Task 3
CLO 2,3 PLO 2

Arguments Pass by Reference, Inline Functions, Overloaded Functions


Objectives:
Pass by Reference
Default Arguments
Inline functions
Overloaded Functions

Default Arguments
Q1) Write a function named calculator that take three arguments two floating numbers and a char(
operator), perform the operation based on the operator and returns a float number back to the
main. If no operator is provided to the function in function call, it should take addition as default.

Q2) Rewrite the palindrome function you did in last lab with certain changes now the function takes
two arguments from the main function a long int number and number of digits in that number.
The second argument number of digits is optional, if it is not given number of digits= 5 will be taken
as default arguments. The function should return whether the number is palindrome 1 or not 0.

Inline Functions
Q3) Write a function that takes two integer x and y as arguments from the main program and return 1 if
the integers are equal, return -1 if the x>y and return 0 if x<y. In main program get the starting time
as unsigned int start = clock();(clock() gives time is millisecond)before calling the function and ending
time unsigned int end = clock(); after the function call. Then calculate the total time taken in program
execution by unsigned int total=end-start. Print the total time taken for the function execution. Use
time.h as a header file for using clock(). You the should get the output like this:

Q4) Rpeat the above program using inline function


Q5) Write an inline functions that takes two integers as input say x and y(x being base and y being
exponent) and calculate the power x^y without using any built in function.

3
Object Oriented Programming TE-53

Overloading Functions
Q6) Calculate the area of circle, rectangle and triangle using function overloading. Write three
functions named area that prints the area of a circle, rectangle and triangle the prototype of these
functions are given below:

a. void area(int); //circle

b. void area(int,int); //rectangle

c. void area(float ,int,int); //triangle

a) Call each function within the main, Write in comments the observation you made from the
output.

b) Change the main such that calls to the function are menu driven such as shown below

Q7) Write a program to model a simple calculator which should be able to do +,-,*,/. All these
operations must be developed as separate functions. Each with two variants. First variant should
take two integers as arguments and returns an integer back to the calling function in this case
main, second variant should take two double numbers as input and return a double to the calling
function which is main. The program should ask the user to enter two operands and the operator.
The function to be called should be selected on the basis of the operator selected.

Anda mungkin juga menyukai