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

Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
/*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...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
• 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...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
white a class having two private variables and one member function which will return the area...
white a class having two private variables and one member function which will return the area of rectangle
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...
***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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT