Top Banner
Prepared by Mr.M.Syed Shahul Hameed M.E., PhD 1 Page College of Arts and Sciences Department of Mathematical and Physical Sciences Computer Science Section COMP 222 Object Oriented Programming (OOP) (2 Hours Lectures and 2 Hours lab)
23

COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Jul 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD1Page

College of Arts and Sciences

Department of Mathematical and Physical Sciences Computer Science Section

COMP 222 Object Oriented Programming

(OOP)

(2 Hours Lectures and 2 Hours lab)

Page 2: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD2Page

CHAPTER – 1

ADVANCED CONCEPTS IN FUNCTIONS

Page 3: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD3Page

Default Argument (or) Default Parameter A default value can be passed as an actual argument to the function call. It is used when the function call does not specify an actual argument for the corresponding position. It must be the rightmost actual argument(s) of function call. It should be specified with the function prototype. Example Program #include<iostream> using namespace std; void add(int,int,int=45); void printline(char,char='*',int=35); int main() {

int x,y; cout<<"Enter the value for x"<<endl; cin>>x; cout<<"Enter the value for y"<<endl; cin>>y; printline('#','-',25); add(x,y); printline('='); return(0);

} void add(int a,int b,int c) {

int d; d=a+b+c; cout<<"The result of adding three int is"<<d<<endl;

}

Page 4: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD4Page

void printline(char p,char q,int r) {

int i; for(i=0;i<=r;i++) { cout<<p; }

cout<<endl; for(i=0;i<=r;i++) { cout<<q; }

cout<<endl; } Output Enter the value for x 12 Enter the value for y 35 ########################## -------------------------- The result of adding three int is92 ==================================== ************************************ In the above program add() and printline() functions are used with default arguments. This default argument’s value should be given in the prototype and it’s a right most argument in the list. For add() function there are three integer values are passed. Third integer value 45 is fixed as a default argument value. So in the function call add(), if the third value is not passed, the compiler will take the default argument value 45 and it is given to formal argument. So at least the first two arguments should be

Page 5: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD5Page

given. If all the three arguments are given, compiler will take these three arguments and they are given to formal arguments. For printline() function second and third arguments are fixed as default arguments. So when printline() function call, if the second and third arguments are not passed the compiler will take default arguments * and 35. So at least the first argument should be given in the function call. If all the three arguments are given, compiler will take these three arguments and they are given to formal arguments. Function Overloading An overloaded function appears to perform different activities depending on the kind of data sent to it. Overloading is like the joke about the famous scientist who insisted that the thermos bottle was the greatest invention of all time. Why? “It’s a miracle device”, he said. “It keeps hot things hot, but cold things it keeps cold”. How does it know? Function overloading is same like thermos bottle. It performs one operation on one kind of data but another operation on a different kind of data. That is, one function can perform the same operation depending on the different number of arguments, depending on the different types of arguments, depending on the different order (sequence) of arguments and depending on the different return type of functions

1. Depending on the different number of arguments Example Program #include<iostream> using namespace std; void add(int,int); void add(int,int,int);

Page 6: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD6Page

int main() {

int x,y,z; cout<<"Enter the value for x"<<endl; cin>>x; cout<<"Enter the value for y"<<endl; cin>>y; cout<<"Enter the value for z"<<endl; cin>>z; add(x,y); add(x,y,z); return(0);

} void add(int a,int b) {

int c; c=a+b; cout<<"The result of adding two integers is"<<c<<endl;

} void add(int a,int b,int c) {

int d; d=a+b+c; cout<<"The result of adding three integers is"<<d<<endl;

} Output Enter the value for x 11 Enter the value for y 45 Enter the value for z 29 The result of adding two integers is56 The result of adding three integers is85

Page 7: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD7Page

In the above program we are having two functions named add, but both of them having different number of arguments with the same data type int. First add() function having two int arguments and second add() function having three int arguments. So calling add() function with two integer arguments goes to the definition of add() function with two int arguments. Calling add() function with three integer arguments goes to the definition of add() function with three integer arguments. Thus the function add() is overloaded with different number of arguments.

2. Depending on the different types of arguments

Example Program #include<iostream> using namespace std; void add(int,int); void add(float,float); int main() {

