Chapter – 5. CONSTRUCTORS & DESTRUCTORS

DELHI 2004

2.a. Given the following C++ code, answer the questions (i)and(ii)

class TestMeOut
{
public: ~TestMeOut( ) //Function 1
{
cout<<”Leaving the examination hall”<<endl;
}
TestMeOut( ) //Function 2
{
cout<<”Appearing for examination”<<endl;
}
void MyWork( )
{
cout<<”Attempting Questions”<<endl;
}
};

(i) In Object Oriented programming, what is Function 1 referred as and when does it get invoked/called?

Ans: Function 1 is called as Destructor, It will automatically executed at the time of destruction of the object of class TestMeOut.

(ii) In Object Oriented Programming, what is Function 2 referred as and when does it get invoked/called?

Ans: Function 2 is called as constructor (Non-parameterized or default constructor) , it will automatically executed at the time of creation of the object of class TestMeOut.

DELHI 2003

2.b. Define a class Play in C++ with the following specifications:

Private members of class Play
Play code integer
Playtime 25 character
Duration float
Noofscenes integer

Public member function of class bPlay

  • A constructer function to initialize Duration as 45 and Noofscenes as
  • Newplay() function to values for Playcode and Playtitle.
  • Moreinfor() to assign the values of assign the values of Duration and Noofscenes with the of corresponding values passed as parameters to this function.
  • Shoplay() function to display all the dataq members on the screen.

Ans:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Play
{
int Playcode;
char Playtitle[25];
float Duration;
int Noofscenes;
public: Play( )
{ Duration=45; Noofscenes=5;
}
void Newplay( )
{
cout<<"\nEnter the Play Code: ";
cin>>Playcode;
cout<<"\nEnter the Play Title: ";
gets(Playtitle);
}
void Moreinfor(float D,int N)
{
Duration = D; Noofscenes = N;
}
void Showplay( )
{
cout<<"\nThe Play Code : "<<Playcode;
cout<<"\nThe Play Title : "<<Playtitle;
cout<<"\nThe Duration : "<<Duration;
cout<<"\nThe No of Scenes:"<<Noofscenes;
}
};
void main( )
{
clrscr( );
Play P;
P.Newplay( );
float Dur;
int NS;
cout<<"\nEnter the Duration and Number of Scenes: ";
cin>>Dur>>NS; P.Moreinfor(Dur,NS);
P.Showplay( );
getch( );
}