COMPUTER SCIENCE CONSTRUCTORS AND 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 thecorrection(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

A function Assign() which calculate and the value of GPrice as follows.For the value of GFabric “COTTON” ,

GType           GPrice(RS)
TROUSER   1300
SHIRT           1100
For GFabric other than “COTTON”, the above mentioned GPrice gets reduced by 10%

public members:
A constructor to assign initial values of GCode,GType and GFabric with the a word “NOT ALLOTED”and Gsize and Gprice with 0.
A function Input () to the values of the data membersGCode, GType,Gsize and GFabric and invoke the Assign() function.
A function Display () which displays the content of all the data members for a garment.

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
class Garments
{ char GCode[21],GType[21];
int Gsize;
char Gfabric[21];
float Gprice;
void Assign( )
{
if(strcmp(strupr(Gfabric),"COTTON")==0)
{ if(strcmp(strupr(GType),"TROUSER")==0)
Gprice=1300;
if(strcmp(strupr(GType),"SHIRT")==0)
Gprice=1100;
}
else
{if(strcmp(strupr(GType),"TROUSER")==0)
Gprice=1300*0.90;
if(strcmp(strupr(GType),"SHIRT")==0)
Gprice=1100*0.90;
} }
public:
Garments( )
{
strcpy(GCode,"NOT ALLOTED");
strcpy(GType,"NOT ALLOTED");
Gsize=0;
strcpy(Gfabric,"NOT ALLOTED");
Gprice=0;
}
void Input( )
{ cout<<"\nEnter the Grament Code: ";
gets(GCode);
cout<<"\nEnter the Garment Type: ";
gets(GType);
cout<<"\nEnter the Garment Size: ";
cin>>Gsize;
cout<<"\nEnter the Garment Fabric: ";
gets(Gfabric);
Assign( );
} void display( )
{ cout<<"\nThe Garment Code: "<<GCode;cout<<"\nThe Garment Type: "<<GType;
cout<<"\nThe Garment Size: "<<Gsize;
cout<<"\nThe Garment Fabric: "<<Gfabric;
cout<<"\nThe Garment Price: "<<Gprice;
}
};
void main( )
{ Garments G;
G.Input( );
G.display( ); }

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )