Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab will test your ability to create and use base classes and derived classes. This program records purchases from Amazon and calculates the cost.

This lab will test your ability to create and use base classes and derived classes.

This program records purchases from Amazon and calculates the cost. There are three types of items that a user can purchase - a book, a DVD, or a digital music download. For each item there is a title, a list of authors, and a price. For each item you need to get information and you need to calculate the costs. You will create four classes as follows:

The Generic Item

This contains everything that is common to all types of purchases.

By default generic item will create an object with the title set to an empty string, the list of authors to a nullptr, the number of authors to 0, and the price to $0.00.

The generic item can also be created with the following data - the title, the number of authors, and the price. The constructor will prompt the user for the authors. Since the number of authors is not known before runtime, dynamic memory allocation will have to be used to get the list of authors.

When asked to calculate the cost, generic item will simply return the price.

When asked to get information, generic item will stream out the title, a list of authors, and the price to 2 decimal places.

Be sure to cleanup on object destruction.

Book.

This contains information specific to a book purchase.

When asked to calculate the cost, the book will add a 10% penalty to the price for using paper and 13% of the price for the harmonized sales tax.

When asked to get information, book will stream out information from the generic item that it is derived from. It will also stream out one line specifying: "There is a 10% penalty for using paper."

DVD.

This contains information specific to a DVD purchase.

When asked to calculate the cost, the DVD will add a 5% penalty to the price for using plastic and 13% of the price for the harmonized sales tax.

When asked to get information, the DVD will stream out information from the generic item that it is derived from. It will also stream out one line specifying: "There is a 5% penalty for using plastic."

Digital Music Download.

This contains information specific to the purchase of digital music download.

When asked to calculate the cost, the digital music download will add 13% of the price for the harmonized sales tax.

When asked to get information, the DVD will stream out information from the generic item that it is derived from.

Sample Run

You have been given the main test program: AmazonMain.cpp. You have to create the header and CPP files for all the classes.

A sample run is as follows:

 For testing purchases, please order one book, one DVD, and one digital download. FOR THE BOOK Enter the title: The Book Enter the price: 11.66 Enter the number of authors: 3 Enter the name of author 1 : Book Author1 Enter the name of author 2 : Book Author2 Enter the name of author 3 : Book Author3 FOR THE DVD Enter the title: The DVD Enter the price: 34.99 Enter the number of authors: 2 Enter the name of author 1 : DVD Author1 Enter the name of author 2 : DVD Author2 FOR THE DIGITAL MUSIC DOWNLOAD Enter the title: Best Hits of Smoopey Enter the price: 21.49 Enter the number of authors: 2 Enter the name of author 1 : Smoopey1 Enter the name of author 2 : Smoopey2 Title: The Book Authors: Book Author1, Book Author2, Book Author3. Price: $11.66 There is a 10% penalty for using paper. The real cost is $14.34 Title: The DVD Authors: DVD Author1, DVD Author2. Price: $34.99 There is a 5% penalty for using plastic. The real cost is $41.29 Title: Best Hits of Smoopey Authors: Smoopey1, Smoopey2. Price: $21.49 The real cost is $24.28 The total cost is $79.91 

To assist you with this lab, you might want to look at the following sample code that keeps track of marks for students in three different ways. The default way is by percentages. The other two ways is to have letter grades (A,B,C,D,F) or to have number grades (0-5): Student.h, Student.cpp, LetterStudent.h, LetterStudent.cpp, NumberStudent.h, NumberStudent.cpp and StudentMain.cpp.

Be sure to document your code with the file name, your name and student number. Add comments throughout the code where necessary.

Sample codes:

//Student.h - class declaration for a student #ifndef _STUDENT_H_ #define _STUDENT_H_ #include  class Student { //these are common to this class and all derived classes std::string name; int number; int* marks; int numMarks; protected://for derived classes only std::string GetName() const; int* GetMarks() const; int GetNumMarks() const; public: Student(std::string _name, int _number, const int* _marks, int _numMarks); double GetAverage() const; void display(std::ostream& os) const; ~Student(); }; std::ostream& operator<<(std::ostream& os, Student& student); #endif// _STUDENT_H_

