Question

C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show...

C++ program

Correctly complete the following assignment. Follow all directions. The main purpose is to show super and sub class relationships with an array of super media pointers to sub class objects and dynamic binding. The < operator will be overloaded in the super class so all subclasses can use it. The selection sort method will be in the main .cpp file because it will sort the array created in main. The final .cpp file, the three .h header class files, and a separate output .txt text file (copied and pasted from the console screen) should be submitted.

The selection sort function for an array of cstrings from chapter 3 notes is also attached with some hints on how to adjust that function to a selection sort function for an array of super media pointers.

Hint: Good Guides: 5-12b inheritance with super pointer array including virtual function, 5-10 operator overload, attached selection sort function.

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. Submit your three class definitions, main program, and output text file. 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

Enter name: Intensity

Enter price: 7.90

Is media a book(b) or movie(m): b

Enter author: Dean Koontz

Output:

Movie

Name is Back to the Future

Price is 13.90

Rating is PG

Book

Name is Intensity

Price is 7.90

Author is Dean Koontz

Movie

Name is Planet of the Apes

Price is 8.90

Rating is G

SORT.docx

The code for selection sort (for an array of cstrings) is:

void selectsort(char str[][20], int N)

{

int pass, j, min;

char temp[20];

for (pass = 0; pass <= N - 2; pass++) // passes

{

min = pass;

for (j = pass + 1; j < N; j++) // in each pass

if (strcmp(str[min], str[j]) > 0)

min = j;

strcpy(temp, str[min]);

strcpy(str[min], str[pass]);

strcpy(str[pass], temp);

}

    }

The 2-D character array becomes a super media pointer array. Change > to <, remove strcmp and directly use < overloaded operator. Change strcpy to directly use = assignment operator. Compare the media object the array element is pointing at. Swap the contents of the array element, which is the address of the media object.

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

//main.cpp
#include <iostream>
#include "media.h"
#include "book.h"
#include "movie.h"
using namespace std;

void sort(media *[], int);

int main()
{
media *med[3]; //array of media pointers
int i;
char name[30], rating[3], author[30], mediatype;
float price;
//loop to prompt the user for the data and assign the media to the array
for(i = 0; i < 3; i++)
{
cout << "Enter name: ";
cin.getline(name, sizeof(name), '\n');
cout << "Enter price: ";
cin >> price;
cin.ignore(1, '\n');
cout << "Is media a book(b) or a movie(m): ";
cin >> mediatype;
cin.ignore(1, '\n');
if(mediatype == 'b')//if the mediatype is a book do this
{
cout << "Enter author: ";
cin.getline(author, sizeof(author), '\n');
}
else if(mediatype == 'm')//if the mediatype is a movie do this
{
cout << "Enter rating: ";
cin >> rating;
cin.ignore(1, '\n');
}

if(mediatype == 'b')
med[i] = new book(name, price, author);//book
if(mediatype == 'm')
med[i] = new movie(name, price, rating);//movie
cout << endl;
}
sort(med, 3);
//loop to print each media
for(i = 0; i < 3; i++)
{
med[i]->print();
cout << endl;
}
return 0;
}

//function to sort the array of medias
void sort(media *m[], int N)
{
int pass, j, min;
media *temp;
for (pass = 0; pass <= N - 1; pass++) // passes
{
min = pass;
for (j = pass + 1; j < N; j++) // in each pass
if (m[min] < m[j])
min = j;
temp = m[min];
m[min] = m[pass];
m[pass] = temp;
}
}


------------------------------------------------------------------------------------------
//media.h
#ifndef INHERITANCEBOOKANDMOVIE_MEDIA_H
#define INHERITANCEBOOKANDMOVIE_MEDIA_H

#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;

class media
{
protected:
char name[50];
float price;

public:
media()
{
*name = '\0';
price = 0.00;
}
media(char sname[], float p)//media constructor
{
strcpy(name, sname);
price = p;
}

virtual void print()//virtual print of the super media class
{
cout.setf(ios::fixed|ios::showpoint);
cout << "Name is " << name << endl;
cout << "Price is " << setprecision(2) << price << endl;
}

bool operator<(media med)//overload the < operator
{
if(strcmp(name, med.name) > 0)
return true;
else
return false;
}
};

#endif //INHERITANCEBOOKANDMOVIE_MEDIA_H

---------------------------------------------------------------------------------------
//book.h
#ifndef INHERITANCEBOOKANDMOVIE_BOOK_H
#define INHERITANCEBOOKANDMOVIE_BOOK_H

#include<iostream>
#include"media.h"
#include<cstring>
using namespace std;

class book : public media
{
private:
char author[30];
public:
book() : media()
{
strcpy(author, " ");
}
book(char sname[], float p, char auth[]) : media(sname, p)
{
strcpy(author, auth);
}
void print()//print the book data
{
cout << "Book" << endl;
media::print();//call super print to print name and price
cout << "Author is " << author << endl;
}
};

#endif //INHERITANCEBOOKANDMOVIE_BOOK_H
---------------------------------------------------------------------------------------
//movie.h
#ifndef INHERITANCEBOOKANDMOVIE_MOVIE_H
#define INHERITANCEBOOKANDMOVIE_MOVIE_H
#include<iostream>
#include"media.h"
#include<cstring>
using namespace std;

class movie : public media
{
private:
char rating[3];
public:
movie() : media()
{
strcpy(rating, " ");
}
movie(char sname[], float p, char rat[]) : media(sname, p)
{
strcpy(rating, rat);
}
void print()//print the movie data
{
cout << "Movie" << endl;
media::print();//call super print to print the name and price
cout << "Rating is " << rating << endl;
}
}

Add a comment
Know the answer?
Add Answer to:
C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show...
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
  • MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use proper rules for...

    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...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • HELP WITH C++ The following movies have recently come out: 1. Black Panther, PG-13 2. Avengers:...

    HELP WITH C++ The following movies have recently come out: 1. Black Panther, PG-13 2. Avengers: Infinity War, PG-13 3. A Wrinkle In Time, PG 4. Ready Player One, PG-13 5. Red Sparrow, R 6. The Incredibles 2, G To do: 1. Create a Movie class that stores the movie name and the MPAA rating (i.e. R, PG13, etc.). Write appropriate constructors, setters, and getters. 2. In your main function create an array of type Movie that stores the movie...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

  • COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see...

    COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see which professor has the highest rating according to student input. You will create a ProfessorRating class consisting of professor Name and three ratings. The three ratings are used to evaluate easiness, helpfulness, and clarity. The value for each rating is in the range of 1 to 5, with 1 being the lowest and 5 being the highest. Your program should contain the following functionalities:...

  • using this code to complete. it python 3.#Display Program Purpose print("The program will show how much...

    using this code to complete. it python 3.#Display Program Purpose print("The program will show how much money you will make working a job that gives a inputted percentage increase each year") #Declaration and Initialization of Variables userName = "" #Variable to hold user's name currentYear = 0 #Variable to hold current year input birthYear = 0 #Variable to hold birth year input startingSalary = 0 #Variable to hold starting salary input retirementAge = 0 #Variable to hold retirement age input...

  • [C++] Outline: The movie data is read from a file into an array of structs and...

    [C++] Outline: The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.   Requirements: Your...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

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