Question

In: Computer Science

The following class keeps track of how many flavors are at an ice-cream place, and the...

The following class keeps track of how many flavors are at an ice-cream place, and the number of calories of each flavor. The calories of each flavor ice-cream are positive values of type unsigned int and are stored as a dynamically allocated array. The first data member is a pointer that will point to the first element of the array. This array will be of an arbitrary size. The default size of the array shall be 25, but may be specified by the user at the time of construction. The second data member represents the size of the array, i.e. the number of menu items, and is stored as size_t.

class IcrecreamCalories {
private:
unsigned int* calories;
size_t num;
public:
// constructors (default, one arg, and copy)
IcecreamCalories();
IcecreamCalories( unsigned int numberOfFlavors);
IcecreamCalories( const IcecreamCalories& original);
// destructor
~IcecreamCalories();
// Member function
int calorieAtIndex( int index );
};

Please answer the following two questions, typing the item number before each answer:

i) Why MUST the parameter of the copy constructor be of type constant reference?

ii) If this is in the Class Specification (header) file IcecreamCalories.hpp, write the function definitions that would be in the Class Implementation (source) file IcecreamCalories.cpp.

Solutions

Expert Solution

1. If the argument is of type call by value, then a temporary variable of class type needs to created and to be initialized with the value of the object passed in the function call. Since this is constructor call, to construct the temporary object it will call again the constructor and repeated and hence the program will terminates with segmentation fault.

2. Program

//IcrecreamCalories.cpp

#include<iostream>
using namespace std;
#include "IcrecreamCalories.h"

void IceCreamCalories::InitializeData()
{
   for (int i = 0; i < num; i++)
   {
       calories[i] = 0;
   }
}

IceCreamCalories::IceCreamCalories()
{
   calories = new unsigned int[25];
   num = 25;
   InitializeData();
}

IceCreamCalories::IceCreamCalories(unsigned int numberOfFlavors)
{
   calories = new unsigned int[numberOfFlavors];
   num = numberOfFlavors;
   InitializeData();
}

IceCreamCalories::IceCreamCalories(const IceCreamCalories& original)
{
   calories = new unsigned int[original.num];
   num = original.num;
   for (int i = 0; i < num; i++)
   {
       calories[i] = original.calories[i];
   }
}

IceCreamCalories::~IceCreamCalories()
{
   if (calories != NULL)
   {
       delete[] calories;
       calories = NULL;
   }
}

int IceCreamCalories::calorieAtIndex(int index)
{
   if (index >= 0 && index < num)
       return calories[index];

   return -1;
}

bool IceCreamCalories::setCalorieAtIndex(int index, int itemColories)
{
   if (index >= 0 && index < num)
   {
       calories[index] = itemColories;
       return true;
   }
   return false;
}

//IceCreamCalories.h

class IceCreamCalories
{
private:
   unsigned int* calories;
   size_t num;
   void InitializeData();
public:
   // constructors (default, one arg, and copy)
   IceCreamCalories();
   IceCreamCalories(unsigned int numberOfFlavors);
   IceCreamCalories(const IceCreamCalories& original);
   // destructor
   ~IceCreamCalories();
   // Member function
   int calorieAtIndex(int index);
   bool setCalorieAtIndex(int index, int itemColories);
};

//main.cpp

#include<iostream>
using namespace std;
#include "IcrecreamCalories.h"
int main()
{
   IceCreamCalories obj(3);
   for (int i = 0; i < 3; i++)
   {
       obj.setCalorieAtIndex(i, 100 * (i + 1));
   }
   cout << "obj constructed by parametrized constructor: calories--" << endl;
   for (int i = 0; i < 3; i++)
   {
       cout<< obj.calorieAtIndex(i)<<" ";
   }
   cout << endl;
   IceCreamCalories obj1(obj);

   cout << "object constructed using copy constructor: calories-- " << endl;
   for (int i = 0; i < 3; i++)
   {
       cout << obj1.calorieAtIndex(i) << " ";
   }
   cout << endl;
   //Modify the obj.
   for (int i = 0; i < 3; i++)
   {
       obj.setCalorieAtIndex(i, 200 * (i + 1));
   }

   cout << "obj values with modified calorie data: calories--" << endl;
   for (int i = 0; i < 3; i++)
   {
       cout << obj.calorieAtIndex(i) << " ";
   }
   cout << endl;
   cout << "Note the change of obj, don't effect the obj1, because of deep copy in the copy constructor" << endl;

}

//output


Related Solutions

At an ice cream store, there are 6 flavors of ice cream: banana, strawberry, vanilla, mint,...
At an ice cream store, there are 6 flavors of ice cream: banana, strawberry, vanilla, mint, chocolate and raspberry. How many different 2- flavor ice cream cones can be made? (5 points) Steve has 6 pants and 7 shirts in his closet. He wants to wear a different pant/shirt combination each day without buying new cloths for as long as he can. How many weeks can he do this for? (5 points)
Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
1. I visit an ice cream place that has 60 flavors! Suppose I'm going to have...
1. I visit an ice cream place that has 60 flavors! Suppose I'm going to have a 4 scoop ice cream cone. I care about the order of flavors a. How many choices do I have? (I can repeat the flavors) b. I now want three different flavors. How many choices do I have? c. I now decide that I only want one flavor for my 3 scoop cone. How many choices do I have? d. I want two flavors...
An ice-cream shop serves 5 flavors ice-cream to its customers. The shop has seating capacity for...
An ice-cream shop serves 5 flavors ice-cream to its customers. The shop has seating capacity for 6 people. One fine Saturday morning, as the shop opens, 4 kids enter the shop for ice-cream. Please provide expanded formulas or expressions to answer following questions. You need not solve the final arithmetic. 1. What are the different ways in which the 4 kids can sit? 2. What are the different types of orders possible, if each kid orders exactly one serving of...
An ice cream shop sells five-scoop ice cream cones, allowing customers to pick which flavors they...
An ice cream shop sells five-scoop ice cream cones, allowing customers to pick which flavors they want stacked on their cone. The shop has the following ice cream flavors available: chocolate, vanilla, strawberry, mint & chocolate chip, rocky road, cookies & cream, cookie dough, cotton candy, butter pecan, birthday cake, and cherry. Note that customers can order a five-scoop cone with multiple scoops of the same flavor. a) How many five-scoop cones are possible if order of the ice cream...
Graeter’s is thinking about expanding its ice cream flavors. They have created three new flavors of...
Graeter’s is thinking about expanding its ice cream flavors. They have created three new flavors of ice cream: (1) Lemon Merengue Pie, (2) Butterscotch, and (3) Banana Cream Pie. They recruit 18 people to participate in their study, and they assign each participant to taste-test one of their new ice cream flavors. After tasting the flavor, participants rate their likelihood of ordering that ice cream flavor on their next trip to Graeter’s, using a scale from 1 (I definitely wouldn’t...
Helena Lorimer runs a set of ice cream cafes that sell mainly three flavors of ice...
Helena Lorimer runs a set of ice cream cafes that sell mainly three flavors of ice cream: vanilla, chocolate, and strawberry. Hot weather and high demand have caused her to run short of the main ingredients: milk, sugar, and cream. She has decided to make the best assortment of ice cream quantities in these three flavors and ration out the deliveries to the cafes. She has collected data on the profitability of the various flavors, availability of supplies, and the...
Helena Lorimer runs a set of ice cream cafes that sell mainly three flavors of ice...
Helena Lorimer runs a set of ice cream cafes that sell mainly three flavors of ice cream: vanilla, chocolate, and strawberry. Hot weather and high demand have caused her to run short of the main ingredients: milk, sugar, and cream. She has decided to make the best assortment of ice cream quantities in these three flavors and ration out the deliveries to the cafes. She has collected data on the profitability of the various flavors, availability of supplies, and the...
The World Health Organization (WHO) keeps track of how many incidents of leprosy there are in...
The World Health Organization (WHO) keeps track of how many incidents of leprosy there are in the world. Using the WHO regions and the World Banks income groups, onc can sask if an income level and a WHO region are dependent on each other in terms of predicting where the disease is. Data on leprosy cases in different countries was collected for hte year 2011 and a summary is present in the following table. Two-Way Table of Leprosy Cases: WHO...
A researcher is examining preferences among four new flavors of ice cream. A sample of n...
A researcher is examining preferences among four new flavors of ice cream. A sample of n = 80 people is obtained. Each person tastes all four flavors and then picks his/her favorite. The distribution of preferences is as follows. Ice Cream Flavors A B C D 6 18 34 22 Do these data indicate any significant preferences among the four flavors? Test at the .05 level of significance and report your findings.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT