Classes, Objects & Constructors
A class is a blueprint; an object is an instance in memory. Constructors initialize objects; destructors clean them up.
Cheat sheet
- ▸Constructor: same name as class, no return type, runs on creation.
- ▸Types: default, parameterized, copy constructor (C++).
- ▸this refers to the current object.
- ▸static members belong to the class, shared across objects.
- ▸Shallow copy copies references; deep copy duplicates referenced data.
- ▸C++ destructors run deterministically; Java uses garbage collection.
Shallow vs Deep copy
| Aspect | Shallow | Deep |
|---|---|---|
| Copies | Field values / pointers | Pointed-to data too |
| Shared state | Yes | No |
| Speed | Fast | Slower |
| Risk | Double free, side effects | Memory use |
Top 10 campus interview questions
Quick quiz
1. A constructor has return type:
2. Static variables are stored:
3. Private constructor is typical in:
4. Copying only the pointer is a:
5. Java String is: