0

can anyone explain code below ??

What is the output of this code? #include <iostream> #include <vector> using namespace std; class A{ public: static int cnt; A(int a){ ~A(){ ++cnt;} int A::cnt = 0; int main() vector<A> B(4, 1); V.push_back(1); cout << A::cnt << endl; return 0;

2nd Oct 2018, 10:19 AM
paras dhir
paras dhir - avatar
1 Answer
+ 1
There won't be any output as the code won't even compile due to several errors. The constructor in line 7 is incomplete, there's a } missing. Also, the constructor doesn't do anything. The destructor in line 8 increases the object count instead of decreasing it. That's probably not what you want, but it's not an error. After the class declaration, }; is missing. int A::cnt = 0; in line 10 won't work. int A::cnt looks like a function definition, not a variable assignment. You can't assign an integer to a function. In line 12, you're trying to create an A object with two integers as parameters. The (empty) constructor only accepts one integer (and doesn't do anything with it). In line 13, you use an undeclared variable V. The function main() is missing the curly brackets {}.
2nd Oct 2018, 11:52 AM
Anna
Anna - avatar