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;
}
};
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;
}
•
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; }
};
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 .