//Student.cpp - function definitions for the student class #include "Student.h" using namespace std; Student::Student(std::string _name, int _number, const int* _marks, int _numMarks) { name = _name; number = _number; numMarks = _numMarks; if (numMarks > 0) { marks = new int[numMarks]; for (int i = 0; i < numMarks; ++i) { marks[i] = _marks[i]; } } else { marks = nullptr; } } std::string Student::GetName() const { return name; } int* Student::GetMarks() const { return marks; } int Student::GetNumMarks() const { return numMarks; } double Student::GetAverage() const { int runningTotal = 0; for (int i = 0; i < numMarks; ++i) { runningTotal += marks[i]; } double average = 0.0; if (numMarks > 0) average = (double)runningTotal / numMarks; return average; } void Student::display(std::ostream& os) const { os << endl << endl; os << name << " with student number " << number << " has the following marks:"; for (int i = 0; i < numMarks; ++i) { os << " " << marks[i]; } os << "." << endl; os << name << " has an average of " << GetAverage() << endl; } Student::~Student() { delete[] marks;//no need to test if(marks!=nullptr) marks = nullptr; } std::ostream& operator<<(std::ostream& os, Student& student) { student.display(os); return os; }
//LetterStudent.h - class declaration for a letter student #ifndef _LETTERSTUDENT_H_ #define _LETTERSTUDENT_H_ #include  #include "Student.h" class LetterStudent : public Student { char GetLetterGrade(double mark) const;//utility function used within the LetterStudent class public: LetterStudent(std::string _name, int _number, const int* _marks, int _numMarks); char GetAverage() const; void display(std::ostream& os) const; ~LetterStudent(); }; std::ostream& operator<<(std::ostream& os, LetterStudent& student);//overload the streaming operator << #endif// _LETTERSTUDENT_H_
//LetterStudent.cpp - function definitions for a letter student #include "LetterStudent.h" using namespace std; LetterStudent::LetterStudent(std::string _name, int _number, const int* _marks, int _numMarks) : Student(_name, _number, _marks, _numMarks){ } char LetterStudent::GetLetterGrade(const double mark) const {//private utility function char letterGrade; if (mark >= 80.0) letterGrade = 'A'; else if (mark >= 70.0) letterGrade = 'B'; else if (mark >= 60.0) letterGrade = 'C'; else if (mark >= 50.0) letterGrade = 'D'; else letterGrade = 'F'; return letterGrade; } char LetterStudent::GetAverage() const { double average = Student::GetAverage();//get the average as a percentage char letterAverage = GetLetterGrade(average); return letterAverage; } void LetterStudent::display(std::ostream& os) const { int* marks = Student::GetMarks(); int numMarks = Student::GetNumMarks(); Student::display(os); os << "In terms of letter grades, the marks are:"; for (int i = 0; i < numMarks; ++i) { char letterGrade = GetLetterGrade((double)marks[i]); os << " " << letterGrade; } os << endl; os << Student::GetName() << " is a(n) " << GetAverage() << " student." << endl; } LetterStudent::~LetterStudent() { } std::ostream& operator<<(std::ostream& os, LetterStudent& student) { student.display(os); return os; }

//NumberStudent.h - class declaration for a number student #ifndef _NUMBERSTUDENT_H_ #define _NUMBERSTUDENT_H_ #include  #include "Student.h" class NumberStudent : public Student { int GetNumberGrade(double mark) const;//private utility function public: NumberStudent(std::string _name, int _number, const int* _marks, int _numMarks); double GetAverage() const; void display(std::ostream& os) const; ~NumberStudent(); }; std::ostream& operator<<(std::ostream& os, NumberStudent& student); #endif// _NUMBERSTUDENT_H_

//NumberStudent.cpp - function definitions for a number student #include "NumberStudent.h" using namespace std; NumberStudent::NumberStudent(std::string _name, int _number, const int* _marks, int _numMarks) : Student(_name, _number, _marks, _numMarks) { } int NumberStudent::GetNumberGrade(const double mark) const { int numberGrade; if (mark >= 90.0) numberGrade = 5; else if (mark >= 80.0) numberGrade = 4; else if (mark >= 70.0) numberGrade = 3; else if (mark >= 60.0) numberGrade = 2; else if (mark >= 50.0) numberGrade = 1; else numberGrade = 0; return numberGrade; } double NumberStudent::GetAverage() const { int runningTotal = 0; int* marks = Student::GetMarks(); int numMarks = Student::GetNumMarks(); for (int i = 0; i < numMarks; ++i) { runningTotal += GetNumberGrade((double)marks[i]); } double numberAverage = 0.0; if (numMarks > 0) numberAverage = (double)runningTotal / numMarks; return numberAverage; } void NumberStudent::display(std::ostream& os) const { int* marks = Student::GetMarks(); int numMarks = Student::GetNumMarks(); Student::display(os); os << "In terms of number grades, the marks are:"; for (int i = 0; i < numMarks; ++i) { int numberGrade = GetNumberGrade((double)marks[i]); os << " " << numberGrade; } os << endl; os << Student::GetName() << " has an average of " << GetAverage() << "." << endl; } NumberStudent::~NumberStudent() { } std::ostream& operator<<(std::ostream& os, NumberStudent& student) { student.display(os); return os; }

//StudentMain.cpp - main function for students #include "Student.h" #include "LetterStudent.h" #include "NumberStudent.h" using namespace std; int main() { int marks1[] = { 55,77,66,62 }; int marks2[] = { 75,72,86,92,55 }; int marks3[] = { 92, 62, 70 }; const int NUM_STUDENT = 3; Student student[NUM_STUDENT] = {//uses percentage //Student(std::string _name, int _number, int* _marks, int _numMarks); {"Brett Lee", 1000123, marks1, 4 }, {"Muttiah Muralitharan", 1000124, marks2, 5 }, {"Lasith Malinga", 1000125, marks3, 3 }, }; int marks4[] = { 97, 92, 94, 85 }; int marks5[] = { 81, 89, 76, 62, 100 }; const int NUM_LETTER_STUDENT = 2; LetterStudent letterStudent[NUM_LETTER_STUDENT] = {//uses a letter grading system (A,B,C,D,F) {"Chris Gayle", 1000126, marks4, 4}, {"Brian Lara", 1000127, marks5, 5}, }; int marks6[] = { 61, 67, 82 }; int marks7[] = { 45, 67, 89, 82, 71 }; int marks8[] = { 53, 45, 67, 77, 71, 68 }; const int NUM_NUMBER_STUDENT = 3; NumberStudent numberStudent[NUM_NUMBER_STUDENT] = {//uses a number grading system (5,4,3,2,1,0) {"Wasim Akram", 1000128, marks6, 3}, {"M S Dhoni", 1000129, marks7, 5}, {"Shoaib Akhtar", 1000130, marks8, 6} }; for (int i = 0; i < NUM_STUDENT; ++i) cout << student[i]; for (int i = 0; i < NUM_LETTER_STUDENT; ++i) cout << letterStudent[i]; for (int i = 0; i < NUM_NUMBER_STUDENT; ++i) cout << numberStudent[i]; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions