C++ Programming

C++ Default Arguments and command line 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.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

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

#include <iostream>

using namespace std;

int main()
{
    void temp (char []="Electronic", int= 2);
    temp();
    temp("clinic",10);
    cout<<"ok";
    return 0;
}
void temp(char x[15],int y)
{
    for (int c=1; c<=y; c++)
    cout<<x<<endl;
    
}

Output:

Electronic
Electronic
clinic
clinic
clinic
clinic
clinic
clinic
clinic
clinic
clinic
clinic
ok

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 “Electronic” will be printed twice.



In the second call, the parameter value “clinic” 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.

struct Base {
  virtual std::pair<int, int> Foo(int x = 1) {
    return {x, 1};
  }
};

struct Derived : public Base {
  std::pair<int, int> Foo(int x = 2) override {
    return {x, 2};
  }
};

int main() {
  Derived d;
  Base& b = d;
  assert(d.Foo() == std::make_pair(2, 2));
  assert(b.Foo() == std::make_pair(1, 2));
}

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 :

#include <iostream>
#include<string.h>

using namespace std;

int main(int ca, char *st[])
{
    char cp[15];
    strcpy(cp,st[1]);
    cout<<cp;
    return 0;
}

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.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button