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

Constructor Time(int h, int m, int s), సమయాన్ని కలిపే add(int seconds) (24 గంటల వద్ద తిరిగి మొదలు), "HH:MM:SS" ను రెండేసి అంకెలతో print చేసే print() ఉన్న class Time రాయండి. 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.