Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with inheritance project for C++ please. If you please make sure code

ID: 665010 • Letter: N

Question

Need help with inheritance project for C++ please. If you please make sure code is accrurate so I do not get confused. Input.txt files are in dropbox below: PLEASE MAKE SURE CODE IS CORRECT AND POST OUTPUT. Most of my questions have been answered incorrectly and with errors. Thanks

https://www.dropbox.com/sh/36sxpfkktf7rawv/AABiPMaEmnPVSsVlR65Jr7S6a?dl=0

This program will use polymorphism in a simple inheritance tree. An online book & media vendor notices that certain properties are common to everything sold on the site. Every item has:

• A title (string, may contain spaces)

• An author or artist, a person or group credited as creating the work in question (also a string, may contain spaces)

• A wholesale and retail price (both decimal numbers—we’ll use doubles). But everything sold also has some additional properties:

• A Book also has a page count (integer) and shipping weight in pounds (a double)

• A Media object (music or video) has a running time in minutes (double).

• A PhysicalMedia object (CD or DVD) has a shipping weight in pounds (a double)

• A Download object has a size in megabytes (integer, fractions rounded up, must be greater than 0.)

Therefore we’re going to use an inheritance hierarchy to let us capture as much of the common behavior as possible. For example, we note that all media objects have a running time; physical media (CDs or DVDs) also have a shipping weight, downloadable media also have a file size. Rectangles indicate abstract classes that are used to describe categories of items; circles indicate concrete classes that will be instantiated. Your program will implement this hierarchy. The top-level parent class, Item, will have getter and setter methods for the title and Author. Each of these will use the getline() method to read the data into a string, assuming the data is on a line by itself. It will also have getters and setters for the wholesale and retail prices. It will also have a pure virtual method called ReadData() that takes an istream passed by reference as its only parameter and returns a bool based on whether or not it read data successfully. (Implement this function for the Item class; even if an Item can’t be instantiated, its children can call this method as part of their own input methods.) It will expect data in the following order: Title, Author, Wholesale price, Retail price. It will also have a method called WriteData() that takes an ostream passed by reference as its only parameter and returns a bool if the data is written successfully.

Item Media Book Physical Download The Media abstract class is an Item that also has one additional data element, with a getter and setter: the running time. This is stored in minutes (a double, necessary for some recorded tracks). It will also have an abstract GetData() method, which should call the parent’s GetData() method and then read its own data; and a WriteData() method, which again calls the parent’s WriteData() method and then writes its own. The Book class is a non-abstract class inheriting from Item. It has getters and setters for its data, a GetData() method, and a WriteData() method. The PhysicalMedia and DownloadMedia classes are both derived from Media. They have GetData(), WriteData(), and getter and setter methods for their own data. Also, every class in this hierarchy has a const method called WhatAmI(), that returns a string: “Item”, “Media”, “Book”, “PhysicalMedia”, or “DownloadMedia”.

Your main program will process a single input file containing order information. There are no more than 100 items in the order. Your program will use an array of Item pointers, instantiating each with the correct type as information is read. Output is very simple: iterate through the array of pointers, printing out the titles of all the media of each type. Then provide a summary of the wholesale and retail totals: Output should be formatted as shown, with columns and decimal places right-aligned and trailing zeros displayed. (Align the data into columns; the exact width of the columns doesn’t matter as long as it’s consistent and looks neat.)

Explanation / Answer

CPP Program:

#include <iostream>

#include <string>

#include <fstream>

#include <iomanip>

using namespace std;

class ItemClass

{

private:

    string title, authorArtist;

    double wholesale_Price, retail_Price;

public:

    ItemClass()

    {

         this->title="";

       this->authorArtist="";

         this->wholesale_Price=0;

         this->retail_Price=0;

    }

    void a_setTitle(string title)

    {

         this->title=title;

    }

    void a_setAuthorArtist(string author)

    {

         this->authorArtist=authorArtist;

    }

    void setwholesale_Price(double wholesale_Price)

    {

         this->wholesale_Price=wholesale_Price;

    }

    void setretail_Price(double retail_Price)

    {

         this->retail_Price=retail_Price;

    }

    string a_getTitle()

    {

         return this->title;

    }

    string a_getAuthorArtist()

    {

         return this->authorArtist;

    }

    double getwholesale_Price()

    {

         return this->wholesale_Price;

    }

    double getretail_Price()

    {

         return this->retail_Price;

    }

   virtual bool a_ReadData(ifstream &a_in)=0;

   virtual bool a_WriteData(ofstream &a_out)=0;

   const string a_WhoAmI()

    {

         string st12="This is Items class.";

       return st12;

    }

};

class MediaClass:public ItemClass

{

private:

    double runningTime12;

public:

   MediaClass():ItemClass()

    {

         runningTime12=0;

    }

    void a_setRunTime12(double runtime)

    {

        runningTime12=runtime;

    }

    double a_getRunTime12()

    {

         return runningTime12;

    }

    virtual bool a_ReadData(ifstream &in)=0;

    virtual bool a_WriteData(ofstream &in)=0;

    const string a_WhoAmI()

    {      string st12="This is MediaClass class.";

         return st12;

    }

};

class BookClass:public ItemClass

{

private:

    int a_pageCount;

    double a_shippingWeight;

public:

    BookClass():ItemClass()

    {

         a_pageCount=0;

         a_shippingWeight=0;

    }

    void seta_pageCount(int count)

    {

         a_pageCount=count;

    }

    double geta_pageCount()

    {

         return a_pageCount;

    }

    void a_setWeight(double weight)

    {

         a_shippingWeight=weight;

    }

    double a_getWeight()

    {

         return a_shippingWeight;

    }

    const string a_WhoAmI()

    {

        string st12="This is BookClass class.";

         return st12;

    }

    bool a_ReadData(ifstream &in12)

    {

         string value12="";

         double v;

         getline(in12, value12);     

         this->a_setTitle(value12);

         getline(in12, value12);     

         this->a_setAuthorArtist(value12);

         getline(in12, value12);

         this->setwholesale_Price(stod(value12));

         getline(in12, value12);

         this->setretail_Price(stod(value12));

         getline(in12, value12);

         this->seta_pageCount(stoi(value12));

         getline(in12, value12);

         this->a_setWeight(stod(value12));

        return true;

    }

    bool a_WriteData(ofstream &a_out)

    {

         a_out<<this->a_WhoAmI()<<endl;

       a_out<<setprecision(2)<<fixed;

         a_out<<this->a_getTitle()<<setw(10)<<this->a_getAuthorArtist()

             <<setw(10)<<this->getwholesale_Price()

             <<setw(10)<<this->getretail_Price()

             <<setw(10)<<this->geta_pageCount()

             <<setw(10)<<this->a_getWeight()<<endl;

         return true;

    }

};

class PhysicalMediaClass:public MediaClass

{

private:

    double a_shippingWeight;

public:

    PhysicalMediaClass():MediaClass()

    {

         a_shippingWeight=0;

    }

    void a_setWeight(double weight)

    {

         a_shippingWeight=weight;

    }

    double a_getWeight()

    {

         return a_shippingWeight;

    }

    const string a_WhoAmI()

    {

         string s="This is Physical Media class.";

         return s;

    }

    bool a_WriteData(ofstream &a_out)

    {

         a_out<<this->a_WhoAmI()<<endl;

         a_out<<setprecision(2)<<fixed;

         a_out<<this->a_getTitle()<<setw(10)<<this->a_getAuthorArtist()

             <<setw(10)<<this->getwholesale_Price()

             <<setw(10)<<this->getretail_Price()

             <<setw(10)<<this->a_getRunTime12()

             <<setw(10)<<this->a_getWeight()<<endl;

         return true;   

    }

    bool a_ReadData(ifstream &in12)

    {

         string value12;

         getline(in12, value12);

         this->a_setTitle(value12);

         getline(in12, value12);

         this->a_setAuthorArtist(value12);

         getline(in12, value12);

         this->setwholesale_Price(stod(value12));

         getline(in12, value12);

         this->setretail_Price(stod(value12));

         getline(in12, value12);

         this->a_setRunTime12(stod(value12));

         getline(in12, value12);

         this->a_setWeight(stod(value12));

         return true;

    }

};

class DownloadClass:public MediaClass

{

private:

    int a_size;

public:

    DownloadClass():MediaClass()

    {

         this->a_size=0;

    }

    void setDownloadSize(int size12)

    {

         this->a_size=size12;

    }

    int getDownloadSize()

    {

         return this->a_size;

    }

    const string a_WhoAmI()

    {

         string st12="This is Download class.";

         return st12;

    }

    bool a_WriteData(ofstream &out)

    {

         out<<this->a_WhoAmI()<<endl;

         out<<setprecision(2)<<fixed;

         out<<this->a_getTitle()<<setw(10)<<this->a_getAuthorArtist()

             <<setw(10)<<this->getwholesale_Price()

             <<setw(10)<<this->getretail_Price()

             <<setw(10)<<this->a_getRunTime12()

             <<setw(10)<<this->getDownloadSize()<<endl;

         return true;

    }

    bool a_ReadData(ifstream &in12)

    {

         string value12;

         getline(in12, value12);

         this->a_setTitle(value12);

         getline(in12, value12);

         this->a_setAuthorArtist(value12);

         getline(in12, value12);

         this->setwholesale_Price(stod(value12));

         getline(in12, value12);

         this->setretail_Price(stod(value12));

         getline(in12, value12);

         this->a_setRunTime12(stod(value12));

         getline(in12, value12);

         this->setDownloadSize(stoi(value12));

         return true;

    }

};

//main function

int main()

{

    ifstream myfile12("Input.txt");

    ofstream outfile12("Output.txt");

    int size =100;

    char ch_d;

    ItemClass *items;

    int counter=0;

    items=(ItemClass *)malloc(sizeof(ItemClass *));

    if(myfile12)

    {

         while(!myfile12.eof())

         {          

             myfile12>>ch_d;

             if(ch_d=='B')

             {

                 items= new BookClass;

                 items[counter].a_ReadData(myfile12);

                 counter++;

             }

             if(ch_d=='P')

             {

                 items = new PhysicalMediaClass;

                 items[counter].a_ReadData(myfile12);

                 counter++;

             }

             if(ch_d=='D')

             {

                 items = new DownloadClass;

                 items[counter].a_ReadData(myfile12);

                 counter++;

             }

         }

    }

    myfile12.close();

    for(int i =0;i< counter; i++)

    {

         items[counter].a_WriteData(outfile12);

    }

    outfile12.close();

    system("Pause");

    return 0;

}