Need help with inheritance project for C++ please. If you please make sure code
ID: 664933 • 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:
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
Program code:
// ItemsInheritance-Demo.cpp
// ItemsInheritance-Demo.cpp
//Header files
#include "stdafx.h" //optional
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
//item class
class Item
{
private:
string title, authorArtist;
double wholesale, retail;
public:
Item()
{
this->title="";
this->authorArtist="";
this->wholesale=0;
this->retail=0;
}
bool WriteData(ofstream &out)
{
out<<setprecision(2)<<fixed;
out<<this->getTitle()<<setw(10)<<this->getAuthorArtist()<<setw(10)<<this->getWholeSale()<<setw(10) <<this->getRetail()<<endl;
return true;
}
bool ReadData(ifstream &in)
{
string value;
getline(in, value);
this->setTitle(value);
getline(in,value);
this->setAuthorArtist(value);
getline(in, value);
this->setWholeSale(stod(value));
getline(in, value);
this->setRetail(stod(value));
return true;
}
void setTitle(string title)
{
this->title=title;
}
void setAuthorArtist(string author)
{
this->authorArtist=authorArtist;
}
void setWholeSale(double wholesale)
{
this->wholesale=wholesale;
}
void setRetail(double retail)
{
this->retail=retail;
}
string getTitle()
{
return this->title;
}
string getAuthorArtist()
{
return this->authorArtist;
}
double getWholeSale()
{
return this->wholesale;
}
double getRetail()
{
return this->retail;
}
const string WhoAmI()
{
string s="This is Items class.";
return s;
}
};
//Media class
class Media:public Item
{
private:
double runningTime;
public:
Media():Item()
{
runningTime=0;
}
void setRunTime(double runtime)
{
runningTime=runtime;
}
double getRunTime()
{
return runningTime;
}
const string WhoAmI()
{
string s="This is Media class.";
return s;
}
};
//Book class
class Book:public Item
{
private:
int pageCount;
double shippingWeight;
public:
Book():Item()
{
pageCount=0;
shippingWeight=0;
}
void setPageCount(int count)
{
pageCount=count;
}
double getPageCount()
{
return pageCount;
}
void setWeight(double weight)
{
shippingWeight=weight;
}
double getWeight()
{
return shippingWeight;
}
const string WhoAmI()
{
string s="This is Book class.";
return s;
}
};
//PhysicalMedia class
class PhysicalMedia:public Media
{
private:
double shippingWeight;
public:
PhysicalMedia():Media()
{
shippingWeight=0;
}
void setWeight(double weight)
{
shippingWeight=weight;
}
double getWeight()
{
return shippingWeight;
}
const string WhoAmI()
{
string s="This is Physical Media class.";
return s;
}
};
//download class
class Download:public Media
{
private:
int size;
public:
Download():Media()
{
this->size=0;
}
void setDownloadSize(int size)
{
this->size=size;
}
int getDownloadSize()
{
return this->size;
}
const string WhoAmI()
{
string s="This is Download class.";
return s;
}
};
//main function
int main()
{
ifstream myfile("Input.txt");
ofstream outfile("output.txt",ios::app);
int size =100;
string type;
Item *items;
int counter=0;
items=(Item *)malloc(sizeof(Item *));
if(!outfile)
{
cout<<"Out File cann't be opened";
return 0;
}
if(!myfile)
{
cout<<"File cann't be opened";
return 0;
}
else
{
while(!myfile.eof())
{
getline(myfile,type);
if(type=="B")
{
items= new Book();
items->ReadData(myfile);
items->WriteData(outfile);
counter++;
}
if(type=="P")
{
items = new PhysicalMedia();
items->ReadData(myfile);
items->WriteData(outfile);
counter++;
}
if(type=="D")
{
items = new Download();
items->ReadData(myfile);
items->WriteData(outfile);
}
}
}
myfile.close();
outfile.close();
system("Pause");
return 0;
}
Sample Input text file: Input.txt
B
Of All The People I've Known, You're One Of Them
Donald Trumper
6.55
7.95
237
1.5
P
Eminem And Bieber: Together At Last
Eminem & Justin Bieber
10.75
16.95
62.5
0.9
D
Weirdest Moments In Anime (PG Version)
Various
12.75
22.75
92.5
2046
B
Intolerant People Should Be Shot
Hippolyta (Hippo) Critts
10.50
21.95
352
3.4
Sample Output: Text file: Output.txt
Of All The People I've Known, You're One Of Them 6.55 7.95
Eminem And Bieber: Together At Last 10.75 16.95
Weirdest Moments In Anime (PG Version) 12.75 22.75
Intolerant People Should Be Shot 10.50 21.95