COMPUTER SCIENCE CONSTRUCTORS AND DESTRUCTORS

OUTSIDE DELHI 2008:

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

#include<iostream.h>
#include<string.h>
class Retail
{ char category[20];
char item[20];
int qty;
float price;
retail () //function 1
{ strcpy (category, “cerial”);
strcpy (Item, “Rice”);
qty =100 ;
price =25 ;
}
public;
void show() //function 2
{ cout << category <<”-“<< Item << “ :”<<Qty<<“@”<< price<<endl;
}
};
void main()
{ Retail R; //statement 1
R. show (); //statement 2
}

(i) will statement 1 initialize all the data members for objects R with the given in the function 1 ? (YES OR NO). Justify your Answer suggesting the corrections(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 out put when the program gets executed ? (Assuming, if required the suggested correction(s) are made in the program)

Ans: Possible Output:

cerial–Rice:100@25

2.c ) Define a class clothing in c++ with the following descriptions :

private members :

code         of type string
type          of type string
size          of type intiger
material  of type string
price        of type float

A function calc_price( )which calculates and assigns the value of GPrice as follows ; For the value of material as “COTTON” :

Type price (Rs)

TROUSER  1500.
SHIRT          1200.

for material other than “COTTON”, the above mentioned GPprice price gets reduced by 25%

public members :

  • A constructor to assign initial values of code ,type and material with the word “NOT ASSIGNED “and size and price with 0.
  • A function enter() to input the values of the data members code, type, size and material and invoke the caclPrice () function.
  • A function show which displays the content of all the data members for a clothing.

    #include<iostream.h>
    #include<string.h>
    #include<conio.h>
    #include<stdio.h>
    class clothing
    { char Code[21],Type[21];
    int size;
    char material[21];
    float price;
    void calc_price( )
    {
    if(strcmp(strupr(material),"COTTON")==0)
    { if(strcmp(strupr(Type),"TROUSER")==0)
    price=1500;
    if(strcmp(strupr(Type),"SHIRT")==0)
    price=1200;
    }
    else
    { if(strcmp(strupr(Type),"TROUSER")==0)
    price=1500*0.75;
    if(strcmp(strupr(Type),"SHIRT")==0)
    price=1200*0.75;
    }
    }
    public:
    clothing( )
    { strcpy(Code,"NOT ALLOTED");
    strcpy(Type,"NOT ALLOTED");
    size=0;
    strcpy(material,"NOT ALLOTED");
    price=0;
    }
    void enter( )
    { cout<<"\nEnter the Cloth Code: ";
    gets(Code);
    cout<<"\nEnter the Cloth Type: ";
    gets(Type);
    cout<<"\nEnter the Cloth Size: ";
    cin>>size;
    cout<<"\nEnter the cloth material:";
    gets(material);
    calc_price( );
    }
    void show( )
    { cout<<"\nThe Cloth Code: "<<Code;
    cout<<"\nThe Cloth Type: "<<Type;cout<<"\nThe Cloth Size: "<<size;
    cout<<"\nThe Cloth Material: “ <<material;
    cout<<"\nThe Cloth Price: "<<price; } };
    void main( )
    { clothing C;
    C.enter( );
    C.show( );
    }

 

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