C++ program to demonstrate the use of constructors and destructors

This program demonstrates the concept of constructors and destructors in c++. A constructor is a funbction with the same name as that of its class. If no explicit constructor is defined the program will invoke the default constructor. Destructors are used to destroy the constructor, when they are no longer used. The program implements the constructor sample() and destructor ~sample().

Program


//program to implement constructors and destructors

#include<iostream.h>

#include<conio.h>

class sample

{

static int count,c1;

public:

sample()

{

count++;

cout<<"\nNumber of objects created="<<count;

}

~sample()

{

c1++;

cout<<"\nNumber of objects destroyed="<<c1;

}

};

int sample::count;

int sample::c1;

void main()

{

{

sample s;

getch();

{

sample s1;

getch();

}

getch();

}

getch();

}

No comments: