Question

In: Computer Science

Modify your dice class to be a class template supporting other types for numRolled. Provide a test program demonstrating it working with different types.

 

Complete the following...

  1. Modify your dice class to be a class template supporting other types for numRolled. Provide a test program demonstrating it working with different types.
  2. Bonus: Convert your variable size dice class to a template class and demonstrate overloads working.

Submit:

  • Commented source for dice template(s) and test zipped into a single file.

 

//diceType.h (header file)

#ifndef H_diceType
#define H_diceType

class diceType
{
public:
diceType();
// Default constructor
// Sets numSides to 6 with a random numRolled from 1 - 6

diceType(int);
// Constructor to set the number of sides of the dice

int roll();
// Function to roll a dice.
// Randomly generates a number between 1 and numSides
// and stores the number in the instance variable numRolled
// and returns the number.

int getNum() const;
// Function to return the number on the top face of the dice.
// Returns the value of the instance variable numRolled.

private:
int numSides;
int numRolled;
};
#endif // H_diceType

============================================

//diceTypeImp.cpp (implementatio file)

//Implementation File for the class diceType

#include
#include
#include
#include "diceType.h"

using namespace std;

diceType::diceType()
{
srand(time(nullptr));
numSides = 6;
numRolled = (rand() % 6) + 1;
}

diceType::diceType(int sides)
{
srand(time(0));
numSides = sides;
numRolled = (rand() % numSides) + 1;
}

int diceType::roll()
{
numRolled = (rand() % numSides) + 1;

return numRolled;
}

int diceType::getNum() const
{
return numRolled;
}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

diceType.h


#ifndef DICETYPE_H_
#define DICETYPE_H_


#include
#include
#include
#include
template
class diceType
{
public:
   diceType();
   // Default constructor
   // Sets numSides to 6 with a random numRolled from 1 - 6

   diceType(int);
   // Constructor to set the number of sides of the dice

   Type roll();
   // Function to roll a dice.
   // Randomly generates a number between 1 and numSides
   // and stores the number in the instance variable numRolled
   // and returns the number.

   Type getNum() const;
   // Function to return the number on the top face of the dice.
   // Returns the value of the instance variable numRolled.

   Type getType(int);
   Type getType(char);
private:
   int numSides;
   Type numRolled;
};

#endif /* DICETYPE_H_ */

diceTypeImp.cpp

#include "diceType.h"

using namespace std;
template
diceType::diceType()
{
   srand(time(nullptr));
   numSides = 6;
   numRolled = (rand() % 6) + 1;
}
template
Type diceType::getType(int i){
   return i;
}
template
Type diceType::getType(char i){
   return 'A'+i-1;
}

template
diceType::diceType(int sides)
{
   srand(time(0));
   numSides = sides;
   numRolled = (rand() % numSides) + 1;
   numRolled=getType(numRolled);
}


template
Type diceType::roll()
{
   numRolled = (rand() % numSides) + 1;
   numRolled=getType(numRolled);
   return numRolled;
}
template
Type diceType::getNum() const
{
   return numRolled;
}

main.cpp

#include
#include
#include "diceType.h"
#include "diceTypeImp.cpp"
using namespace std;

int main () {
   diceType dice(6);
   cout<<"Rolling int dice..."<    for(int i=0;i<10;i++){
       cout<

   }


   diceType dice2(6);
   cout<<"Rolling char dice..."<    for(int i=0;i<10;i++){
       cout<    }
   return 0;
}


Related Solutions

Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
Complete the coding/testing of the heap sort method we began in class. Then modify your program...
Complete the coding/testing of the heap sort method we began in class. Then modify your program so that it outputs a comparison of the actual number of swaps it performs to the predicted number of swaps, and a comparison of the actual sort effort to the predicted and minimum sort effort. The output should be formatted exactly as shown below. Actual swaps = xxxx; Predicted swaps = xxxx Actual sort effort = xxxx; Predicted sort effort = xxxx; Min sort...
Why is medical terminology important? How is it different from other types of writing? Provide 3...
Why is medical terminology important? How is it different from other types of writing? Provide 3 examples of types of medical writing.
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
How is auditing different from other types of accounting? In your view should anything be done...
How is auditing different from other types of accounting? In your view should anything be done to strengthen the auditing profession and the quality of audit reports?
What are relevant Audit and applicable standards discuss different types of assurances your firm can provide...
What are relevant Audit and applicable standards discuss different types of assurances your firm can provide in South Africa context
How is conducting graduate-level research different from research you did in your undergraduate program? Provide specific...
How is conducting graduate-level research different from research you did in your undergraduate program? Provide specific examples.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT