Professional Documents
Culture Documents
6
A destructor will have exact same name
as the class prefixed with a tilde (~)
and it can neither return a value nor
can it take any parameters.
Destructor can be very useful for
releasing resources before coming out
of the program like closing files,
releasing memories etc.
7
1. #include<iostream.h> 12. void createObject();
2. #include<conio.h> 13. void main()
3. class Example 14. {
4. { 15. clrscr();
5. public: 16. createObject();
6. Example() { 17. getch();
7. cout<<“object 18. }
created”<<endl;} 19. void createObject()
12. ~Example() { 20. {
13. cout<<“object 21. Example ob1, ob2;
destroyed”<<endl; } }; 22. }
#include <iostream.h> Line::~Line(void) {
#include<conio.h> cout << "Object is being
class Line { deleted" << endl; }
private: void Line::setLength( double len )
double length; {
public: length = len; }
void setLength( double len ); double Line::getLength( void ) {
double getLength( void ); return length; }
// Main function for the program
Line(); // This is the
constructor declaration void main( )
~Line(); // This is the {
destructor: declaration Line line;
}; // set line length
// Member functions definitions line.setLength(6.0);
including constructor cout << "Length of line : " <<
Line::Line(void) line.getLength() <<endl;
{ return 0;
cout << "Object is being getch(); }
created" << endl;
}
9
Function Overloading.