C++ Default Arguments and command line Arguments

C++ Default Arguments

C++ Default Arguments:

C++ default arguments in which data is initialized during the function declaration are called default arguments.

If the values of the c++ default arguments are specified in the function declaration then the arguments of the function in the function call can be omitted. If the arguments are omitted in function call then the default values are automatically passed the function definition.

For example, in the following program example, two c++ default arguments parameters are defined in the function declaration.



Example how to use C++ Default Arguments in  Programming :

Output:

In the above program the function “temp” has two c++ default arguments parameters, first of string type and second of integer type. These values are initialized at the time of function declaration. When the function “temp” is called without using parameter values, the c++ default arguments parameter values are passed to the function definition and the string “Programming” will be printed twice.

In the second call, the parameter value “Digest” and “10” are also passed with the function call. In this case the string “Islamabad” will be printed ten times.

Consider the following function declaration:

int MyFunc(int a, int b, int c = 12);


This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:

int result = MyFunc(1, 2, 3);

result = MyFunc(1, 2);

In the first case the value for the default arguments called c is specified as normal. In the second case, the argument is omitted, and the default value of 12 will be used instead.

There is no means to know if the argument has been specified by the caller or if the default value was used.

The above-mentioned method is especially useful when one wants to set default criteria so that the function can be called with or without parameters. Consider the following:

void PrintGreeting(std::ostream& stream = std::cout) {

  stream << “hello world!”;

}

The function call:

PrintGreeting();

will by default print “hello world!” to the standard output std::cout (typically the screen). On the other hand, any object of type std::ostream can now be passed to the same function and the function will print to the given stream instead of to the standard output.

PrintGreeting(std::cerr);


Because default arguments’ values are “filled in” at the call site rather than in the body of the function being called, virtual functions take their default argument values from the static type of the pointer or reference through which the call is made, rather than from the dynamic type of the object supplying the virtual function’s body.

C++ Command line Arguments:

When a program in C++ is compiled, an executable file is created this executable file can be used to executed the program directly from the DOS prompt. The arguments can also be passed to the program from the DOS prompt during execution of the program.

The arguments that are passed to a function at the DOS prompt are called command line arguments.

For example in MSDOS to copy a file from one location to another location, two arguments ( I.e. source file name and other for target file name), are provided to the copy command. These are called command line arguments.

In C++, arguments can also be given in the main function. These arguments are given as command line arguments when the program is executed from the DOS prompt.

The program example given below explains the concept of command line arguments.



Example how to use command line arguments in C++ programming :

In the above program, two arguments have been used in the main() function. The first argument is use by the program to count the number of command line arguments it must be given as arguments and it is always of int type.

The second argument is an array of string. The arguments passing from the DOS prompt with the program are stored in this array. The  program file name with its path is store in the first element of array and the first parameter passed with the program is stored in the second element of array .

Suppose the above program is compiled and is name “test” as filename. The program is executed from the DOS prompt as:

c>test Pakistan


the word “pakistan “ is stored in second element of “st”, i.e st[1] as argument. The contents of st[1] are copied to string variable “cp” within the program and then printed on the screen. In the first element of “st” array , i.e. st[0], the pathname of the program file is stored. It must be noted that to pass more than one argument, each argument is given, separated by blank space.

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: