Thursday 14 November 2013

Ambiguity Cases

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);
};

6 comments:

  1. please explain in detail sir.

    ReplyDelete
    Replies

    1. In 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

      Delete
  2. How ambiguity is raising here..?

    ReplyDelete
    Replies
    1. I have explained Case 1 already.

      Case 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.

      Delete
  3. sir, please give an example for implementing the use of destructors.

    ReplyDelete
    Replies
    1. #include
      #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";
      }

      Delete