Question

In: Computer Science

Create a derived class that allows you to reset the number of sides on a die....

  1. Create a derived class that allows you to reset the number of sides on a die. (Hint: You will need to make one change in diceType.) -- Completed in Part 1
  2. Overload the << and >> operators. -- Completed in Part 1
  3. Overload the +, -, and ==, <, > operators for your derived class.
  4. Overload the = operator.

I need help with 3 and 4 of the question. I've already completed step 1 and 2

Note: You should test each step in a client program.

//diceType.h

#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.

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

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

//diceTypeImp.cpp

//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;
}

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

//diceTypeDerived.h

#ifndef diceTypeDerived_H
#define diceTypeDerived_H

#include "diceType.h"
#include
#include


class diceTypeDerived : public diceType
{
friend std::ostream& operator<<(std::ostream&, const diceTypeDerived &);
friend std::istream& operator>>(std::istream&, diceTypeDerived &);

public:
diceTypeDerived(int = 6);

void SetSides(int);
};

#endif // diceTypeDerived_H

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

//diceTypeDerived.cpp (Implementation file)

#include "diceTypeDerived.h"

diceTypeDerived::diceTypeDerived(int sides) : diceType(sides) { }

std::ostream& operator << (std::ostream& osObject, const diceTypeDerived& dice) {

osObject << dice.numRolled;

return osObject;
}

std::istream& operator >> (std::istream& isObject, diceTypeDerived& dice) {
int tempNum;

isObject >> tempNum;

if (tempNum < dice.numSides)
dice.numRolled = tempNum;
else
dice.numRolled = dice.numSides;

return isObject;
}

void diceTypeDerived::SetSides(int newSides){
numSides = newSides;
}

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

//test.cpp

#include "diceTypeDerived.h"
#include

using namespace std;

int main() {
diceTypeDerived dice1, dice2;

dice1.roll();
dice1.SetSides(12);
dice1.roll();

cout << "Set value rolled for dice2: ";
cin >> dice2;

cout << "dice1: " << dice1 << " dice2: " << dice2 << endl;

return 0;
}

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : I have used +,-,< and > operators to compare numRolled of two dices. If you want to compare numSides please replace numRolled with numSides in respective overloaded methods.

I have used dev-c++ to run the code, hence I had to link .cpp files instead of .h files in diceTypeDerived.h file and test.cpp files.Please link the files as per the linking type of your compiler before execution(if using"#include 'diceType.h' and #include 'diceTypeDerived.cpp' " in diceTypeDerived.h and test.cpp respectively works for you,please include them only).

#######################################################################

CODE :

diceType.h

#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.
  
   protected:
       int numSides;
       int numRolled;
};
#endif // H_diceType

###################################################################

diceTypeImp.cpp

//Implementation File for the class diceType
#include "diceType.h"
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

diceType::diceType(){
   srand(time(0));
   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;
}

################################################################

diceTypeDerived.h

#ifndef diceTypeDerived_H
#define diceTypeDerived_H

#include<cstdlib>
#include<iostream>
#include "diceTypeImp.cpp"

class diceTypeDerived : public diceType
{
  
   friend std::ostream& operator<<(std::ostream&,const diceTypeDerived &);
   friend std::istream& operator>>(std::istream&,diceTypeDerived &);
   public:
       int operator+( diceTypeDerived &obj);      
       int operator-( diceTypeDerived &obj);                  
       bool operator==( diceTypeDerived &obj);                  
       bool operator<( diceTypeDerived &obj);                  
       bool operator>( diceTypeDerived &obj);          
       void operator=(diceTypeDerived &obj);          
              
       diceTypeDerived(int = 6);  
       void SetSides(int);
};

#endif // diceTypeDerived_H

################################################################

diceTypeDerived.cpp

#include "diceTypeDerived.h"

diceTypeDerived::diceTypeDerived(int sides) : diceType(sides) { }  
std::ostream& operator << (std::ostream& osObject, const diceTypeDerived& dice) {
   osObject << dice.numRolled;
   return osObject;
}
std::istream& operator >> (std::istream& isObject,diceTypeDerived& dice) {
   int tempNum;  
   isObject >> tempNum;
  
   if (tempNum < dice.numSides)
       dice.numRolled = tempNum;
   else
       dice.numRolled = dice.numSides;  
   return isObject;
}
void diceTypeDerived::SetSides(int newSides){
   numSides = newSides;
}
int diceTypeDerived::operator+(diceTypeDerived &obj){
   //Based on requirement you can add sides in the same manner by replaced numRolled with numSides
   int totalRolled;
   totalRolled = this->numRolled + obj.numRolled;
   return totalRolled;
}
int diceTypeDerived:: operator-(diceTypeDerived &obj){
   //Based on requirement you can subtract sides in the same manner by replaced numRolled with numSides
   int extraRolled;
   extraRolled = abs(this->numRolled - obj.numRolled);
   return extraRolled;
}
bool diceTypeDerived::operator==(diceTypeDerived &obj){
   //Based on requirement you can compare sides in the same manner by replaced numRolled with numSides
   return (this->numRolled == obj.numRolled)?true:false;
}
bool diceTypeDerived::operator<(diceTypeDerived &obj){
   //Based on requirement you can compare sides in the same manner by replaced numRolled with numSides
   return (this->numRolled < obj.numRolled)?true:false;
}
bool diceTypeDerived::operator>(diceTypeDerived &obj){
   //Based on requirement you can compare sides in the same manner by replaced numRolled with numSides
   return (this->numRolled > obj.numRolled)?true:false;
}
void diceTypeDerived::operator=(diceTypeDerived &obj){
   this->numRolled = obj.numRolled;
   this->numSides = obj.numSides;
}  

