Thursday, March 3, 2011

C++ program that illustrates the order of constructor and destructor invocation in nice way



#include<iostream>
using namespace std;
class Dept{
    private:
        int id;
        char name[30];
    public:
        Dept(){}

        Dept(int i,const char str[30])
        {
            id=i;
            strcpy(name,str);
            cout<<"You have entered "<<name<<" dept. with id "<<id<<endl;
        }
        ~Dept()
        {
            cout<<"You are leaving "<<name<<" dept. with id "<<id<<endl;
        }
};
int main()
{
    Dept d1(1,"Electrical"),d2(2,"Electronics"),d3(3,"Computer");
    cout<<"Function main() is terminating"<<endl;
    return 0;
}

No comments:

Post a Comment