int x,y; float a,b; cout<<"Enter the value for x"<<endl; cin>>x; cout<<"Enter the value for y"<<endl; cin>>y; cout<<"Enter the value for a"<<endl; cin>>a; cout<<"Enter the value for b"<<endl; cin>>b; add(x,y); add(a,b); return(0);

}

Page 8: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD8Page

void add(int a,int b) {

int c; c=a+b; cout<<"The result of adding two integers is"<<c<<endl;

} void add(float a,float b) {

float c; c=a+b; cout<<"The result of adding two float value is"<<c<<endl;

} Output Enter the value for x 12 Enter the value for y 34 Enter the value for a 56.56 Enter the value for b 25.7 The result of adding two integers is46 The result of adding two float value is82.26 In the above program we are having two functions named add, but both of them having different data types of arguments. First add() function having two int arguments and second add() function having two float arguments. So calling add() function with two integer arguments goes to the definition of add() function with two int arguments. Calling add() function with two float arguments goes to the definition of add() function with two float arguments. Thus the function add() is overloaded with different data types of arguments.

Page 9: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD9Page

3. Depending on the different order(sequence) of arguments Example Program #include<iostream> using namespace std; void add(int,float); void add(float,int,float); int main() {

int x,y; float a,b,c; cout<<"Enter the value for int x"<<endl; cin>>x; cout<<"Enter the value for int y"<<endl; cin>>y; cout<<"Enter the value for float a"<<endl; cin>>a; cout<<"Enter the value for float b"<<endl; cin>>b; cout<<"Enter the value for float c"<<endl; cin>>c; add(x,a); add(b,y,c); return(0); }

void add(int a,float b) {

float c; c=a+b; cout<<"The result of adding one int,one float value is"<<c<<endl;

}

Page 10: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD11Page

void add(float a,int b,float c) {

float d; d=a+b+c; cout<<"The result of adding one int and two float value is"<<d<<endl;

} Output Enter the value for int x 22 Enter the value for int y 67 Enter the value for float a 5.67 Enter the value for float b 88.5 Enter the value for float c 12.34 The result of adding one int,one float value is27.67 The result of adding one int and two float value is167.84 In the above program we are having two functions named add, but both of them having different order(sequence) of arguments. First add() function having one int argument, one float argument and second add() function having one int, one float and another float arguments. So calling add() function with one int argument, one float argument goes to the definition of add() function with one int argument, followed by one float argument. Calling add() function with one int, one float, and another float arguments goes to the definition of add() function with one int, one float and another float arguments. Thus the function add() is overloaded with different order(sequence) of arguments.

Page 11: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD11Page

4. Depending on the different return types of functions Example Program #include<iostream> using namespace std; void add(); double add(double,double); int main() {

double x,y,r1; add(); cout<<"Enter the double value for x"<<endl; cin>>x; cout<<"Enter the double value for y"<<endl; cin>>y; r1=add(x,y); cout<<"Result of adding double is"<<r1<<endl; return(0);

} void add() {

int a,b; cout<<"Enter the int value for a"<<endl; cin>>a; cout<<"Enter the int value for b"<<endl; cin>>b; int res1; res1=a+b; cout<<"Result of adding int is"<<res1<<endl;

} double add(double a,double b) {

double res1; res1=a+b; return(res1);

}

Page 12: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD12Page

Output: Enter the int value for a 44 Enter the int value for b 78 Result of adding int is122 Enter the double value for x 35.689 Enter the double value for y 78.123 Result of adding double is113.812 Recursive Function (or) Recursion The term recursion refers to a situation in which a function calls itself either directly or indirectly. Indirect recursion occurs when one function calls another function that then calls the first function. C++ allows recursive functions, and they can be useful in some situations. For example, recursion can be used to calculate the factorial of a number. The factorial of a number x is written x! and it is calculated as follows: x! = x * (x-1) * (x-2) * (x-3) * ... * (2) * 1 However, you can also calculate x! like this: x! = x * (x-1)! Going one step further, you can calculate (x-1)! using the same procedure: (x-1)! = (x-1) * (x-2)! You can continue calculating recursively until you're down to a value of 1, in which case you're finished. Example Program #include<iostream> using namespace std; int factorial(int);

Page 13: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD13Page

int main() {

int number,res; cout<<"Enter the number"<<endl; cin>>number; res=factorial(number); cout<<"Factorial of the given number is"<<res<<endl; return(0);

} int factorial(int x) {

int f; if(x>1) {

f=x*factorial(x-1); return(f);

} else {

return(1); }

} Output Enter the number 5 Factorial of the given number is120 As a recursive function the function factorial() is called from main() for the first time. Then the function factorial() is called from the same function(by itself). For example the given number is 5, then it is passed to the factorial() function for the first time. Then inside the factorial() function if x>1 then the right hand side of assignment statement

Page 14: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD14Page

f=x*factorial(x-1) is calculated. i.e., right hand side x*factorial(x-1) is calculated. i.e., 5*factorial(5-1) is performed and it is returned. So for the second time factorial() function is called by itself. This recursion is continued up to x>1. When x<1 the value 1 is returned. 5!=5*(5-1)! 4!=4*(4-1)! 3!=3*(3-1)! 2!=2*(2-1)! 1!=1 After this recursive step at last value of 1!=1 is returned. So value of 2!=2, this value is returned to 3!. So the value of 3!=6, this value is returned to 4!. So the value of 4!=24, this value is returned to 5!. So the value of 5!=120, this value is returned to main(). Thus main() receives this value in variable res. Then the value of res is displayed. Inline Functions Suppose that a program frequently requires finding the absolute value of an integer quantity. For a value denoted by n, this may be expressed as: (n>0)? n:-n However, instead of replicating this expression in many places in the program, it is better to define it as a function: int absolute(int n) {

int r; r=(n>0)?n:-n; return(r);

}

Page 15: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD15Page

The function version has a number of advantages. 1. It leads to a more readable program. 2. It is reusable. 3. It avoid undesirable side effects when the argument is itself

an expression with side-effects. The disadvantage of the function version, however, is that its frequent use can lead to a considerable performance penalty due to the overheads associated with calling a function. For example, if absolute() is used within a loop which is iterated thousands of times, then it will have an impact on performance. The overhead can be avoided by defining absolute() as an inline function: inline int absolute(int n) {

int r; r=(n>0)?n:-n; return(r);

} The effect of this is that when absolute() is called, the compiler, instead of generating code to call absolute(), expands and substitutes the body of absolute in place of the call. The qualifier inline before a function’s return type in the function definition is actually a request to the compiler. That is, each time there is a function call in the main(), the actual code from the function is inserted, instead of a jump to the function. Note: Generally, the use of inline should be restricted to simple, frequently used functions. A function which contains anything more than a couple of statements is unlikely to be a good candidate. Use of inline for excessively long and complex functions is almost certainly ignored by the compiler.

Page 16: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD16Page

In general, an inline function executes faster than a normal function, but requires more memory. Function Templates A function template is a “generic” function that can work with any data type. The programmer writes the specifications of the function, but substitutes parameters for data types. When the compiler encounters a call to the function, it generates code to handle the specific data type(s) used in the call. Overloaded functions make programming convenient because only one function name must be remembered for a set of functions that perform similar operations. Each of the functions, however, must still be written individually, even if they perform the same operations. For example, suppose a program uses the following overloaded square() functions. int square(int number) {

int r; r=number*number; return(r);

} double square(double number) {

double r; r=number*number; return(r);

} The only differences between these two functions are the data types of their parameters and data types of their return values. In

Page 17: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD17Page

situation like this, it is more convenient to write a function template than an overloaded function. Function templates allow you to write a single function definition that works with many different data types, instead of having to write a separate function for each data type used. A function template is not an actual function, but a “mold” the compiler uses to generate one or more functions. When writing a function template, you do not have to specify actual types for the parameters, return value, or local variables. Instead, you use a type parameter to specify a generic data type. When the compiler encounters a call to the function, it examines the data types of its arguments and generates the function code that will work with those data type. (The generated code is known as template function) Here is the function template for the square() function template <class T> T square(T number) {

T r; r=number*number; return(r);

} The beginning of a function template is marked by a template prefix, which begins with the keyword template. Next is a set of angled brackets that contains one or more generic data types used in the template. A generic data type starts with the keyword class followed by a parameter name that stands for the data type. The example just given only uses one, where is named T.(If there were more, they would be separated by commas).

Page 18: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD18Page

After this, the function definition is written as usual, except the type parameters are substituted for the actual data type names. In the example the function header reads T square(T number) Example program (By function Overloading) #include<iostream> using namespace std; int square(int); double square(double); int main() {