################################################################

test.cpp

#include "diceTypeDerived.cpp"
using namespace std;
int main() {  
   diceTypeDerived dice1, dice2;  
   dice1.roll();
   dice1.SetSides(12);
   dice1.roll();
  
   cout << "Set value rolled for dice 2 : ";
   cin >> dice2;  
   cout << "Dice 1 : " << dice1 << " Dice 2 : " << dice2 << endl;
   dice1.roll();
   dice2.roll();
   cout << endl << "Rolled the dices." << endl << "Dice 1 : " << dice1 << " Dice 2 : " << dice2 << endl;
   cout << endl << "Sum of the two dices : " << (dice1 + dice2) << endl;
   cout << endl << "Difference of the two dices : " << (dice1 - dice2) << endl;
   string greater = (dice1 > dice2)?"Yes":"No";
   cout << endl << "Is Dice 1 greater than Dice 2? " << greater << endl;
  
   string lesser = (dice1 < dice2)?"Yes":"No";
   cout << endl << "Is Dice 1 lesser than Dice 2? " << lesser << endl;
  
   string equals = (dice1 == dice2)?"Yes":"No";
   cout << endl << "Is Dice 1 equals Dice 2? " << equals << endl;
  
   dice1 = dice2;
   cout << endl << "Dice 2 is assigned to dice 1." << endl;
   cout << "Dice 1 : " << dice1 << " Dice 2 : " << dice2 << endl;
   return 0;
}

############################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

diceType.h

############################################################################

diceTypeImp.cpp

#####################################################################

diceTypeDerived.h

##############################################################

diceTypeDerived.cpp

#####################################################################

test.cpp

####################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

A reset mortgage allows for one interest rate reset during the life of the loan. The...
A reset mortgage allows for one interest rate reset during the life of the loan. The mortgage rate will be reset after 5 years, to fully amortize at the end of the original 30 year period (i.e. after 25 more years). For a 6 5/8%, $120,000, mortgage, compute the reset payment if the new rate resets to 7 3/8%. (Hint: calculate how much balance is left after you pay for 5 years at the rate of 6.625%, then use the...
JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping...
C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping cart and get the total price of purchases made. Items are simply described by an Item class as follows: class Item {   public:      std :: String description;      float price; }; The ShopCart class must be able to add and remove items and display an invoice. This class must use a dynamically allocated array of items whose capacity is fixed in advance to the build....
suppose we take a die with 3 on three sides 2 on two sides and 1...
suppose we take a die with 3 on three sides 2 on two sides and 1 on one side, roll it n times and let Xi be the number of times side i appeared find the conditional distribution P(X2=k|X3=m)
Suppose that you roll a die and your score is the number shown on the die....
Suppose that you roll a die and your score is the number shown on the die. On the other hand, suppose that your friend rolls five dice and his score is the number of 6’s shown out of five rollings. Compute the probability (a) that the two scores are equal. (b) that your friend’s score is strictly smaller than yours.
Consider four nonstandard dice, whose sides are labeled as follows (the 6 sides on each die...
Consider four nonstandard dice, whose sides are labeled as follows (the 6 sides on each die are equally likely). A: 4, 4, 4, 4, 0, 0 B: 3, 3, 3, 3, 3, 3 C: 6, 6, 2, 2, 2, 2 D: 5, 5, 5, 1, 1, 1 These four dice are each rolled once. Let A be the result for die A, B be the result for die B, etc. (a) Find P(A > B), P(B > C), P(C >D),...
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
Part I – Understand a Given Class (die class) (40 pts) • Download the die class...
Part I – Understand a Given Class (die class) (40 pts) • Download the die class (attached as usingDieClass.cpp), save it as LabUseDieClassFirstName1_FirstName2.cpp • Add proper opening comments at the beginning of the program. The comments must include the description of all three parts of this lab. You may want to modify the comments after all parts are done to be sure that it is done properly. • Run the program and answer the following questions: o How many objects...
An ordinary (fair) die is a cube with the numbers through on the sides (represented by...
An ordinary (fair) die is a cube with the numbers through on the sides (represented by painted spots). Imagine that such a die is rolled twice in succession and that the face values of the two rolls are added together. This sum is recorded as the outcome of a single trial of a random experiment. Compute the probability of each of the following events. Event A : The sum is greater than 5 . Event B: The sum is an...
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT