Question

In C++ Write a program that contains a class called VideoGame. The class should contain the...

In C++

Write a program that contains a class called VideoGame.
The class should contain the member variables:

Name
price
rating

Specifications:

Dynamically allocate all member variables.
Write get/set methods for all member variables.
Write a constructor that takes three parameters and initializes the member variables.
Write a destructor.
Add code to the destructor. In addition to any other code you may put in the destructor you should also add
a cout statement that will print the message “Destructor Called”.
Figure out where to allocate and release memory.
Create an instance of VideoGame in main.
You should set the values on the instance and then print them out on the console.

Modify the previous program. Make the following changes:

Inside of main create a dynamically allocated two element array of VideoGame objects.
Set the values of both elements of the array.
Make sure you release memory allocated to the array.
Write a method on the VideoGame class called Show that will display the values of all member variables.
Write a loop that will iterate through the array and display the contents of each VideoGame object.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

(1)

#include <iostream>
#include <string>

using namespace std;

//Class definition
class VideoGame
{
    //Private member variables
    private:
        string name;
        double price;
        int rating;
        string specifications;

    public:
        //Constructor
        VideoGame(string vname, double vprice, double vrating)
        {
            name = vname;
            price = vprice;
            rating = vrating;
        }

        //Setter Methods
        void setName(string vname)
        {
            name = vname;
        }

        void setPrice(double vprice)
        {
            price = vprice;
        }

        void setRating(int vrating)
        {
            rating = vrating;
        }

        void setSpecifications(string vSpec)
        {
            specifications = vSpec;
        }

        //Getter Methods
        string getName()
        {
            return name;
        }

        double getPrice()
        {
            return price;
        }

        int getRating()
        {
            return rating;
        }

        string getSpecifications()
        {
            return specifications;
        }

        //Destructor
        ~VideoGame()
        {
            cout << "\n Destructor Called... \n";
        }
};

//Main function
int main()
{
    //Creating object
    VideoGame game("Tom & Jerry", 56.0, 2);

    //Calling getter methods
    cout << "\n Game Name: " << game.getName();
    cout << "\n Game Price: $" << game.getPrice();
    cout << "\n Game Rating: " << game.getRating();

    cout << "\n\n";
    return 0;
}


Sample Run:

Т САТС\VideoGamebin\DebugVideoGame.exe Game Name: Tom & Jerry Game Price: $56 Game Rating: 2 Destructor Call1ed... Process re

______________________________________________________________________________________________

(2)

#include <iostream>
#include <string>

using namespace std;

//Class definition
class VideoGame
{
    //Private member variables
    private:
        string name;
        double price;
        int rating;
        string specifications;

    public:
        //Default Constructor
        VideoGame()
        {
            name = "";
            price = 0.0;
            rating = 0;
        }

        //Constructor
        VideoGame(string vname, double vprice, int vrating)
        {
            name = vname;
            price = vprice;
            rating = vrating;
        }

        //Setter Methods
        void setName(string vname)
        {
            name = vname;
        }

        void setPrice(double vprice)
        {
            price = vprice;
        }

        void setRating(int vrating)
        {
            rating = vrating;
        }

        void setSpecifications(string vSpec)
        {
            specifications = vSpec;
        }

        //Getter Methods
        string getName()
        {
            return name;
        }

        double getPrice()
        {
            return price;
        }

        int getRating()
        {
            return rating;
        }

        string getSpecifications()
        {
            return specifications;
        }

        //Method that displays Game details
        void show()
        {
            //Calling getter methods
            cout << "\n\n Game Name: " << getName();
            cout << "\n Game Price: $" << getPrice();
            cout << "\n Game Rating: " << getRating();
            cout << "\n Game Specifications: " << getSpecifications() << "\n\n";
        }

        //Destructor
        ~VideoGame()
        {
            cout << "\n Destructor Called... \n";
        }
};

//Main function
int main()
{
    int i;

    //Creating Array of object
    VideoGame *games;

    //Dynamically allocating memory
    games = new VideoGame[2];

    //Assigning values
    games[0].setName("GTA Vice City");
    games[0].setPrice(250.25);
    games[0].setRating(3);

    games[1].setName("NFS");
    games[1].setPrice(189.50);
    games[1].setRating(5);

    //Iterating over array and printing values
    for(i=0; i<2; i++)
    {
        games[i].show();
    }

    delete[] games;

    cout << "\n\n";
    return 0;
}

Sample Run:

Add a comment
Know the answer?
Add Answer to:
In C++ Write a program that contains a class called VideoGame. The class should contain the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • In C++ Create a class called MyInteger. It should have a field of type pointer-to-int called...

    In C++ Create a class called MyInteger. It should have a field of type pointer-to-int called pInteger. It should have a constructor that takes as a parameter an int - the constructor will then dynamically allocate memory for an int, using pInteger, and assign the parameter's value to that memory. The class should have a destructor that will deallocate that memory when the object is destroyed. You should write a copy constructor that will correctly make a separate copy of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT