This project is continuation Code as below. In this assignment you will use inhe
ID: 3815521 • Letter: T
Question
This project is continuation Code as below. In this assignment you will use inheritance, Interfaces, abstract classes and polymorphism to generate random numbers as easy pick for two different lotto games: Lotto and Small Lotto.
#include<iostream>
#include <cstdlib>
using namespace std;
class GameMenu
{
public:
int numbers[5], wildNum;
int arraySize;
GameMenu(){
/*Initiate with default value*/
arraySize = 0;
}
GameMenu(int size){ //--overload constructor
/*Initiate with value*/
arraySize = size;
}
void generateLottoNumbers(){
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand();
}
}
void display(){
for(int inc=0; inc<arraySize; inc++)
{
cout << numbers[inc];
if (inc != arraySize-1)
cout << ",";
}
cout << endl;
}
};
class Lotto:public GameMenu
{
public:
Lotto() : GameMenu(5){}//-- super class contructor value will be assigned here, when subclass call made
void generateLottoNumbers(){ //-- method override
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand() % 54 + 1;
}
}
void getWildNumber(){
int random_number = numbers[rand() % arraySize];
if (random_number > 1 and random_number < 40){
wildNum = random_number;
}
cout << "Your wild numbers is: " << wildNum << endl;
}
};
class SmallLotto:public GameMenu
{
public:
SmallLotto() : GameMenu(5){}//-- super class contructor value will be assigned here, when subclass call made
void generateLottoNumbers(){ //-- method override
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand() % 52 + 1;
}
}
};
class CheckGame{
public:
int main(){
int ch;
Lotto l1;
SmallLotto l2;
do{
cout << "What game do you want to play?" << endl;
cout << "1. Lottp" << " " << "2. Small lotto" << endl << "3. Quit" << endl;
cin >> ch;
if (ch == 1){
l1.generateLottoNumbers();
cout << "Your lotto numbers:";
l1.display();
l1.getWildNumber();
}
if (ch == 2){
l2.generateLottoNumbers();
cout << "Your small lotto numbers:";
l2.display();
}
}while(ch != 3);
return 0;
}
};
int main()
{
CheckGame cg;
cg.main();
return 0;
}
Explanation / Answer
#include<iostream>
#include <cstdlib>
using namespace std;
class GameMenu
{
public:
int numbers[5], wildNum;
int arraySize;
GameMenu(){
arraySize = 0;
}
GameMenu(int size){
arraySize = size;
}
void generateLottoNumbers(){
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand();
}
}
void display(){
for(int inc=0; inc<arraySize; inc++)
{
cout << numbers[inc];
if (inc != arraySize-1)
cout << ",";
}
cout << endl;
}
};
class Lotto:public GameMenu
{
public:
Lotto() : GameMenu(5){}
void generateLottoNumbers(){
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand() % 54 + 1;
}
}
void getWildNumber(){
int random_number = numbers[rand() % arraySize];
if (random_number > 1 and random_number < 40){
wildNum = random_number;
}
cout << "Your wild numbers is: " << wildNum << endl;
}
};
class SmallLotto:public GameMenu
{
public:
SmallLotto() : GameMenu(5){}
void generateLottoNumbers(){
for (int i = 0; i < arraySize; ++i) {
numbers[i] = rand() % 52 + 1;
}
}
};
class CheckGame{
public:
int main(){
int ch;
Lotto l1;
SmallLotto l2;
cout << "What game do you want to play?" << endl;
cout << "1. Lottp" << " " << "2. Small lotto" << endl << "3. Quit" << endl;
cin >> ch;
do{
if (ch == 1){
l1.generateLottoNumbers();
cout << "Your lotto numbers:";
l1.display();
l1.getWildNumber();
}
if (ch == 2){
l2.generateLottoNumbers();
cout << "Your small lotto numbers:";
l2.display();
}
}while(ch != 3);
return 0;
}
};
int main()
{
CheckGame cg;
cg.main();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.