Use c++ programming Name your project and header file DiceClass. Name your .cpp
ID: 3714897 • Letter: U
Question
Use c++ programming
Name your project and header file DiceClass.
Name your .cpp file DiceClient.
Write a C++ class in your header file that represents a die. Include a default constructor that sets the number of sides to 6, and an overloaded constructor that sets the number of sides to any number between 1 and 18.
Include methods to set and get the private variable(s).
Include a method to roll the die and return the result.
Write a C++ program in your .cpp file that tests all of the capabilities of the class.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class DiceClass {
private:
int value;
public :
DiceClass(){
value =6;
}
DiceClass(int v){
value = v;
}
int getValue() {
return value;
}
void setValue(int v) {
value = v;
}
int roll() {
return rand() % value + 1;
}
};
int main()
{
srand (time(NULL));
DiceClass die1, die2(10);
cout << "First die roll: "<<die1.roll() << endl;
cout << "Second die roll: "<<die2.roll() << endl;
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.