Object Oriented Programming: Chapter 2: Classes and Objects

Destructors

C++ Programming | Object Oriented Programming

The destructor is called when the object is destroyed. The object can be destroyed automatically when the scope of the objects end (i.e. when the function ends) or explicitly by using operator delete.

Destructors

•  The destructor is called when the object is destroyed. The object can be destroyed automatically when the scope of the objects end (i.e. when the function ends) or explicitly by using operator delete.

•  The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value.

•  We normally use the destructor when the object assigns the dynamic memory in its lifetime.

C++ Program

#include<iostream>

using namespace std;

class image

{

private:

      int *height,*width;

public:

      image(int,int);//constructor

       ~image();//destructor

      int area();//regular function

};

image::image(int x,int y)

{

      height=new int;//assigns memory dynamically using 'new'

      width=new int;

      *height=x;

      *width=y;

image::~image()

{

      delete height;//destroys the memory using 'delete'

      delete width;

}

int image::area()

{

      return (*height* *width);

}

int main()

{

      image obj1(10,20);

      cout<<"The area is :"<<obj1.area()<<endl;

      return 0;

}

Output

The area is : 200

 

Difference between constructor and destructor :


Constructor

1. It allocates memory to an object.

2. It does not use ~ symbol.

3. Constructor accepts argument.

4. There can be multiple constructors in a program.

5. Constructor can be overloaded.

Destructor

1. It deallocates memory of the object.

2. It uses ~symbol.

3. Destructor does not accepts argument.

4. There is always a single destructor in a program.

5. Destructor can not be overloaded.

 

Review Questions

1. Discuss the characteristics of constructors and destructors

2. What are constructors? Explain the concept of destructor with an example.

3. Explain the constructors and destructors in detail with example program.

4. What are constructors and destructors? With suitable example explain various forms of constructors.

5. Explain the various types of constructors that are available in C++ with suitable example.

6. What are constructors? Explain the concept of destructor with an example.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Destructors


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation