From the course: Object-Oriented Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Constructors and destructors

Constructors and destructors - C++ Tutorial

From the course: Object-Oriented Programming with C++

Constructors and destructors

- [Narrator] C++ has a special set of functions to control the lifecycle of objects. In this video, we'll discuss the constructor responsible for the creation, and the destructor, which controls the destruction of an object. A constructor is a function that is automatically called when an object of a class is created. We define constructors to simplify the process of initializing a class, whereas a destructor is a function that gets executed when an object's lifetime ends. Its main purpose is to free up any resources that the object acquired during its lifetime. The compiler provides default implementations for both the constructor and the destructor. However, these default implementations may not always satisfy our needs, as we just saw in our example. So let's declare a constructor for our review class in the review.h file. I declare the constructor within the public section of the class, otherwise it couldn't be accessed when creating an instance of review. The syntax for a…

Contents