Question

In: Computer Science

5. In the following code snippet, which member function of the class Car is called first?...

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();
   }
}

Solutions

Expert Solution

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?

  1. Constructor has same name as the class itself (here, Car()).
  2. Constructors don't have return type.
  3. A constructor is automatically called when an object is created.

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.


Related Solutions

/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
• Based on the following code snippet and plan attributes, provide the output for Plan A,...
• Based on the following code snippet and plan attributes, provide the output for Plan A, Plan B and Plan C. Code Snippet • {{IF:[Copay]=Not Covered && [Coinsurance]=Not Covered, show ‘Not covered’}} • {{IF:[Copay]!=$0.00 && [Copay]!=Blank, show value from {{Copay}} copayment}} • {{IIF:[Copay]!=$0.00 && [CopayFrequency]!=Blank, show {{Copay}} copayment/{{CopayFrequency}}}} • {{IF:[CopayMax]=$0.00 OR [Copay]=$0.00, show No Charge}} • {{IF:[[CopayMax]!=$0.00 && [CopayMax] contains ‘$’ && [Coinsurance] contains ‘%’ && [CopayMaxFrequency]=Blank, show {{CopayMax}} copayment then {{Coinsurance}} coinsurance]}} Plan Copay CopayFrequency CopayMax CopayMaxFrequency Coinsurance Plan...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
***IN JAVA*** 5. Currency conversion: Write a snippet that first asks the user to type   today's...
***IN JAVA*** 5. Currency conversion: Write a snippet that first asks the user to type   today's US dollar price for one  Euro. Then use a loop to: -- prompt the user to enter a Euro amount. (allow decimals) -- convert that amount to US dollars. (allow decimals) -- print the amount to the screen, formatted to two decimal places Use 0 as a sentinel to stop the loop.
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open...
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open account (with an initial deposit). This is called to put initial values in dollars and cents. 2. Make a deposit. A function that will add value to dollars and cents 3. Make a withdrawal. A function that will subtract values from dollars and cents. 4. Show current balance. A function that will print dollars and cents. Data (Member Data) 1. dollars 2. cents Operations...
Divide by classes. Parameters and return types of each function and class member function should be...
Divide by classes. Parameters and return types of each function and class member function should be decided in advance. Write a program that computes a patient's bill for a hospital stay. The different components are: PatientAccount Class Surgery Class Pharmacy Class main program Patient Account will keep total of the patient's charges. Keep track of the number of days spent in the hospital. Group must decide on hospital's daily rate. Surgery class will have stored within it the charges for...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT