C++ Program Using Constructor and Destructor





This is an object oriented programming in c++ to create database of the following items:

1.       Name of the student

2.       Roll number of the student

3.       Height of the student

4.       Weight of the student

Constructor has the same name as the name of the class. Constructors are automatically called as soon as the object of the class is created. Constructor is also used to initialize the object of that class.

Destructor is the opposite  of constructor. Destructor is used to destroy the objects created by constructor.  Destructor also has the same name as of the class.  There will be only one destructor in a class.

 

#include<iostream.h>

#include<conio.h>

Class student

{

Private:

Char name [25];

Int roll;

Float height, weight;

Public:

Student ()

{

Strcpy (name, “ram”);

roll=0;

height=0;

weight=0;

}

Void display ()

{

Cout<<”\n name :”<< name;

Cout<<”\n roll no”<<roll;

Cout<<”\n Height”<<height;

Cout<<”\n weight”<<weight;

}

~ Student ()

{

Cout<<”\n destroying object”;

}

};

Void main ()

{

Student obj;

Obj. Student ();

getch ();

}

The above  program is designed in-order to store the data’s of student. The class named ‘student’ has been declared in the program. This program uses constructor and destructor.



Related posts:

  1. Destructor In C++ Destructor in c++ It is used o destroy the object...
  2. Constructor in C++ Constructor in C++ Constructor is a member function with the...
  3. Array Of Class Object Array of class objects Array is a collection of similar...
  4. Array of class objects Array of class objects and single inheritance A program consisting...
  5. Virtual Destructor Virtual destructor C++ standard does not allow declaration of destructor...