int a,res1; double b,res2; cout<<”Enter an integer value for a”<<endl; cin>>a; cout<<”Enter a double value for b”<<endl; cin>>b; res1=square(a); cout<<”The squared value of a is”<<res1<<endl; res2=square(b); cout<<”The squared value of b is”<<res2<<endl; return(0);

} int square(int number) {

int r; r=number*number; return(r);

}

Page 19: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD19Page

double square(double number) {

double r; r=number*number; return(r);

} Output Enter an integer value for a 12 Enter a double value for b 2.5 The squared value of a is144 The squared value of b is6.25 Example program (By Function template) #include<iostream> using namespace std; template <class T> T square(T number) {

T r; r=number*number; return(r);

} int main() {

int a,res1; double b,res2; cout<<"Enter an integer value for a"<<endl; cin>>a; cout<<"Enter a double value for b"<<endl; cin>>b; res1=square(a); cout<<"The squared value of a is"<<res1<<endl;

Page 20: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD21Page

res2=square(b); cout<<"The squared value of b is"<<res2<<endl; return(0);

} Output Enter an integer value for a 12 Enter a double value for b 2.5 The squared value of a is144 The squared value of b is6.25 Note: Whenever using function templates in program, define the function template before the main() function. Thus it omits the function declaration. Refreshing your knowledge

1. Write a C++ program to read 2 integers and uses a function add() to find their sum and display the result.

2. Write a C++ program to read 2 integers and uses a function

add() to find their sum and return the result

3. Write a C++ program to read 2 integers and pass the arguments by reference to a function add() to find their sum and display the result

4. Write a C++ program to read 2 integers and pass the

arguments by reference to a function add() to find their sum and return the result

Page 21: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD21Page

5. Write a C++ program to read 2 floats and uses 4 functions to find their sum, subtraction, multiplication and division.

a) sum() function gets two floats and display the result. b) subtraction() function gets two floats and return the result. c) multiplication() function gets two floats by reference and

display the result. d) division() function gets two floats by reference and return

the result. Exercises 1. Write declarations for two overloaded functions named bar().

They both return type int. The first takes one argument of type char, and the second takes two arguments of type char. If this is impossible, say why?

2. Write a declaration for a function called blyth() takes two arguments and return type char. The first argument is type int, and the second is type float with a default value of 3.14159

3. Write a C++ program to find the factorial of a given number by

using a recursive function named factorial().

4. Write a C++ program to compute the xy by using a recursive function named power().

5. Write a C++ program to print the Fibonacci series

0 1 1 2 3 5 8 13 21 . . . by using a recursive function named fibo().

6. Write a C++ program to find the GCD of two numbers by using

a recursive function named GCD().

7. Write a function takes an integer array and its size as a parameter and prints this array backwards.

Page 22: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD22Page

8. Write a function takes an integer array and its size as a parameter and finds the maximum value.

9. Write a function called convert() that converts the Pounds to

Kilograms. It should take an argument of type float and return an argument of same type. Write a main() function that gets 3 computers weights in Pounds, calls convert() function 3 times, and displays the results. Is this necessary to implement the convert() as an inline function? If so, explain your answer. Hint: 1 pound=0.453592 Kilograms.

10. Write a C++ program to find the maximum value among

three values of integers, doubles and characters by using a template function.

11. Write a C++ program to swap the value among two values of integers, doubles and characters using pass reference of a template function.

12. Write a template for the two functions minimum and maximum. The minimum() function should accept two arguments and return the value of the argument that is the lesser of the two. The maximum() function should accept two arguments and return the value of the argument that is the greater of the two. Write a simple C++ program that demonstrates the function templates with various data types.

13. True or False. Function template always allows only one

generic type.

Page 23: COMP 222 Object Oriented Programming · Page 3 Prepared by Mr.M.Syed Shahul Hameed M.E., PhD Default Argument (or) Default Parameter A default value can be passed as an actual argument

Prepared by Mr.M.Syed Shahul Hameed M.E., PhD23Page

14. What is the output of the following C++ program: #include <iostream> using namespace std; int avg(int a=5,int b=7,int c=3); int main( ) {

cout<< "Avg1="<<avg()<<endl; cout<<"Avg2="<<avg(6,3)<<endl; cout<<"Avg3="<<avg(6,4,8)<<endl; return(0);

}

int avg(int a, int b, int c) {

return((a+b+c)/3); }