Theory
The till that opened empty
Monday, 7 am. The canteen software starts, creates its CanteenTill object, and prints the opening balance:
Balance: -734829156
Nobody typed that. Nobody typed anything: cash was never initialised, so it holds whatever garbage sat in that memory before.
Relying on "someone will remember to call setup()" fails the day someone forgets. What you want is initialisation that runs automatically, the instant the object is born.
Theory
Opening and closing rituals
A canteen never just appears mid-service: every morning has an opening ritual (count the float, wipe counters, switch on the kettle), and every night a closing ritual (tally cash, lock the drawer, lights off).
A constructor is the opening ritual: it runs by itself when the object is created. A destructor is the closing ritual: it runs by itself when the object is destroyed. Nobody has to remember either.
Theory
Constructor, formally
A constructor is a special member function that:
- has the same name as the class,
- has no return type, not even void,
- runs automatically when an object is created.
Three kinds you must know:
- Default: no parameters, sets safe starting values.
- Parameterized: takes arguments:
Item chai("Chai", 12.5); - Copy: builds an object from another:
Item second = chai;
Practical
The till that opens itself
#include <iostream>
using namespace std;
class CanteenTill {
private:
double cash;
public:
CanteenTill() { // default constructor
cash = 0;
cout << "Till opened, balance 0" << endl;
}
CanteenTill(double openingFloat) { // parameterized constructor
cash = openingFloat;
cout << "Till opened with float " << cash << endl;
}
~CanteenTill() { // destructor: note the tilde
cout << "Till closed, final balance " << cash << endl;
}
};
int main() {
CanteenTill a; // prints: Till opened, balance 0
CanteenTill b(500.0); // prints: Till opened with float 500
return 0;
} // going out of scope: b closes first, then a
Theory
Destructor, formally
A destructor is the class name with a tilde: ~CanteenTill().
- Takes no arguments and returns nothing.
- Therefore it cannot be overloaded: exactly one per class.
- Runs automatically when the object goes out of scope (or is deleted).
Order matters: objects are destroyed in reverse order of creation (last built, first closed). Its real job in bigger programs: release resources, close files, free memory the object acquired.
Think first
Predict the print order
Using the class above, main() creates CanteenTill a; CanteenTill b(500); and then ends. Before tapping: write the exact four lines the program prints, in order.
Show the answer
1. Till opened, balance 0 (a's default constructor)
2. Till opened with float 500 (b's parameterized constructor)
3. Till closed, final balance 500 (b's destructor: reverse order, last in first out)
4. Till closed, final balance 0 (a's destructor)
Construction order a, b; destruction order b, a. That reversal is a guaranteed exam favourite, and yes, it is the stack idea again.
Quiz
A student writes: int CanteenTill() { cash = 0; return 1; } inside the class. Why does the till still open with garbage?
- A function with a return type is NOT a constructor, just an ordinary member function nobody calls
- Constructors must be private to work
- The constructor should be named init(), not CanteenTill
- return 1 crashes the constructor before cash = 0 runs
Show the answer
A function with a return type is NOT a constructor, just an ordinary member function nobody calls
The rule is strict: a constructor has no return type at all. Writing int CanteenTill() creates a normal member function that happens to share the class name and never runs automatically, so cash stays garbage. Options B and C invert the actual rules (constructors are usually public and MUST bear the class name); option D describes behaviour C++ does not have.
Watch out
The vanishing default constructor
If you write no constructor, the compiler gifts you an empty default one.
The moment you define any parameterized constructor, that gift is withdrawn: now CanteenTill a; fails to compile unless you also write the default constructor yourself. Exams phrase it as "why does Item i; give an error after adding Item(char n[], double p)?" Now you know: define both, or lose the plain form.
Theory
Constructors meet inheritance
When a Student (derived from Person) is created, Person's constructor runs first, then Student's: the base floor is built before the extension. Destructors run in the opposite order, child first. Keep the opening/closing ritual picture: this ordering question is where constructors, inheritance and even the stack quietly join hands.
Summary
Key takeaways
- Constructor: same name as the class, no return type, runs automatically at creation.
- Three kinds: default (no args), parameterized (args), copy (builds from another object).
- Destructor: ~ClassName(), no arguments, only one per class, runs automatically at destruction.
- Objects are destroyed in reverse order of creation; base constructs before derived.
- Defining a parameterized constructor removes the compiler's free default one.
- A 'constructor' with a return type is just an ordinary function: the classic trick question.
- Memory hook: opening ritual, closing ritual.