Page 69 - DCAP404 _Object Oriented Programming
P. 69
Object-oriented Programming
Notes for (int j = 0; j < num; j ++)
cout << ch;
cout << endl;
}
The function rchar() is called 10 times in the main( ) function with different number of arguments
passed to it. This is allowed in C++.
The first function call to rchar( ) takes no arguments in the main( ) function. The function
declaration provides default values for the two arguments required by rchar(). When values are
not provided, the compiler supplies the defaults ‘*’ and 10.
In the second call, one argument is missing which is assumed to be the last argument. rchar()
assigns the single argument ‘=’ to the ch parameter and uses the default value 10 for num.
In the third case, both arguments are present and no defaults are considered. A few points worth
remembering here.
The missing or default arguments must be the trailing arguments, those at the end of the
argument list. The compiler will flag an error when you leave something out. Default arguments
may be useful when an argument has the same value.
void abc (int a, float b, int c = 10, char d = ‘P’); / / c o r r e c t
defaulting!
The above declaration is correct because no un-defaulted arguments appear after defaulted
arguments. For this reason the following declaration is incorrect.
void abc (int a, float b = 2.5, int c, char d = ‘P’); //incorrect
defaulting!
Did u know? How to use two default parameter values?
#include <iostream>
int f(int length, int width = 25, int height = 1);
int main()
{
int length = 100;
int width = 50;
int height = 2;
int area;
area = f(length, width, height);
std::cout << “First time area equals “ << area << “\n”;
area = f(length, width);
std::cout << “Second time area equals “ << area << “\n”;
area = f(length);
std::cout << “Third time area equals “ << area << “\n”;
62 LOVELY PROFESSIONAL UNIVERSITY