C++ Constructors
Question 1 |
Predict output of the following program
C
#include <stdio.h> int main() { printf("new_c_questionbr"); printf("geeksforgeeks"); getchar(); return 0; }
ew_c_question | |
new_c_ques | |
| |
Depends on terminal configuration |
Discuss it
Question 1 Explanation:
Question 2 |
Which of the followings is/are automatically added to every class, if we do not write our own.
Copy Constructor | |
Assignment Operator | |
A constructor without any parameter | |
All of the above |
Discuss it
Question 2 Explanation:
In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class.
Question 3 |
When a copy constructor may be called?
When an object of the class is returned by value. | |
When an object of the class is passed (to a function) by value as an argument. | |
When an object is constructed based on another object of the same class | |
When compiler generates a temporary object. | |
All of the above |
Discuss it
Question 3 Explanation:
Question 4 |
Output of following program?
C++
<br> #include < iostream ><br> using namespace std;<br> class Point {<br> Point() { cout << "Constructor called"; }<br> };<br><br> int main()<br> {<br> Point t1;<br> return 0;<br> }<br>
Compiler Error | |
Constructor called | |
None of these | |
Runtime Error |
Discuss it
Question 4 Explanation:
By default all members of a class are private. Since no access specifier is there for Point(), it becomes private and it is called outside the class when t1 is constructed in main.
Question 5 |
#include<iostream> using namespace std; class Point { public: Point() { cout << "Constructor called"; } }; int main() { Point t1, *t2; return 0; }
Compiler Error | |
Constructor called Constructor called | |
Constructor called |
Discuss it
Question 5 Explanation:
Only one object t1 is constructed here. t2 is just a pointer variable, not an object
Question 6 |
Output of following program?
#include<iostream> using namespace std; class Point { public: Point() { cout << "Normal Constructor calledn"; } Point(const Point &t) { cout << "Copy constructor calledn"; } }; int main() { Point *t1, *t2; t1 = new Point(); t2 = new Point(*t1); Point t3 = *t1; Point t4; t4 = t3; return 0; }
Normal Constructor called Normal Constructor called Normal Constructor called Copy Constructor called Copy Constructor called Normal Constructor called Copy Constructor called | |
Normal Constructor called Copy Constructor called Copy Constructor called Normal Constructor called Copy Constructor called | |
Normal Constructor called Copy Constructor called Copy Constructor called Normal Constructor called |
Discuss it
Question 6 Explanation:
See following comments for explanation:
Point *t1, *t2; // No constructor call t1 = new Point(10, 15); // Normal constructor call t2 = new Point(*t1); // Copy constructor call Point t3 = *t1; // Copy Constructor call Point t4; // Normal Constructor call t4 = t3; // Assignment operator call
Question 7 |
#include<iostream> using namespace std; class X { public: int x; }; int main() { X a = {10}; X b = a; cout << a.x << " " << b.x; return 0; }
Compiler Error | |
10 followed by Garbage Value | |
10 10 | |
10 0 |
Discuss it
Question 7 Explanation:
The following may look like an error, but it works fine.
X a = {10};
Like structures, class objects can be initialized.
The line "X b = a;" calls copy constructor and is same as "X b(a);". Please note that, if we don't write our own copy constructor, then compiler creates a default copy constructor which assigns data members one object to other object.
Question 8 |
What is the output of following program?
#include <iostream> using namespace std; class Point { int x, y; public: Point(const Point &p) { x = p.x; y = p.y; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1; Point p2 = p1; cout << "x = " << p2.getX() << " y = " << p2.getY(); return 0; }
x = garbage value y = garbage value | |
x = 0 y = 0 | |
Compiler Error |
Discuss it
Question 8 Explanation:
There is compiler error in line "Point p1;". The class Point doesn't have a constructor without any parameter. If we write any constructor, then compiler doesn't create the default constructor.
It is not true other way, i.e., if we write a default or parameterized constructor, then compiler creates a copy constructor. See the next question.
Question 9 |
#include <iostream> using namespace std; class Point { int x, y; public: Point(int i = 0, int j = 0) { x = i; y = j; } int getX() { return x; } int getY() { return y; } }; int main() { Point p1; Point p2 = p1; cout << "x = " << p2.getX() << " y = " << p2.getY(); return 0; }
Compiler Error | |
x = 0 y = 0 | |
x = garbage value y = garbage value |
Discuss it
Question 9 Explanation:
Compiler creates a copy constructor if we don't write our own. Compiler writes it even if we have written other constructors in class. So the above program works fine. Since we have default arguments, the values assigned to x and y are 0 and 0.
Question 10 |
Predict the output of following program.
#include<iostream> #include<stdlib.h> using namespace std; class Test { public: Test() { cout << "Constructor called"; } }; int main() { Test *t = (Test *) malloc(sizeof(Test)); return 0; }
Constructor called | |
Empty | |
Compiler Error | |
Runtime error |
Discuss it
There are 26 questions to complete.