Wednesday, October 1, 2014

Write a class that can store department id and department name with constructors to initialize its members. Write destructor member in the same class and display the message “objects n goes out of scope” . Your program should be made such that it should show the order of constructor and destructor invocation.

Algorithm:
1.      Start of program
2.      Create a class with member functions getdata().
3.      Create constructor and destructor
4.      Enter department id and name
5.      Constructor and destructor is called.
6.      End of program
Code:
#include
#include
int c=0;
class dept
{
            private:
            int i,n,id;
            char name[10];
            public:
            dept()
            {
                        c++;
                        cout<<"\n No. of object created:"<
            }
            ~dept()
            {
                        cout<<"\n Object\t"<
                        c--;
            }
            void getdata()
            {
                        cout<<"\n Enter Department ID:";
                        cin>>id;
                        cout<<"\n Enter Department Name:";
                        cin>>name;
            }
};
void main()
{

            clrscr();
            cout<<"\t OBJECT CREATED IN MAIN"<
            {
            dept d1;
            d1.getdata();
            dept d2;
            d2.getdata();
            dept d3;
            d3.getdata();
            dept d4;
            d4.getdata();
            }
            cout<<"\n \t ALL OBJECTS DESTROYED";
            getch();
}


No comments:

Post a Comment