What is the output of this code? class Temp { public int num = 2; public Temp() { num++;} ~Temp() { num++; } } static void | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the output of this code? class Temp { public int num = 2; public Temp() { num++;} ~Temp() { num++; } } static void

when the object is created the constructor increments the num value by 1 and when the program ends destructors is called which again increments the value by one .So how the answer might be 3?

30th Apr 2019, 2:07 PM
Smit Chawda
Smit Chawda - avatar
3 Answers
+ 4
After instantiating a new class, value in num var will be incremented, cause public Temp() - is constructor which call after object create . When object is destroyed , value incremented again, cause ~Temp() -is destructor, which call before object destroying. "static void" in the end not have any reason! answer is 3 cause method , which show value was called before object is destroyed.
30th Apr 2019, 3:59 PM
id001x
id001x - avatar
+ 2
Answer: 3
10th Jan 2020, 1:12 AM
Đào Ngọc Tuấn Anh
Đào Ngọc Tuấn Anh - avatar
0
Please place code in the playground and post a link here. Your question is hard to read. Because of the code in the title. It looks like a part of the code is missing
30th Apr 2019, 2:40 PM
sneeze
sneeze - avatar