Questions: 1. Explain the importance of constructors and destructors with examples. 2. Write short notes on the following: Constructors and destructors. 3. Write a short note on destructor."
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.
#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);
}
void main()
{
image
obj1(10,20);
cout<<"The
area is :"<<obj1.area() <<endl;
getch();
}
Output
The area is 200
Review Questions
1. Explain the
importance of constructors and destructors with examples.
2. Write short notes
on the following: Constructors and destructors.
3. Write a short note
on destructor."
Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Destructors
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation