In: Computer Science
QUESTION 5
Consider the following Die class:
class Die {
public:
void setValue(int x);// assign x to num
int getValue(); // return num
void roll(); // set num with a random number ranging 1-6
Die(); // constructor
private:
int num;
};
Which of the following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained.
Die myDie; |
||
Die myDie; |
||
Die myDie; |
||
Die myDie; |
3 points
QUESTION 6
Consider the following statements.
struct supplierType {
string name;
int supplierID;
};
struct paintType {
supplierType supplier;
string color;
string paintID;
};
paintType paint;
Which stataement is correct?
paint.name="Sherwin-Williams"; |
||
paintType.supplyType.name="Sherwin-Williams"; |
||
paint.supplier="Sherwin-Williams"; |
||
paint.supplier.name="Sherwin-Williams"; |
following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained.
Die myDie;
int dieValue, countTil6 = 0;
myDie.roll();
dieValue = myDie.getValue();
while (dieValue != 6) {
countTil6++;
myDie.roll();
dieValue = myDie.getValue();
}
cout << countTil6;
explaination of code
Die will be rolled first and then we get the value of the die
say we get value as 2,, before while loop value of countTil6 is zero
In while loop we check if dieValue is not equal to zero
if yes then we increament the count of variable countTil6
then again we roll the Die and get the value of the Die.. assume we get value as 6
Again we check in while loop if value is 6,,
This time condition is false as we got value 6 ..
So it comes out of the loop and print value countTil6 , in this case 1,, which is correct
=========================================
Below stataement is correct
paint.supplier.name="Sherwin-Williams";
as supplier is the member of struct variable paint and name is the member of struct variable supplier, this is correct statement to assign variable name of the supplier through struct variable paint.
===========
//why other statements are not correct
paint.name="Sherwin-Williams"; //name is not member of paint
paintType.supplyType.name="Sherwin-Williams"; //supplyType type is name of the user defined data type
paint.supplier="Sherwin-Williams";// supplier is the variable of type struct supplyType which has data members name and supplierID