Constructors and Destructors in
C++
Presented by: Wasiq Aslam
What is a Constructor?
• - A constructor is a special member function
of a class that automatically initializes objects
when they are created.
• - It has the same name as the class and no
return type .
Types of Constructors in C++
• 1. Default Constructor
• 2. Parameterized Constructor
• 3. Copy Constructor
Example of a Default Constructor
#include <iostream>
using namespace std;
class Car1 {
public:
Car1() { // Default Constructor
cout << "A car is created!" << endl;
}
};
int main() {
Car c1; // Constructor automatically invoked
return 0;
}
Parameterized Constructor
• - Allows passing arguments while creating an object.
class Car {
public:
string brand;
Car(string b) { // Parameterized Constructor
brand = b;
}
void showBrand() {
cout << "Car brand: " << brand << endl;
}
};
Using Parameterized Constructor
int main() {
Car c1("Toyota");
c1.showBrand();
return 0;
}
Copy Constructor
• - A copy constructor creates a new object as a copy of an existing object .
class Car {
public:
string brand;
Car(string b) { brand = b; }
Car(const Car &c) { // Copy Constructor
brand = c.brand;
}
};
Using Copy Constructor
int main() {
Car c1("Honda");
Car c2 = c1; // Copy constructor is called
return 0;
}
What is a Destructor?
• - A destructor is a special member function
that automatically deletes an object when it
goes out of scope.
• - It has the same name as the class , prefixed
with a tilde (~) , and no parameters .
Example of a Destructor
class Car {
public:
Car() { cout << "Car created!" << endl; }
~Car() { cout << "Car destroyed!" << endl; }
};
int main() {
Car c1;
return 0;
}
•
Why Use a Destructor?
• - Frees resources (e.g., memory allocation,
closing files).
• - Prevents memory leaks .
Constructor Overloading
• - Multiple constructors in the same class with
different parameters .
class Car {
public:
Car() { cout << "Default Car" << endl; }
Car(string b) { cout << "Car brand: " << b << endl; }
};
Using Constructor Overloading
int main() {
Car c1;
Car c2("Ford");
return 0;
}
Dynamic Memory Allocation with
Constructor & Destructor
class Car {
public:
int *speed;
Car() { speed = new int(100); }
~Car() { delete speed; }
};
Example with Dynamic Allocation
int main() {
Car *c = new Car();
delete c;
return 0;
}
Best Practices for Constructors and
Destructors
• - Always define a destructor when dynamic
memory allocation is used.
• - Use initializer lists for member variables.
• - Keep constructors simple .
Constructor in Inheritance
class Vehicle {
public:
Vehicle() { cout << "Vehicle Created" << endl; }
};
class Car : public Vehicle {
public:
Car() { cout << "Car Created" << endl; }
};
Constructor in Derived Class
int main() {
Car c1; // Calls both base and derived class
constructors
return 0;
}
Destructor in Inheritance
class Vehicle {
public:
~Vehicle() { cout << "Vehicle Destroyed" << endl; }
};
class Car : public Vehicle {
public:
~Car() { cout << "Car Destroyed" << endl; }
};
Order of Constructor and
Destructor Calls
• - Constructors execute from base class to
derived class .
• - Destructors execute in reverse order
(derived to base class) .
Example with Constructor and
Destructor Calls
int main() {
Car c1; // Constructor executes (Vehicle ->
Car)
return 0; // Destructor executes (Car ->
Vehicle)
}
Summary of Constructors
• - Automatically called when object is created.
• - Can be default, parameterized, or copy
constructor .
Summary of Destructors
• - Used for resource deallocation .
• - Cannot be overloaded.
Thank You!
• - Questions?

object oriented programming with C++ Constructors_Destructors.pptx

  • 1.
    Constructors and Destructorsin C++ Presented by: Wasiq Aslam
  • 2.
    What is aConstructor? • - A constructor is a special member function of a class that automatically initializes objects when they are created. • - It has the same name as the class and no return type .
  • 3.
    Types of Constructorsin C++ • 1. Default Constructor • 2. Parameterized Constructor • 3. Copy Constructor
  • 4.
    Example of aDefault Constructor #include <iostream> using namespace std; class Car1 { public: Car1() { // Default Constructor cout << "A car is created!" << endl; } }; int main() { Car c1; // Constructor automatically invoked return 0; }
  • 5.
    Parameterized Constructor • -Allows passing arguments while creating an object. class Car { public: string brand; Car(string b) { // Parameterized Constructor brand = b; } void showBrand() { cout << "Car brand: " << brand << endl; } };
  • 6.
    Using Parameterized Constructor intmain() { Car c1("Toyota"); c1.showBrand(); return 0; }
  • 7.
    Copy Constructor • -A copy constructor creates a new object as a copy of an existing object . class Car { public: string brand; Car(string b) { brand = b; } Car(const Car &c) { // Copy Constructor brand = c.brand; } };
  • 8.
    Using Copy Constructor intmain() { Car c1("Honda"); Car c2 = c1; // Copy constructor is called return 0; }
  • 9.
    What is aDestructor? • - A destructor is a special member function that automatically deletes an object when it goes out of scope. • - It has the same name as the class , prefixed with a tilde (~) , and no parameters .
  • 10.
    Example of aDestructor class Car { public: Car() { cout << "Car created!" << endl; } ~Car() { cout << "Car destroyed!" << endl; } }; int main() { Car c1; return 0; } •
  • 11.
    Why Use aDestructor? • - Frees resources (e.g., memory allocation, closing files). • - Prevents memory leaks .
  • 13.
    Constructor Overloading • -Multiple constructors in the same class with different parameters . class Car { public: Car() { cout << "Default Car" << endl; } Car(string b) { cout << "Car brand: " << b << endl; } };
  • 14.
    Using Constructor Overloading intmain() { Car c1; Car c2("Ford"); return 0; }
  • 15.
    Dynamic Memory Allocationwith Constructor & Destructor class Car { public: int *speed; Car() { speed = new int(100); } ~Car() { delete speed; } };
  • 16.
    Example with DynamicAllocation int main() { Car *c = new Car(); delete c; return 0; }
  • 17.
    Best Practices forConstructors and Destructors • - Always define a destructor when dynamic memory allocation is used. • - Use initializer lists for member variables. • - Keep constructors simple .
  • 18.
    Constructor in Inheritance classVehicle { public: Vehicle() { cout << "Vehicle Created" << endl; } }; class Car : public Vehicle { public: Car() { cout << "Car Created" << endl; } };
  • 19.
    Constructor in DerivedClass int main() { Car c1; // Calls both base and derived class constructors return 0; }
  • 20.
    Destructor in Inheritance classVehicle { public: ~Vehicle() { cout << "Vehicle Destroyed" << endl; } }; class Car : public Vehicle { public: ~Car() { cout << "Car Destroyed" << endl; } };
  • 21.
    Order of Constructorand Destructor Calls • - Constructors execute from base class to derived class . • - Destructors execute in reverse order (derived to base class) .
  • 22.
    Example with Constructorand Destructor Calls int main() { Car c1; // Constructor executes (Vehicle -> Car) return 0; // Destructor executes (Car -> Vehicle) }
  • 23.
    Summary of Constructors •- Automatically called when object is created. • - Can be default, parameterized, or copy constructor .
  • 24.
    Summary of Destructors •- Used for resource deallocation . • - Cannot be overloaded.
  • 25.
    Thank You! • -Questions?