c++ polymorphism - I need to modify this project by demonstrating virtual functi
ID: 3697740 • Letter: C
Question
c++ polymorphism - I need to modify this project by demonstrating virtual functions and inheritance
Lottery.h
*************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 5
#define MAX_RANGE 10
using namespace std;
class Lottery
{
private:
int lottery[SIZE];
public:
Lottery();
~Lottery();
virtual void intro();
virtual void generateNumbers();
virtual int findMatches(Lottery &);
friend ostream &operator<<(ostream &, Lottery &);
friend istream &operator>>(istream &, Lottery &);
bool operator==(Lottery &);
bool operator!=(Lottery &);
Lottery &operator=(Lottery &);
Lottery &operator+(Lottery &);
};
Lottery.cpp
************************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
using namespace std;
Lottery::Lottery(){}
Lottery::~Lottery(){}
void Lottery::intro()
{
cout << "Welcome to the grand prize lottery game!" << endl;
cout << "Win $5 if you have 1 match number!" <<endl;
cout << "Win $20 if you have 2 match numbers!" <<endl;
cout << "Win $100 if you have 3 match numbers!" <<endl;
cout << "Win a trip to the Paris if you have 4 match numbers!" <<endl;
cout << "Win a brand new BMW if you have all 5 match numbers!" <<endl;
cout << "Now Please enter five numbers 0 through 9 for lottery numbers" << endl;
cout << endl;
}
void Lottery::generateNumbers()
{
srand((unsigned)time(NULL));
for (int i = 0; i < SIZE; i++)
{
lottery[i] = 0 + rand() % MAX_RANGE;
cout << "lottery[" << i+1 << "] = " << lottery[i] << " " << endl;
}
cout << "For dubugging purpose only" << endl;
cout << endl << endl;
}
int Lottery::findMatches(Lottery &obj)
{
int matches = 0;
for (int i = 0; i < SIZE; ++i)
{
if (lottery[i] == obj.lottery[i])
matches = matches + 1;
}
return matches;
}
ostream &operator<<(ostream &os, Lottery &obj)
{
for (int i = 0; i < SIZE; ++i)
os << obj.lottery[i] << " ";
os << endl;
return os;
}
istream &operator>>(istream &is, Lottery &obj)
{
for (int i = 0; i < SIZE; ++i)
{
float temp;
cout << "Please enter number # " << (i + 1) << " : ";
is >> temp;
while (is.fail() || (int)temp != temp || temp < 0 || temp > 9 || getchar() != ' ')
{
cout << "Invalid input. Enter five numbers 0 through 9 for lottery numbers: ";
is.clear();
is.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
is >> temp;
cout << " " << endl;
}
obj.lottery[i] = (int)temp;
}
return is;
}
bool Lottery::operator==(Lottery &obj)
{
for (int i = 0; i < SIZE; i++){
if (lottery[i] != obj.lottery[i])
return false;
}
return true;
}
bool Lottery::operator!=(Lottery &obj)
{
return (*this) == obj;
}
Lottery &Lottery::operator=(Lottery &obj)
{
for (int i = 0; i < SIZE; i++)
lottery[i] = obj.lottery[i];
return *this;
}
Lottery &Lottery::operator+(Lottery &obj)
{
Lottery temporal = *this;
for (int i = 0; i < 5; i++){
lottery[i] += obj.lottery[i];
if (lottery[i] > 9){
lottery[i] -= 10;
if (i != 4)
lottery[i + 1]++;
else
return temporal;
}
}
return *this;
}
Main.cpp
**************************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main()
{
//create an object
Lottery player, automate;
automate.generateNumbers();
int numMatches; // Number of matches
player.intro();
cin >> player;
cout << "Player lottery: " << player << " Lottery Number: " << automate << endl;
numMatches = player.findMatches(automate);
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
Answer for Question:
To solve this given code i created the new class called LotteryType with three methods which are defined as a virtual
in Lottery class.
These intro , generateNumbers and findMatches methods are defined in base and derived publicly in Lottery class.
so that Lottery class can access the derived class methods.
And also defined in main method for object created with help of base class pointer with derived class object.
main Method code:
//create an object
LotteryType *base = new Lottery()
class LotteryType
{
private:
string type;
public:
LotteryType()
{
type = "Number Lottery";
}
void intro()
{
cout<<"Lottery Type:"<<type<<endl;
}
void generateNumbers()
{
// This method will generate the lottery numbers and derived
cout<<"generateNumbers method will generate the numbers :"<<endl;
}
void findMatches()
{
// This method will match the Lottery numbers
cout<<"findMatches method will match lottery numbers :"<<endl;
}
};
class Lottery : public LotteryType
{
// Lottery class
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.