In: Computer Science
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;
}
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 class diceType |
###################################################################
diceTypeImp.cpp
//Implementation File for the class diceType using namespace std; diceType::diceType(){ diceType::diceType(int sides){ int diceType::roll(){ int diceType::getNum() const{ |
################################################################
diceTypeDerived.h
#ifndef diceTypeDerived_H #include<cstdlib> class diceTypeDerived : public diceType #endif // diceTypeDerived_H |
################################################################
diceTypeDerived.cpp
#include "diceTypeDerived.h" diceTypeDerived::diceTypeDerived(int sides) :
diceType(sides) { } |
################################################################
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 :)