Classes & Objects
Use C++ classes and objects to build more scalable, modular programs.
StartKey Concepts
Review core concepts you need to learn to master this subject
Destructor
Destructor
City::~City() {
// Any final cleanup
}
For a C++ class, a destructor is a special method that handles object destruction, generally focused on preventing memory leaks. Class destructors don’t take arguments as input and their names are always preceded by a tilde ~
.
Classes and Objects
Lesson 1 of 1
- 1So far, we’ve used several data types, including int, double, std::string, and bool. When we work with int or std::string, we can create variables with certain properties and methods specific to th…
- 2An empty class is pretty useless. Classes are designed to bring together related information and functionality — time to add stuff inside! Components of a class are called class members. Just l…
- 3Now that you have a class, let’s create some objects! To refresh your memory, an object is an instance of a class, which encapsulates data and functionality pertaining to that data. To create (or …
- 4Let’s circle back to that public keyword. What was that about? By default, everything in a class is private, meaning class members are limited to the scope of the class. This makes it easier to ke…
- 5Is there a way to give an object some data right when it gets created? We’re so glad you asked! A constructor is a special kind of method that lets you decide how the objects of a class get crea…
- 6So far, you’ve learned how to create and use objects. But there’s another part of the object lifecycle we need to cover: how to destroy them! Muahahaha. It’s actually far less nefarious than it so…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory