C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 32)
32.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class BixArray
{
    int array[3][3];
    public:
    BixArray(int arr[3][3] = NULL)
    { 
        if(arr != NULL)
        for(int i = 0; i < 3; i++) 
            for(int j = 0; j < 3; j++) 
                array[i][j] = i+j; 
    } 
    void Display(void)
    {
        for(int i = 0; i < 3; i++) 
            for(int j = 0; j < 3; j++)
                cout<< array[i][j] << " "; 
    }
};
int main()
{
    BixArray objBA;
    objBA.Display();
    return 0; 
}
The program will report error on compilation.
The program will display 9 garbage values.
The program will display NULL 9 times.
The program will display 0 1 2 1 2 3 2 3 4.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
6 comments Page 1 of 1.

PRAFUL ANAND said:   1 decade ago
At the time of creation of object objBA no value is passed to the constructor. So, arr=NULL, if part will not execute and default value is assigned to the elements of matrix. Display function will print garbage values.

Sanki said:   1 decade ago
Just can't get it. Since there is a Parameterized constructor, the default constructor should have been declared. Otherwise, how would the Program compile.

Ankit said:   1 decade ago
If we will declare the default constructor, the compiler will get ambiguity.

Shubham arora said:   1 decade ago
@Sanki.

There are two ways one is either pass the value while creating object, or create object without passing any value but write value in constructor. Example:

First case:
class one
{
one(int a)
{
}};
main()
{
one obj(10);
}

Second case:

class one
{
one(int a=10)
{
}};
main()
{
one obj();
}

Both ways are correct.

Honey said:   8 years ago
The correct option is A as the compiler returns error when an object is created with the default constructor for a class with parameterized constructor without default constructor.

Vaibhav said:   6 years ago
The answer is A because of compile-time error.

Post your comments here:

Your comments will be displayed after verification.