You must Sign In to post a response.
  • Category: Programming

    MSBTE computer engineering exam question on C program


    Unable to solve a question in the MSBTE computer engineering exam paper? Get quick help on how to solve it from our experts.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 examination. This question is asked for 8 marks and hence requires some explanation along with program code. Please let me know the solution.

    The question is: Write a program to declare class having data member as hrs, mins, secs. Write constructor to assign values and destructor to destroy values. Accept & display data for one object. [8 Marks]
  • #153030
    Before going to the program I think it is necessary to refresh some basic terms of object oriented programming.

    Class is a user defined data type which normally has some member variables and some member functions to work on those member variables. Though class is a user defined data type but it works almost as a built in data type.

    Object is nothing but the variable of type class. It actually holds the memory for the member variables. So object always have memory space for member variables and user required values get stored in these member variables.

    As stated earlier class may have at least one function to work on the member variables of the class. Constructor is nothing but the member function of the class whose name is same as that of the class. The main task of constructor is to initalise the member variables of the object with user defined values, whenever the object is created. The name this function is constructor because it constructs the members of the object when object is created. This is also called as dynamic initialization of the object. Different types of constructors are as follows
    1) default constructor(constructor without any argument)
    2)parameterised constructor(constructor with arguments)
    3)copy constructor (constructor with parameter as a previously created object)
    4)default argument constructor
    Destructor is also a special member function of the class whose name is same as that of the class but it has preceding ~ sign. The task of destructor is to destroy the members of the object which are created by constructor. Destructor get executed either exit upon function or exit upon program. That is why in the below program, after execution of the program to see the effect of destructor user has to press Alt + F5 key.

    In below given program variable int c is declared as static variable. Static variable has some special features. Only one copy of static variable is created and it is shared by all the objects of the class. Static variable is normally used to indicate the values which are common to all the object. In case of normal variable(non static variable) each variable has its own different value for each object. On contrast static variable is having only one value for all objects. So though static variable is part of class it is not bound to a specific object. This is the reason static variable is initalised after class declaration is over. The initial value of static variable is zero if not specified, when the first object of the class is created.

    Hope this explanation and program will make you satisfied.

    Please include "iostream.h" and "conio.h" header files before class declaration.

    //PROGRAM TO IMPLEMENT CONTSRUCTURE AND DESTRUCTOR.

    #include
    #include
    class time
    {
    private:
    static int c;
    int hrs,min,sec;
    public:
    time(int h, int m, int s)
    {
    c=c+1;
    hrs=h;
    min=m;
    sec=s;
    cout<<"Entered time is "< }
    ~time()
    {
    cout< }
    };

    int time::c;
    void main(void)
    {
    int h,m,s;
    cout<<"\nEnter required hours, minutes and seconds\n";
    cin>>h>>m>>s;
    class time t(h,m,s);
    getch();
    }


  • Sign In to post your comments