C++ Multiple constructor in a class

C++ multiple constructor in a class

so far we have used two kinds of constructor. They are

integer(); // no arguments
integer(int,int) // two arguments

In the first case, the constructor integer() itself supplies the data values and no values are passed by the calling program. in above example, In the second case the function call passes the appropriate values from main(). C++ permits use to both these constructors in the same class.

For example in the below we could define a class as follows

class integer
{
int m,n;
public:
integer() // constructor first
{
m=0;
n=0;
}
integer(int a,int b) // constructor second
{
m=a;
n=b;
}
integer(integer &i) // constructor Third
{
m=i.m;
n=i.n;
}
};

This declares three constructor for an integer object.so The first constructor receives no arguments, and The second case receive two integer arguments and the third receives one integer object as an argument. for example The Declaration

integer I1;

would automatically invoke first constructor and set both m and n of I1 to zero.
The statement

integer I2(20,40);

would call the second constructor which will initialize the data members m , n of I2 to 20 and 0 respectively.
Finally the statement

integer I3(I2);
would invoked

CONTSRUCTOR WITH DEFAULT ARGUMENTS

iT IS possible to define constructor with default arguments for example, The constructor complex() can be declared as follows

complex(float real,float img=0);

The default value of the argument img is zero. Then the statement

complex C(5.0);

assign the value 5.0 to real variable and 0.0 to img(by default) however the statement

complex(3.0,4.0);

assigns 2.0 real and 3.0 to img. The actual parameter(arguments), when specified overrides the default value as pointed out earlier, The missing arguments must be the trailing ones it is important to difference between the default constructor A::A() and the default arguments constructor A:A()(int=0).

The default argument constructor can be called with either no arguments or one argument. When called with no arguments, it becomes a default constructor.

When both those forms are used in a class, it generates ambiguity for statement such as

A a;
The ambiguity is whether to call A::A() or A:A(int=0)

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise