Lesson 16 of 24

Constructors மற்றும் Destructors

10 min 30 XP

Constructor object உருவாகும்போது இயங்கும்; destructor (~ClassName) object அழியும்போது இயங்கும். Initializer list (: name(n)) fields-ஐ அமைக்கும் சிறந்த வழி. 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

Time class: constructor Time(int h, int m, int s); method add(int seconds) நேரத்தைக் கூட்டும் (24 மணி நேரம் சுழற்சி); method print() "HH:MM:SS" (இரண்டு இலக்கங்கள்). 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.