C++ ClassesLesson 34Free preview
C++ Constructors
This lesson is part of C++ · All levels
Summary
A constructor is a special function that is automatically executed whenever an object is created, usually to fill in the initial data. C++ supports built-in constructors, constructors with parameters (overloading), constructors that delegate, and destructors that run when objects are destroyed. Understanding the life cycle of this object is important so that data is never empty or incorrectly filled.
Key points
- The constructor has the same name as the class and has no return type.
- Constructor overloading allows multiple constructors with different lists of parameters.
- The initializer list is written after the list of parameters with a colon mark, e.g. Siswa(string n): nama(n) {}.
- Destructor is written with a tilde (~) and is automatically called when the object is out of scope.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.