MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use prope
ID: 3817079 • Letter: M
Question
MEDIA INHERITANCE - C++
You will create an inheritance set of classes. Use proper rules for data types, class definitions, and inheritance (check input/output of data below). You will create a super media class that will define a general form of media. A media properties will consist of the name and price. The media should be able to construct itself out of the arguments sent to it, virtually print both data fields labeled (price to two decimal places) and overload the < operator to compare two medias by comparing their name field.
You will then create two subclasses of media for a movie and a book. A movie "is a" media, but also has a rating. It also should construct itself by initializing all data fields, but use the super media constructor to initialize the inherited data fields. It will also override the media print function. In the new version, it should print that it is a movie, then call the super print function to print the name and price, then print the new labeled rating data field itself.
A book "is a" media, but also has an author and will be set up similar to a movie. It also should override the media print function. In the new version, it should print that it is a book, then call the super print function to print the name and price, then print the new labeled author data field itself.
You will then create a main class that will declare an array of three super media pointers (so you can combine movies and books together in the array). Then using a loop, prompt and input all the data fields (asking the user what kind of media for what added data to input). At the end of the loop, construct the next (proper type) object and assign it into the array. Put a blank line between prompts for the input objects.
When the input loop is over, call a function in the main program that will selection sort the array of media pointers. Use the overloaded < operator to perform the sort on the name field of the calling object.
In a separate loop, call the print function (with dynamic binding!). Put a blank line between output objects.
Run your program with the data below and create the output as shown below. Document all files with at least 4 lines of comments at the top and at least 5 comments throughout the code for all of the not easily understandable lines of code.
MEDIA INHERITANCE - DATA
Input:
Enter name: Planet of the Apes
Enter price: 8.90
Is media a book(b) or movie(m): m
Enter rating: G
Enter name: Back to the Future
Enter price: 13.90
Is media a book(b) or movie(m): m
Enter rating: PG
Explanation / Answer
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
// Base class
class Media {
public:
Media(string n, double h)
{
name = n;
price = h;
}
void seName(string w) {
name = w;
}
void setPrice(double h) {
price = h;
}
virtual void print()
{
cout << "name:" << name << endl;
cout << "price:" << price << endl;
}
int operator<(const Media& m) {
// Meida box;
return m.name.compare(this->name);
}
protected:
string name;
double price;
};
// Derived class
class Movie: public Media {
public:
Movie(string n, double p, string r): Media(n,p)
{
// super(n,h);
rating = r;
}
void setRating(string r)
{
rating = r;
}
string getRating() {
return rating;
}
virtual void print()
{
cout << "It is a movie" << endl;
Media::print();
cout << "rating:" << rating<<endl;
}
private:
string rating;
};
// Derived class
class Book: public Media {
public:
Book(string n, double p,string a): Media(n,p)
{
author = a;
}
void setAuthor(string a)
{
author = a;
}
string getAuthor() {
return author;
}
void print()
{
cout << "It is a Book" << endl;
Media::print();
cout << "Author:" << author<<endl;
}
private:
string author;
};
int main(void) {
Media* a[3];
cout << "Input: " <<endl;
for(int i=0;i<3;i++)
{
string name;
cout << "Enter name : ";
cin >> name;
// cout << endl;
double price;
cout << "Enter price: ";
cin >> price;
cout << "Is media a book(b) or a movie(m):";
char c;
cin >> c;
if(c=='b')
{
string author;
cout << "Enter author name:";
cin >> author;
a[i] = new Book(name,price,author);
}
else if(c=='m')
{
string rating;
cout << "Enter rating of the movie:";
cin >> rating;
a[i] = new Movie(name,price,rating);
}
else{
cout << "Invalid Input";
}
cout <<endl;
}
Media *iTemp;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
// for descending sort change '<' with '>'
if (a[i] < a[j])
{
iTemp = a[i];
a[i] = a[j];
a[j] = iTemp;
}
}
}
for(int i=0;i<3;i++)
{
a[i]->print();
cout <<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.