Home | | Computer Science 11th std | C++: Declaration and Definition of Destructors

Example Program in C++ - C++: Declaration and Definition of Destructors | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: Declaration and Definition of Destructors

A destructor is a special member function that is called when the lifetime of an object ends and destroys the object constructed by the constructor.

Declaration and Definition

 

A destructor is a special member function that is called when the lifetime of an object ends and destroys the object constructed by the constructor. Normally declared under public.

 

Illustration14.29 to illustrate destructor function in a class

#include<iostream>

using namespace std;

class simple

{

private:

int a, b;

public:

simple()

{

a= 0 ;

b= 0;

cout<< "\n Constructor of class-simple ";

}

void getdata()

{

cout<<"\n Enter values for a and b (sample data 6 and 7)... ";

cin>>a>>b;

}

void putdata()

{

cout<<"\nThe two integers are .. "<<a<<'\t'<< b<<endl;

cout<<"\n The sum of the variables "<<a<<" + "<<b<<" = "<<a+b;

}

~simple()

{ cout<<”\n Destructor is executed to destroy the object”;} };

int main()

{

simple s;

s.getdata();

s.putdata();

return 0;

}

Output:

Constructor of class-simple

Enter values for a and b (sample data 6 and 7)... 6 7

The two integers are .. 6       7

The sum of the variables 6 + 7 = 13

Destructor is executed to destroy the object


Tags : Example Program in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Declaration and Definition of Destructors | Example Program in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.