Question 67 Which of the following statements about constructors is true? Answers a. A constructor can have any name b. There can be one and only one constructor in an object oriented program c. A constructor is never passed any arguments d. All of a through c are true statements e. None of a through c are true statements
Expert Answer
(67)
Thus the correct option is (e) None of a through c are true statements.
(A) A constructor can have any name is False
Due to Reason is
Constructor have same name as class name
Example:
class test{
int x,y;
public:
//Default constructor
test(){
}
//parameter constructor
test(int a,int b){
x =a;
y =b;
}
};
(B) There can be one and only one constructor in an object oriented program is False.
- Object oriented program supports multiple constructors in a program.
- Constructor used to initialize the object of the class.
(C) A Constructor is never passed any arguments is False.
- Constructors are allowed to pass arguments.
- It will used initialize the variables of class.
Example:
//parameter constructor
test(int a,int b){
x =a;
y =b;
}
(D) All of a through c is true statements are False.
- From the explanation of above (A),(B),(C) statements is False.
//Code Image
//Sample Output
//Code to copy
#include<iostream>
using namespace std;
class test {
private:
int x, y;
public:
int sum(); //returns the sum of the private data members
void print(); //prints the values of the private data members
// the x value followed by the y value
test(); //initializes the private data member to 0
test(int, int); //initializes the private datam member to the passed in values:
// x = value of the first argument
// y = value of the second argument
};
/*Implementation of sum method*/
int test:: sum() {
int sum;
sum = x + y;
return sum;
}
/*Implementation of print method*/
void test::print() {
cout << x << “t” << y << endl;
}
/*Implementation of Defualt Constructor */
test::test() {
}
/*Implementation of Parameterized Constructor */
test::test(int a, int b) {
x = a;
y = b;
}
int main()
{
test t1 = test();
test t2 = test(240, 360);
t1.print();
system(“pause”);
return 0;
}
//Explanation
Thus the correct option is (c)
It displays garbage values because no values were passed when object t1 was created.
- When try to call the values x and y through the object t1 of the test class, but not initialized the values of x and y.
- So for that reason it will display the garbage values for x and y through t1.print () method.