Resolves the default parameters of the constructor in C++ and the overloading of the constructor

  • 2020-05-05 11:30:26
  • OfStack

The default parameter for the C++ constructor is

As with normal functions, the value of an argument in a constructor can be passed as an argument or specified as some default value, that is, if the user does not specify an argument value, the compilation system makes the parameter take the default value.

[example]


#include <iostream>
using namespace std;
class Box
{
  public :
  Box(int h=10,int w=10,int len=10); // Specify the default parameters when you declare the constructor 
  int volume( );
  private :
  int height;
  int width;
  int length;
};
Box::Box(int h,int w,int len) // You can define functions without specifying default parameters 
{
  height=h;
  width=w;
  length=len;
}
int Box::volume( )
{
  return (height*width*length);
}
int main( )
{
  Box box1; // No arguments are given 
  cout<<"The volume of box1 is "<<box1.volume( )<<endl;
  Box box2(15); // Given only one argument 
  cout<<"The volume of box2 is "<<box2.volume( )<<endl;
  Box box3(15,30); // Only given 2 An argument 
  cout<<"The volume of box3 is "<<box3.volume( )<<endl;
  Box box4(15,30,20); // For a given 3 An argument 
  cout<<"The volume of box4 is "<<box4.volume( )<<endl;
  return 0;
}

The result is:


The volume of box1 is 1000
The volume of box2 is 1500
The volume of box3 is 4500
The volume of box4 is 9000

The constructor definition in the program (lines 12-16) can also be rewritten as the parameter initialization table:


  Box::Box(int h,int w,int len):height(h),width(w),length(len){ }

As you can see, the use of default parameters in constructors is convenient and effective, providing multiple options for creating objects that act as several overloaded constructors.

The advantage is that even if you don't provide an argument value when the constructor is called, not only do you not make an error, but you also ensure that the object is initialized with the default parameter value. This is especially handy if you want to have the same initialization state for every object.

A few notes on constructor defaults:
The default value should be specified when the constructor is declared, not just when the constructor is defined.
Line 5 of the program can be omitted when declaring a constructor.
If all arguments to the constructor specify default values, you can define an object with one or more arguments, or without arguments.
An overloaded constructor cannot be defined after a constructor with all default parameters is defined in a class.

Overloading
for C++ constructor Multiple constructors can be defined in a class to provide different initialization methods for users to choose from. These constructors have the same name, and the number or types of arguments are not the same. This is called an overload of the constructor.

See how to apply constructor overloads in the following example.

Define two constructors, one with no arguments and one with arguments.


#include <iostream>
using namespace std;
class Box
{
  public : Box( ); // Declare a constructor with no arguments 
  // Declare a constructor with arguments and initialize the data member with the initialization table of the argument 
  Box(int h,int w,int len):height(h),width(w),length(len){ }
  int volume( );
  private :
  int height;
  int width;
  int length;
};
Box::Box( ) // Defines a parameterless constructor 
{
  height=10; width=10; length=10;
}
int Box::volume( ){
  return (height*width*length);
}
int main( )
{
  Box box1; // Object to establish box1, No arguments are specified 
  cout<<"The volume of box1 is "<<box1.volume( )<<endl;
  Box box2(15,30,25); // Object to establish box2, The specified 3 An argument 
  cout<<"The volume of box2 is "<<box2.volume( )<<endl;
  return 0;
}

Two overloaded constructors are defined in this program, but other overloaded constructors can be defined with a prototype declaration of


  Box::Box(int h); // There are 1 Constructor of the 
  Box::Box(int h,int w); // Constructor with two arguments 


One and two parameters are given when the object is created.

A few notes on constructor overloading:
A constructor that does not give arguments when a constructor is called is called the default constructor (default constructor). Obviously, the no-argument constructor belongs to the default constructor. A class can only have one default constructor.
If you use a no-argument constructor when creating an object, be careful to write the statement that defines the object correctly.
Although you can have more than one constructor in a class, for each object, only one constructor is executed when the object is created, not every constructor is executed.


Related articles: