Constructors, Destructors, and Operator Overloading
Class Instantiation
- Classes are like blueprints, instances are like buildings.
- Instantiation: Creating an instance of a class.
- Instances have allocated memory.
- Multiple identical instances of the same type can exist.
Constructor and Destructor
- Constructor: Sets up necessary stuff when an instance is created.
- Destructor: Cleans up stuff when an instance is destroyed.
Class Constructor
- Special member functions to initialize objects.
- Same name as the class, no return type.
- Use
: field(value), ... to initialize member variables.
Class Destructor
- Cleanup function called when the object is destroyed.
- Name:
~ + class name. - No arguments, no return type.
Scope
- Variables declared within a block have scope limited to that block.
- Constructors and destructors are called when objects enter and exit scope.
Copy Constructor
- Initializes an object using another instance of the same class.
ClassName(const ClassName& obj);- Called when:
- Object is returned by value.
- Object is passed by value as an argument.
- Object is constructed based on another object of the same class.
Default Copy Constructor
- Implicitly created by the compiler if no user-defined copy constructor exists.
- Performs a member-wise copy.
- May not work correctly when dealing with pointers; requires a user-defined copy constructor to allocate new memory.
Default Constructor
- Implicitly created if there are no user-defined constructors.
- If a copy constructor is defined, the compiler does not create the default constructor.
Caution: Default Copy Constructor
- Only copies pointer values, NOT the allocated memory.
- Requires a user-defined copy constructor for deep copying.
Copy Elision
- Compilers can optimize code to avoid unnecessary copying of objects.
- Return value optimization.
- Disable with
-fno-elide-constructors.
C Structure Example: Complex Number
- Example of defining a structure with real and imaginary parts.
Static Members
- Belong to the class, not individual instances.
- Static member variables are shared among all instances.
- Static member functions do not have a
this pointer and can only access static member variables.
Class Design Principle
- Hide data members.
- Make member functions meaningful and atomic.
- Use
const as much as possible. - Keep initialization in constructors simple.
- Use static members only when necessary.
Binary Search Tree
- Allows binary search for fast lookup, addition, and removal of data items.
Function Overloading
- Defining same-name functions with different parameters.
- Must differ by at least one parameter, or const-ness.
Operator Overloading
- Redefining built-in operators.
operatorX(arguments...)- Can be a class member function or a non-member function.
Operator Overloading by Non-member Function
- Use non-member functions when the first operand is not a class type.
Converting Constructor & Operator Overloading
- Constructors can convert one type to another.
Overloading << and >>
- Useful for printing contents of an object using
cout and reading from cin.
Assignment Operator =
- Default assignment operator does a member-wise copy.
Increment Operator ++
- Prefix and postfix versions can be distinguished using a dummy
int parameter. Point& Point::operator++(); // prefix ++ptPoint Point::operator++(int); // postfix pt++
Summary of the class
- Class vs instance, constructor, destructor,
this pointer - Static members
- Function and operator overloading