c++ polymorphism - I need to modify this project by demonstrating virtual functi
ID: 3698292 • Letter: C
Question
c++ polymorphism - I need to modify this project by demonstrating virtual functions and inheritance. Please show me the complete code.
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
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(){}
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;
}
ExtLottery.cpp
**************************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
using namespace std;
class ExtLottery:public Lottery{
public:
void 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 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 findMatches(Lottery &obj)
{
int matches = 0;
for (int i = 0; i < SIZE; ++i)
{
if (lottery[i] == obj.lottery[i])
matches = matches + 1;
}
return matches;
}
};
Main.cpp
**************************************************************************
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main()
{
//create an object
ExtLottery 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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.