Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ polymorphism - I need to modify this project by demonstrating virtual functions and inheritance. Please show me the complete code. Lottery.h ************************************************************* #include #include

c++ polymorphism - I need to modify this project by demonstrating virtual functions and inheritance. Please show me the complete code.

Lottery.h

*************************************************************

#include #include #include

#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 #include #include #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!" <

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::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 #include #include #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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions