HELP!! I need help to redesign this project by demonstrating inheritance and vir
ID: 3696325 • Letter: H
Question
HELP!! I need help to redesign this project by demonstrating inheritance and virtual functions.
1. Modify the program by demonstrating inheritance
2. Include virtual functions to support polymorphism
Lottery.h
**********************************************************************
#include <iostream>
using namespace std;
class Lottery
{
public:
void generateNumbers(int[], int);
int findMatches(int[], int[], int);
void print(int[], int[], int);
int iInputValidation(float);
};
LotteryClass.cpp
*****************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
const int MAX_RANGE = 10;
void Lottery::generateNumbers(int lottery[], int size)
{
srand(time(NULL));
for (int i = 0; i < size; i++)
{
lottery[i] = 0 + rand() % MAX_RANGE;
cout << "lottery[" << i << "] = " << lottery[i] << " " << endl;
}
cout << "For dubugging purpose only" << endl;
cout << endl << endl;
}
int Lottery::findMatches(int lottery[], int user[], int size)
{
int matches = 0;
for (int i = 0; i < size; ++i)
{
if (user[i] == lottery[i])
matches = matches + 1;
}
return matches;
}
void Lottery::print(int lottery[], int userInput[], int size)
{
cout << "Users numbers: ";
for (int i = 0; i < size; ++i)
cout << userInput[i] << " ";
cout << endl << "Lottery numbers: ";
for (int i = 0; i < size; ++i)
cout << lottery[i] << " ";
cout << endl;
}
int Lottery::iInputValidation(float temp)
{
while (cin.fail() || (int)temp != temp || temp < 0|| temp > 9 || getchar() != ' ')
{
cout << "Invalid input. Enter five numbers 0 through 9 for lottery numbers: ";
cin.clear();
cin.ignore(std::numeric_limits::max(), ' ');
cin >> temp;
cout << " "<< endl;
}
return (int)temp;
}
Main.cpp
*****************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main()
{
//create an object
Lottery player;
const int SIZE = 5;
int lottery[SIZE]; // Lottery numbers
int userInput[SIZE]; // User's numbers
int numMatches; // Number of matches
player.generateNumbers(lottery, SIZE);
cout << "Welcome to the grand prize lottery game!" << endl;
cout << "Win $5 if you have 1 match number!" < cout << "Win $20 if you have 2 match numbers!" < cout << "Win $100 if you have 3 match numbers!" < cout << "Win a trip to the Paris if you have 4 match numbers!" < cout << "Win a brand new BMW if you have all 5 match numbers!" < cout << "Now Please enter five numbers 0 through 9 for lottery numbers" << endl;
cout << endl;
float temp;
for (int i=0; i<5; ++i)
{
cout << "Please enter number # " << (i + 1) << " : ";
cin >> temp;
userInput[i] = player.iInputValidation(temp);
}
numMatches = player.findMatches(lottery, userInput, SIZE);
player.print(lottery, userInput, SIZE);
cout << endl;
cout << "You matched " << numMatches << " numbers. ";
if (numMatches == 1)
cout << "Congratulations! You just won $5! "<< endl ;
else if (numMatches == 2)
cout << "Congratulations! You just won $20! "<< endl ;
else if (numMatches == 3)
cout << "Congratulations! You just won $100! "<< endl ;
else if (numMatches == 4)
cout << "Congratulations! You just won a trip to the Paris! "<< endl ;
else if (numMatches == 5)
cout << "Congratulations! You're the grand prize winner!! "
<<"You just won a new BMW"<< endl ;
else
cout << "Thanks for your participation. "
"Try again next time. Good Luck!" << endl;
system("pause");
return 0;
}
Explanation / Answer
For implementing the virtual functionality as well as the inheritance we can create a abstract base class, where we can declerate the methods are virtual
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class baseLottery
{
public:
virtual void generateNumbers(int[], int) = 0;
virtual int findMatches(int[], int[], int);
virtual void print(int[], int[], int);
virtual int iInputValidation(float);
};
Now Lottery class will inherit this base class
#include <iostream>
using namespace std;
class Lottery:public baseLottery
{
public:
void generateNumbers(int[], int);
int findMatches(int[], int[], int);
void print(int[], int[], int);
int iInputValidation(float);
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.