C++ Program to illustrate the use of constructor and destructor

Program :



//Program to illustrate the use of constructor and destructor

#include <iostream>


using namespace std;


class geek

{

    private: int num;

    public:
    geek() { num=0;} //default constructor
    geek(int n) { num = n;}// overloaded constructor
    ~geek(){ cout<<endl<<"The Object is Destroyed";}

    void getdata()

    { cout<<endl<<"Enter an integer: "; cin>>num; }

    void dispdata()

    { cout<<endl<<"Number is:"<<num;}

};


int main()

{
    geek a; //invokes default constructor
    geek b(6); // invokes overloaded constructor
    a.dispdata();
    b.dispdata(); //displays instantiated values
    a.getdata(); b.getdata(); //to enter new values
    a.dispdata(); b.dispdata();// to display new values
}

Output :



Comments