Constructors and Destructors

  • By
  • September 29, 2022
  • C & C++
Constructors and Destructors

Constructors and Destructors

C++ is an OOP(Object Oriented Programming) Language, which means it uses objects in the programming. The main aim of oops is to bind data and functions together. Oops, the concept includes different characteristics like classes, constructor, destructor, polymorphism, encapsulation, abstraction, and inheritance. Class is the basic concept that is used in all other concepts. In this blog, we are going to learn about constructors and destructors with examples. We are going to learn types of constructors also with examples.

Constructors:- A constructor in CPP is a member function of a class that has the same name as the class name. Constructor is always used to initialize the objects of a class. If required we can pass the arguments to it. It is also used to allocate the memory to an object of the class. It is automatically called whenever an instance of the class is created. We can define it manually with arguments or without arguments. We can use many constructors in a class. Constructors can be overloaded but they cannot be inherited. You can define a constructor inside class or outside the class. Looking for C++ Classes in Pune, SevenMentor is providing excellent courses.

Different features of the constructor are as follows:-

  • Constructors are the members of the class that are used to initialize the object of a class.
  • Whenever an object is created a class constructor is automatically called.
  • The constructor is used to initialize the variables.
  • The constructor has the name of the class.
  • We can use more than one constructor in the program.
  • Constructors can be overloaded.

There are 3 types of constructors. They are as follows:-

  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

 

The default constructor does not have any arguments. An example of a default constructor is as follows:-

e.g. Constructor inside a class:-

class abc

{     

  public:

    abc() {     
      cout << “Hello Everyone!”;
    }
};

int main() 

{
  abc obj1;    

  return 0;
}

e.g. Constructor outside a class:-

class abc

{     

  public:

    abc();

};

abc::abc()

{     
      cout << “Hello Everyone!”;
}
int main() 

{
  abc obj1;    

  return 0;
}

Parameterized constructor has parameters. Example of the parameterized constructor is as follows:-

class add {

  private:

    int a;

    int b;

  public:

    add(int x, int y) {

      a = x;

      b = y;

    }

    int total() {

      return a+b;

    }

};

int main() 

{  add s1(10, 8);

  add s2(8, 6);

  cout << “Addition of first numbers is: ” << s1.total() << endl;

  cout << “Addition of second numbers is:” << s2.total();

  return 0;

}

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

copy constructor is a member function that initializes an object using another object of the same class.  The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.

Copy constructor takes a reference to an object of the same class as an argument.

An example of copy constructor is as follows:

class ass

{

private:

  int x;

public:

    ass()

    {

 

    }

ass(int a)

{

    x=a;

}

ass(ass & b)

{

    x=b.x;

}

  void display()

  {

      cout<<x;

  }

};

int main(void)

{

  ass s(100);

  ass s1(s);

  ass s2=s;

  ass s3;

  s3=s;

  cout<<“\n value of s: “;

  s.display();

  cout<<“\n value of s1: “;

  s1.display();

  cout<<“\n value of s2: “;

  s2.display();

  cout<<“\n value of s3: “;

  s3.display();

  return 0;

}

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

Destructor:- Destructor is an instance member function that is called or invoked automatically whenever an object is going to be destroyed, which means that a destructor is the last function that is going to be called before an object is destroyed. Different features of a destructor are as follows:-

  • The destructor does not require any arguments and it does not return any value also. 
  • Like constructor destructor also has the same name as its class but it starts with a tilde(~) symbol.
  • You cannot have more than one destructor.
  • The destructor destroys the objects created by the constructor so it cannot be overloaded.
  • The destructor also frees the memory occupied by the objects created using the constructor.
  • Destructor destroys the objects in reverse order of their creation.

e.g. class abc

{

public:

    abc()

    {

        cnt++;

        cout<<“\n no of objects created  “<<cnt;

    };

    ~abc()

    {

        cout<<“\n no. of objects destroyed  “<<cnt;

        cnt–;

    }

};

int main()

{

    abc a1,a2,a3,a4;

    {

        cout<<“\n Block1\n”;

        alpha a5;

    }

    {

        cout<<“\n Block2 \n”;

        alpha a6;

    }

    cout<<“\n Re-enter main\n”;

    return 0;

}

 

Author:

Sarika Ganesh Kore
Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*