Lesson 16 of 24

Constructors आणि Destructors

10 min 30 XP

Object तयार झाल्यावर constructor चालतो; नष्ट झाल्यावर destructor (~ClassName) चालतो. Fields सेट करण्याचा सर्वोत्तम मार्ग initializer list (: name(n)). static member सर्व objects मध्ये सामायिक असतो.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Block संपताच b चा destructor आपोआप चालतो. हेच RAII, म्हणजे resources ना object च्या आयुष्याशी बांधणे, आणि ही C++ मधली सर्वात महत्त्वाची कल्पना आहे.

Challenge

+30 XP

class Time लिहा ज्यात constructor Time(int h, int m, int s), वेळ जोडणारे add(int seconds) (24 तासांवर परत फिरते) आणि "HH:MM:SS" दोन-दोन अंकांत print करणारे print() असेल. main() मध्ये h m s आणि जोडायचे seconds वाचा, मग print करा. Input: 23 59 30 45 Output: 00:00:15

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.