Utilize the code from last week
Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class VideoGame {
private:
string name;
int highScore;
int numOfPlays;
public:
VideoGame() {
name = "NA";
highScore = 0;
numOfPlays = 0;
}
VideoGame(string n, int score, int num) {
name = n;
highScore = score;
numOfPlays = num;
}
void setName(string n) {
name = n;
}
void setHighScore(int score) {
highScore = score;
}
void setNumOfPlays(int num) {
numOfPlays = num;
}
string getName() {
return name;
}
int getHighScore() {
return highScore;
}
int getNumOfPlays() {
return numOfPlays;
}
};
string inputName() {
string n;
cout << "Enter name of video game: ";
getline(cin >> ws, n);
return n;
}
int inputHighScore() {
int score;
cout << "Enter current high score of game: ";
cin >> score;
return score;
}
int inputNumOfPlays() {
int num;
cout << "Enter number of times game is played: ";
cin >> num;
return num;
}
void output(VideoGame game) {
cout << game.getName() << endl
<< "Played " << game.getNumOfPlays() << " times (HIGH SCORE " << game.getHighScore() << ")" << endl;
}
int main() {
VideoGame game1, game2;
game1.setName(inputName());
game1.setHighScore(inputHighScore());
game1.setNumOfPlays(inputNumOfPlays());
cout << "\nGame1" << endl;
output(game1);
cout << endl;
game2.setName(inputName());
game2.setHighScore(inputHighScore());
game2.setNumOfPlays(inputNumOfPlays());
cout << "\nGame2" << endl;
output(game2);
return 0;
}
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class VideoGame {
private:
string name;
int highScore;
int numOfPlays;
public:
VideoGame() {
name = "NA";
highScore = 0;
numOfPlays = 0;
}
VideoGame(string n, int score, int num) {
name = n;
highScore = score;
numOfPlays = num;
}
VideoGame(string n) {
name = n;
highScore = 0;
numOfPlays = 0;
}
VideoGame(const VideoGame &p2)
{
name = p2.name;
highScore = p2.highScore;
numOfPlays = p2.numOfPlays;
}
void setName(string n) {
name = n;
}
void setHighScore(int score) {
highScore = score;
}
void setNumOfPlays(int num) {
numOfPlays = num;
}
string getName() {
return name;
}
int getHighScore() {
return highScore;
}
int getNumOfPlays() {
return numOfPlays;
}
};
string inputName() {
string n;
cout << "Enter name of video game: ";
getline(cin >> ws, n);
return n;
}
int inputHighScore() {
int score;
cout << "Enter current high score of game: ";
cin >> score;
return score;
}
int inputNumOfPlays() {
int num;
cout << "Enter number of times game is played: ";
cin >> num;
return num;
}
void output(VideoGame game) {
cout << game.getName() << endl
<< "Played " << game.getNumOfPlays() << " times (HIGH SCORE " << game.getHighScore() << ")" << endl;
}
int main() {
VideoGame game1, game2, game3("VideoGame 3");
game1.setName(inputName());
game1.setHighScore(inputHighScore());
game1.setNumOfPlays(inputNumOfPlays());
cout << "\nGame1" << endl;
output(game1);
cout << endl;
game2.setName(inputName());
game2.setHighScore(inputHighScore());
game2.setNumOfPlays(inputNumOfPlays());
cout << "\nGame2" << endl;
output(game2);
cout << "\nGame3" << endl;
output(game3);
VideoGame game4 = game2;
cout << "\nGame4" << endl;
output(game4);
return 0;
}
=============================================
SEE OUTPUT
====================================================
Thanks, let me know if you need more information. PLEASE COMMENT if
there is any concern.
Get Answers For Free
Most questions answered within 1 hours.