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.
#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;
}
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
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.