Consider the following C++ code snippet and what is the output of this program
Answers
Answered by
2
Output of C++ Program | Set 14
2.6
Predict the output of following C++ program.
Difficulty Level: Rookie
Question 1
#include <iostream> using namespace std; class A { int id; public: A (int i) { id = i; } void print () { cout << id << endl; } }; int main() { A a[2]; a[0].print(); a[1].print(); return 0; }
There is a compilation error in line “A a[2]”. There is no default constructor in class A. When we write our own parameterzied constructor or copy constructor, compiler doesn’t create the default constructor (See this Gfact). We can fix the error, either by creating a default constructor in class A, or by using the following syntax to initialize array member using parameterzied constructor.
// Initialize a[0] with value 10 and a[1] with 20 A a[2] = { A(10), A(20) }
2.6
Predict the output of following C++ program.
Difficulty Level: Rookie
Question 1
#include <iostream> using namespace std; class A { int id; public: A (int i) { id = i; } void print () { cout << id << endl; } }; int main() { A a[2]; a[0].print(); a[1].print(); return 0; }
There is a compilation error in line “A a[2]”. There is no default constructor in class A. When we write our own parameterzied constructor or copy constructor, compiler doesn’t create the default constructor (See this Gfact). We can fix the error, either by creating a default constructor in class A, or by using the following syntax to initialize array member using parameterzied constructor.
// Initialize a[0] with value 10 and a[1] with 20 A a[2] = { A(10), A(20) }
Similar questions