In: Computer Science
Study the following class definition:
class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; };
Which of the following options would make logical sense in the definition of the void accelerate(double speed)function?
Group of answer choices
this->speed = this->speed;
this->speed = speed;
this.speed = speed;
speed1 = this->speed;
Flag this Question
Question 131 pts
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; }
Group of answer choices
display_point(point_ptr);
point_ptr->display_point();
*point_ptr.display_point();
point_ptr.display_point();
Flag this Question
Question 141 pts
Study the following class definition:
class Athlete { public: Athlete(double new_speed); void train(); void run(double new_speed); void rest(); double get_speed() const; private: double speed; };
Which of the following options would be legal in the definition of the void run(double new_speed)function?
Group of answer choices
this.speed = new_speed;
this->speed = this->new_speed;
speed = this->new_speed;
this->speed = new_speed;
Flag this Question
Question 151 pts
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];
Group of answer choices
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() }
for (int i = 0; i < 20; i++) { all_registers->clear(); }
Question 1) this->speed=speed;
Here we have speed as an argument in the function and we have to map that speed argument with the speed of the Car class variable. For any variable which is defined in side the class can be called as this->(name of variable). This happens if we are using the variable inside the class. So for assigning speed we need to do it like this->speed=speed.
Question 131)
The given code snippet is
int main() { Point* point_ptr = new Point; // call the display_point function using point_ptr return 0; }
so the variable we have is a pointer of Point type. and we have to call a function on the point_ptr so that means we have to call function on a pointer. So we needed to use it like point_ptr.display_point() if it was a simple variable of that class but here due to pointer variable we will have to use it like (*point_ptr).display_point() which is also written as point_ptr->display_point(); so our option will be point_ptr->display_point().
Question 141) This one is similar to the problem 1st. Here we have the argument given as new_speed and the variable for storing the speed inside the class is speed. So as mentioned earlier we can say that to access the speed of the class we will have to use this->speed and this value is provided as new_speed as argument. So we can our answer will be this->speed = new_speed.
Question 151) Here we will have to clear each variable of the object of the class. We have an array of object of class so we need to go each object using for loop and then use the erase method on the corresponding object. But here we are given the pointer array of that class so we will have to use the array method as mentioned in 2nd question. So basically our answer should be this
for (int i = 0; i < 20; i++) { (*all_registers[i]).clear(); }
as we know (*all_registers[i]).clear() can be written as all_registers[i]->clear(). So our answer is
for (int i = 0; i < 20; i++) { all_registers[i]->clear(); }