Consider a class Movie that contains information about a movie. The class has th
ID: 3699376 • Letter: C
Question
Consider a class Movie that contains information about a movie. The class has the following attributes:
The movie name
The MPAA rating (for example, G, PG, PG-13, R)
The number of people that have rated this movie as a 1 (Terrible)
The number of people that have rated this movie as a 2 (Bad)
The number of people that have rated this movie as a 3 (OK)
The number of people that have rated this movie as a 4 (Good)
The number of people that have rated this movie as a 5 (Great)
Write a program the definition of the class called Movie as following:
Create members and member functions for the movie name and MPAA rating.
Design a member function addRating( ), that takes an integer as an input parameter. The function should verify that the parameter is a number between 1 and 5, and if so, increment the number of people rating the movie that match the input parameter. For example, if 3 is the input parameter, then the number of people that rated the movie as a 3 should be incremented by 1.
Design another member function, getAverage( ), that returns the average value for all of the movie ratings.
Develop a constructor that allows the programmer to create the object with a specified name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor.
In a main program, create at least two movie objects, add at least five ratings for each movie, and output the movie name, MPAA rating, and average rating for each movie object.
Use an array to store the movie ratings instead of separate variables. All changes should be internal to the class so the main function to test the class should run identically with either the old Movie class or the new Movie class that uses an array member variable.
In the main program, instead of creating separate variables for each Movie object, an array of at least four Movie objects is created with any sample data. Loop through the array and output the name, MPAA rating, and average rating for each of the four movies. (C++)
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
class Movie
{
private:
int IntRate[5];
int ratings;
string movie;
string UserRate[5] = {"Terrible","Bad","Okay","Good","Great"};
string MPAA;
public:
Movie(string title,string MpaaRating)
{
movie = title;
MPAA = MpaaRating;
ratings = 0;
for(int i = 0; i < 5; i++)
IntRate[i] = 0;
}
void SetMPAA(string MpaaRating)
{
MPAA = MpaaRating;
}
float getAverage()
{
int total = 0;
ratings = 0;
for(int i = 0; i < 5; i++)
{
ratings += IntRate[i];
total += IntRate[i] * (i+1);
}
if(ratings > 0)
return floorf((total / ratings)*10) / 10;
else
return 0;
}
void addRating(int rating)
{
if(rating >= 1 && rating <= 5)
IntRate[rating-1]++;
}
friend ostream& operator << (ostream &out, Movie &M)
{
out << "Movie: " << M.movie << " ";
out << "MPAA rating: " << M.MPAA << " ";
out << "viewer Rating: " << M.getAverage() << " ";
return out;
}
};
int main()
{
Movie A("A movie","PG");
A.addRating(1);
A.addRating(2);
A.addRating(3);
A.addRating(4);
A.addRating(5);
cout << A << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.