Constructors and Destructors in C++

Mannan Ul Haq
0

Constructors:

Constructors are special member functions of a class that are called automatically when an object of that class is created. Constructors are used to initialize the data members of an object.

A constructor has the same name as the class and no return value.


Types of Constructors:

1. Default Constructor:

  • A default constructor is a constructor that is automatically generated by the compiler if no constructor is explicitly defined in the class.
  • It has no parameters and initializes the data members to their default values.
  • The default constructor is invoked when an object is created without any arguments.

Example:

class MyClass
{
private:
    int data;

public:
    // Default constructor
    MyClass()
    {
        data = 0;
    }
};

 

In the above example, the `MyClass` has a default constructor that initializes the `data` member to zero.

2. Parameterized Constructor:

  • A parameterized constructor is a constructor that takes one or more parameters.
  • It allows you to initialize the data members of an object with specific values provided at the time of object creation.

Example:

class Rectangle
{
private:
    int length;
    int width;

public:
    // Parameterized constructor
    Rectangle(int len, int wid)
    {
        length = len;
        width = wid;
    }
};

In the above example, the `Rectangle` class has a parameterized constructor that takes the length and width as parameters and initializes the corresponding data members.

3. Constructors with Default Arguments:

  • Constructors can have default arguments for one or more parameters.
  • Default arguments allow you to create objects without providing explicit values for those parameters.

Example:

class Circle
{
private:
    double radius;

public:
    // Constructor with default argument
    Circle(double rad = 0.0)
    {
        radius = rad;
    }
};

In the above example, the `Circle` class has a constructor with a default argument for the `rad` parameter. If no argument is provided during object creation, the radius is initialized to zero.

4. Copy Constructor:

  • A copy constructor is a constructor that creates a new object by initializing it with an existing object of the same class.
  • It is used to create a copy of an object, ensuring that each member of the original object is properly copied to the new object.
NOTE: The copy constructor can be defined explicitly by the programmer. If the programmer does not define the copy constructor, the compiler does it for us. (alert-success)

Example:

class Person
{
private:
    string name;
    int age;

public:
    //Constructor
    Person(string n, int a)
    {
        name = n;
        age = a;
    }

    // Copy constructor
    Person(const Person& other)
    {
        name = other.name;
        age = other.age;
    }
};

int main()
{
    Person person1("Ali", 10);
    Person person2 = person1; // Calling Copy Constructor
    Person person3(person1); // Calling Copy Constructor

    return 0;
}

In the above example, the `Person` class has a copy constructor that creates a copy of an existing `Person` object by copying the name and age from the original object to the new object.

Constructor Overloading:

Constructor overloading refers to the ability to define multiple constructors within a class, each having a different parameter list. The compiler determines which constructor to invoke based on the arguments provided during object creation.

Example:

class Employee
{
private:
    string name;
    int age;
    double salary;

public:
    // Default constructor
    Employee()
    {
        name = "";
        age = 0;
        salary = 0.0;
    }

    // Parameterized constructor
    Employee(const string& newName, int newAge, double newSalary)
    {
        name = newName;
        age = newAge;
        salary = newSalary;
    }

    // Constructor with default arguments
    Employee(const string& newName, int newAge = 0, double newSalary = 0.0)
    {
        name = newName;
        age = newAge;
        salary = newSalary;
    }
};

In the above example, the `Employee` class demonstrates constructor overloading. It has a default constructor, a parameterized constructor, and a constructor with default arguments. These constructors allow creating `Employee` objects with different initialization options.

Destructors:

  • The destructor is a special member function that is called automatically when an object is destroyed or goes out of scope. It is used to clean up resources allocated by the object, such as dynamic memory.
  • Destructor has the same name as their class name preceded by a tilde (~) symbol.
  • It is not possible to define more than one destructor.
  • Destructor neither requires any argument nor returns any value.
  • In destructors, objects are destroyed in the reverse of an object creation.

Example:

class Car
{
private:
    string brand;

public:
    // Constructor
    Car(const string& carBrand)
    {
        brand = carBrand;
    }

    // Destructor
    ~Car()
    {
        // Clean up resources, if needed
    }
};

In the above example, the `Car` class has a destructor that is invoked automatically when a `Car` object is destroyed. The destructor can be used to release any resources held by the object, such as closing files or freeing allocated memory.

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !