Classes: Constructors And Destructors
Applies To: C++Builder 1 or higher
Category: C++ Language
- Classes can optionally include constructors and destructors. A constructor is a function which is called when an object is first created. The destructor is called when the object is destroyed. They cannot return values.
-
- Constructors and destructors have the same name as the class, but a destructor is preceded by ~. Here's an example:
-
- class List {
- public:
- int Count;
- char *Caption;
-
- // Add constructor
- List();
-
- // Add destructor
- ~List();
- };
-
- List::List() {
- Count = 10;
- }
-
- List::~List() {
- Count = 0;
- }
-
- The constructor initializes the variable Count and the destructor set its value to 0.
C++Builder Developer's Network
Copyright © Yoto Yotov