Chances of ambiguity in the case of Constructors with default arguments:
Case:1
class complex
{
float real;
float imag;
public:
complex(void);
complex(float a=1.5);
};
Case:2
class complex
{
float real;
float imag;
public:
complex(float a=1.5);
complex(float, float b=2.5);
};
Case:1
class complex
{
float real;
float imag;
public:
complex(void);
complex(float a=1.5);
};
Case:2
class complex
{
float real;
float imag;
public:
complex(float a=1.5);
complex(float, float b=2.5);
};
please explain in detail sir.
ReplyDeleteIn case-1: complex(void) is the default constructor and complex(float a=1.5) is a constructor with default argument. The constructor with default argument can be invoked with no parameter or with one parameter. If we create an object with no argument the ambiguity arises - whether to invoke the default constructor or to invoke the constructor with default argument
How ambiguity is raising here..?
ReplyDeleteI have explained Case 1 already.
DeleteCase 2 has two constructors with default argument(s). The first constructor can be invoked with one or no argument and the second constructor can be invoked with two or one argument(s). Ambiguity arises when we create an object with one argument - that is whether to invoke the first constructor or the second one.
sir, please give an example for implementing the use of destructors.
ReplyDelete#include
Delete#include
class alpha
{
public:
alpha() {cout << endl << "Created Object";
getch();}
~alpha() {cout << endl << "Object Destroyed";
getch();}
};
void main()
{
clrscr();
cout << endl << "Enter Main";
alpha A1, A2, A3;
{
cout << endl << "Enter First Block";
alpha A4;
cout << endl << "Exit First Block";
}
{
cout << endl << "Enter Second Block";
alpha A5;
cout << endl << "Exit Second Block";
}
cout << endl << "Exit Main";
}