Chapter – 5. CONSTRUCTORS & DESTRUCTORS

DELHI 2008

2.b. Answer the questions (i) and (ii) after going through the following program: 2

#include <iostream.h>
#include<string.h>
class bazaar
{
char Type[20] ;
char product [20];
int qty ;
float price ; bazaar()
//function 1
{
strcpy (type , “Electronic”) ;
strcpy (product , “calculator”);
qty=10;
price=225;
}
public : void Disp()
//function 2
{
cout<< type <<”-”<<product<<”:” <<qty<< “@” << price << endl ;
}
};
void main ()
{
Bazaar B ;
//statement 1 B.
disp() ; //statement 2
}

(i) Will statement 1 initialize all the data members for object B with the values given in the function 1 ? (YES OR NO). Justify your answer suggesting the correction(s) to be made in the above code.

Ans: No. The reason is the constructor should be defined under the public visibility label.

(ii) What shall be the possible output when the program gets executed ? (Assuming, if required _ the suggested correction(s) are made in the program).

Ans: Possible Output:
Electronic–Calculator:10@225

2.c. Define a class Garments in c++ with following descriptions 4

private members:

GCode of type string
GType of type string
Gsize of type intiger
Gfabric of type istring
Gprice of type float