Question: This lab will give you more experience with creating and using classes, and add in concepts such as object composition, arrays of Objects, and using
This lab will give you more experience with creating and using classes, and add in concepts such as object composition, arrays of Objects, and using separate header and source files for class definitions.
The following questions below are to be answered based on the codes attached to it. Were going to want to start thinking about organizing our code a bit more though, so start by separating the Movie class definition into a separate header file.
Once the Movie class is created and moved into its own file(s), I recommend testing it thoroughly to make sure all of the parts are working before moving on. You will need to remember to include the header file in any other file where you intend to make reference to the class.
After the Movie class is created, you will create a second new class, MovieDatabase, this one with its own header AND source files (MovieDatabase.h and MovieDatabase.cpp). The MovieDatabase class will have (at least) the following private attributes:
An array, Movies, capable of storing 100 Movie objects.
o This is inefficient, and limited, but well fix it later in the semester.
An integer, movieCount, that counts how many movies have been added into our database.
o While 100 is the MAX size of the array, the ACTUAL size of the filled part of the array is important to keep track of, and this variable serves that purpose.
Besides any accessors and mutators, your MovieDatabase should include methods designed to accomplish the following tasks:
Add a movie to the database
o This function will need to allow the user to enter all the required information to set the values of a movie in the array. You will need to know how many movies are already in the array to determine what index of the array you should edit. For instance, if there are 10 movies already, then the new movie would go to the 11th position, or Movies[10].
Display the current number of movies in the database.
Print out the titles of all of the movies in the database.
In the main, you should declare an instance of your database class, and implement a menu driven program that allows the user to choose to add movies, display the number of movies, or list the titles (by calling the associated methods). Test your program thoroughly, and make sure that it works reliably.
Implement another method in the MovieDatabase class that accepts a movie title as a string argument, and searches the array of movies for a match. You can use a technique called a Linear Search for this start at the first element in the array, and compare the title of that movie to the desired title. If you find a match, call the display function for that movie to print out the data, and then exit the search. If you dont find a match, iterate to the next movie in the array and try again, until you either find a match or run out of movies to check (its not efficient, but it works). If no match is found, you should display an error message indicating such.
In the main, you should update your menu to allow the user to use your new search method.
Below is the code to build on please:
#include
#include
using namespace std;
class Movie
{
private:
string title;
string genre;
int runtime;
double review;
public:
// constructors
Movie(); // default constructor
Movie(string title);
Movie(string title, string genre, int runtime, double review); //constructor that has all the parameter
// Accessors that can be done inline
string getTitle() { return title; };
string getGenre() { return genre; };
int getRuntime() { return runtime; };
double getReview() { return review; };
void setTitle(string title) { this->title = title; };
void setGenre(string genre) { this->genre = genre; };
void setRuntime(int runtime) { this->runtime = runtime; };
void setReview(double review);
void display(); // prints out movies information
};// Class definition end here
//class constructors and mutator for review
Movie::Movie()
{
this->title = "";
this->genre = "";
this->runtime = 0;
this->review = 0.0;
}
Movie::Movie(string title)
{
// initialize the strings for title with given title
// and genre to empty strings (""), and the integer and double for runtime and rating to 0.
this->title = title;
this->genre = "";
this->runtime = 0;
this->review = 0.0;
}
Movie::Movie(string title, string genre, int runtime, double review)
{
this->title = title;
this->genre = genre;
this->runtime = runtime;
// mutator will be usesd here
this->setReview(review);
}
void Movie::setReview(double review)
{
if (review < 0.0 || review > 100.0)
{
cout << "Error!! Review Score is out of range." << endl;
}
else
{
this->review = review;
}
}
void Movie::display()
{
cout<<"*********************************************"< cout<<"*"< cout<<" - Title: " << title << endl; cout<<" - Genre: " << genre << endl; cout<<" - Running Time: " << runtime << " minutes" << endl; cout<<" - Rotten Tomatoes Review: " << review <<"%"< cout<<"*"< cout<<"**********************************************"< cout< } int main() { Movie movie1; // use the default constructor cout<<"Movie - 1 Default Constructor"< cout< cout << " Enter a title for movie 1: "; string title; getline(cin, title); movie1.setTitle(title); cout << " Enter the "< string genre; cin >> genre; movie1.setGenre(genre); cout << " Enter the "< int runtime; cin >> runtime; movie1.setRuntime(runtime); cout << " Enter the "< double review; cin >> review; movie1.setReview(review); //objects display() function will be called to print out the information cout << endl; movie1.display(); Movie movie2("Harriet"); cout<<"Movie 2 - Title Only Constructor."< cout< cout << " Enter Harriet's genre: "; cin >> genre; // get user input // set movie genre movie2.setGenre(genre); cout << " Enter Harriet's runtime(in minutes): "; cin >> runtime; // get user input // set running time for movie movie2.setRuntime(runtime); cout << " Enter Harriet's Rotten Tomatoes Review: "; cin >> review; // get user input // set review score for movie, movie2.setReview(review); cout << endl; movie2.display(); Movie movie3("Fatima", "Drama", 113, 62); // call the display function to print it out and shows result cout<<"Movie 3 - All Data Set in Constructor"< cout< movie3.display(); return 0; } //end of program
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
