In: Computer Science
5. In the following code snippet, which member function of the class Car is called first?
class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed(); c1.stop(); return 0; }
get_speed()
Car()
start()
accelerate()
13. The Point class has a public function called display_point(). What is the correct way of calling the display_point function in the main function?
int main() { Point* point_ptr = new Point; // call the display_point function using point_ptr return 0; }
*point_ptr.display_point();
point_ptr.display_point();
display_point(point_ptr);
point_ptr->display_point();
15.
You are given the class definition for CashRegister. One of the member functions of this class is clear(). Furthermore, you have set up and allocated an array of pointers to CashRegister objects. Given the declaration of the array all_registers below, which code snippet correctly calls the clear() function on every object pointed to from the all_registers array? CashRegister* all_registers[20];
for (int i = 0; i < 20; i++) { all_registers->clear(); }
for (int i = 0; i < 20; i++) { all_registers[i].clear(); }
for (int i = 0; i < 20; i++) { all_registers[i]->clear(); }
for (int i = 0; i < 20; i++) { all_registers.clear() }
16.Which expression references the data member suit within an object that is stored in position win (an integer variable) of the variable my_deck, which is a one-dimensional array of objects?
my_deck[suit].win
suit[object].win
my_deck[win].suit
my_deck[win]->suit
17.You are given the class definition for CashRegister. One of the member functions of this class is get_total(), which returns a double that represents the register total for the object. Furthermore, you have set up and allocated an array of CashRegisterobjects. Given the declaration of the array all_registers below, which code snippet correctly calls the get_total() function on every object in the all_registers array and uses it to calculate the largest total over all registers (the maximum value of all individual register totals)?
CashRegister all_registers[20];
double result = 0.0; for (int i = 0; i < 20; i++) { if (all_registers[i].get_total() > result) { result = all_registers[i].get_total(); } }
double result = 0.0; for (int i = 0; i < 20; i++) { result += all_registers[i]->get_total(); }
double result = 0.0; for (int i = 0; i < 20; i++) { result += all_registers[i].get_total(); }
double result = 0.0; for (int i = 0; i < 20; i++) { if (all_registers[i].get_total() > result) { result = all_registers[i]->get_total(); } }
ANS 5. When an object or an instance of a class is created, the very first method to run is constructor by default without calling any method explixitly i.e., as soon as the object is created the constructor will run for sure automatically to allocate memory to that created object.
So, here the Car() constructor is the first method to be called.
NOTE - How to identify a constructor?
ANS 13.
Point* point_ptr = new Point;
Here, point_ptr is pointer instance which points to a new instance of Point class.
So, to call a member function using this pointer variable is a bit tricky. When there is pointer variable, one can make use of an arrow operator to call a member function because the instance created is not a usual variable, it's a pointer variable.
In case of pointer instance we can make use either of the following :
a.) pointer_ptr->display_point(()
b.) (*pointer_ptr).display_point()
Note that we cannot use *pointer_ptr.display_point() because of the precedence rule. Here, it will first find the member function display_point() for point_ptr and then it's address which will lead to an error because the display_point() method will be called using pointer_ptr instance which is not defined and it is expecting to be called by a pointer variable, therefore it leads to an error.
So, the answer will be : point_ptr->display_point();
ANS 15. This is the same case as above.
CashRegister* all_registers[20];
This is again a pointer instance but a pointer instance to array.
The same explanatin as above but herein we have to loop through each element in the array.
So, the answer is:
for (int i = 0; i < 20; i++) { all_registers[i]->clear(); }
And all the other answers will lead to an error.
for (int i = 0; i < 20; i++) { all_registers->clear(); }
This will lead to an error because one cannot simply call the member function with array pointer, we need to call member function with each element pointer.
ANS 16. Here, the object created is not a pointer to variable instead it's is normal/usual instance of the class with data member suit.
So to reference a data member which is stored in in an array at index win we need to simply use the dot(.) operator.
Therefore, the answer is : my_deck[win].suit
array_name[index].data_member
ANS 17.
To calculte the maximum/largest total over all_registers array by:
a. Intialise a variable to 0 as double.
b. Loop over each element of the array.
c. For each element, call the member variable get_total() to calculate the total for each element in the array.
d. And store the result in result variable as double if the vlaue sored in result is less than the calculted value for each element in the array.
So, the option that follow above algorithm is :
CashRegister all_registers[20];
double result = 0.0;
for (int i = 0; i < 20; i++)
{
if (all_registers[i].get_total() > result)
{
result = all_registers[i].get_total();
}
}
Note that here we have used the dot (.) operator for calling the member function and not the arrow(->) operator because as discussed earlier we are having the usual array declaration of objects not the pointers as instance variable.