Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment Submission Submit the source code file. Submit the P12.txt; this file should contain content generated (outputted) by the program) The purpose of this C++

Assignment Submission

Submit the source code file.

Submit the P12.txt; this file should contain content generated (outputted) by the program)

The purpose of this C++ programming project is to provide more practice in defining classes and to familiarize students with inheritance. In this project we create a new class that inherits the SalesPerson class. The base class will be SalesPerson, and it will be used to get and validate the id, firstname, lastname. The derived class, SalesInfo, will inherit SalesPerson, and add the functionally of getting and validating the bonus rate and quantity.

UML - Inheritance Diagram: Use the suggested variable and function names so the code provided will work without any changes. Symbol definitions: - private + public # protected

SalesPerson <-- Inherits -- SalesInfo
# int salesPersonId # string firstName # string lastName - - double rate - int qty
+ SalesPerson ( ) //default constructor doesn't have parameters + SalesPerson(int id, string fn, string ln) //overloaded constructor; //parameter names in overloaded constructors should be different //from the class variable names, because the parameter values //are going to be assigned to the class variables. + ~SalesPerson ( ) //destructor //Set accessors do NOT return a value (void) because they are used //to assign a value to a private variable. The value to assign is //passed through the parameter. + void setSalesPersonId(int id) + void setFirstName(string fn) + void setLastName(string ln) //Get accessors are used to return the value stored in a private //variable, so a parameter is not passed. + int getSalesPersonId( ) + string getFirstName( ) + string getLastName( ) //Input accessors prompt for, get, and store a value in a private //variable. A value is not returned and a parameter is not passed. + void inputSalesPersonId( ) + void inputFirstName( ) + void inputLastName( ) - + SalesInfo ( ) //default constructor doesn't have parameters + SalesInfo (int id, string fn, string ln, double rt, int qt) //overloaded //Since SalesInfo inherits SalesPerson, the overloaded constructor //must also list the variables from the base class. The values //will be passed to the constructor in the base class. + ~SalesInfo ( ) //destructor //Set accessors do NOT return a value (void) because they are used //to assign a value to a private variable. The value to assign is //passed through the parameter. + void setRate(double rt) + void setQty(int qt) //Get accessors are used to return the value stored in a private //variable, so a parameter is not passed. + double getRate( ) + int getQty( ) //Input accessors prompt for, get, and store a value in a private //variable. A value is not returned and a parameter is not passed. + void inputRate( ) + void inputQty( )

Create Project P12.

Add the text file P12.txt to the project. Do not enter anything into this file.

Add the source file P12.cpp to the project.

Copy-and-paste the following lines of code into P12.cpp.

//P12 Inheritance - Your Name /* This program is a driver to test SalesInfo class, which inherits SalesPerson. It is used to create 2 objects, which test the constructors and accessors. The objects created are saved to P12.txt */ #include  // file processing #include  // cout and cin #include  // setw #include  // string class using namespace std; 

Copy-and-paste the class definition for SalesPerson from the top of your P11 project. To allow for inheritance, change private: to protected:. The outline is listed below.

class SalesPerson { protected: //Paste from P11.cpp private public: //Paste from P11.cpp public }; 

After the SalesPerson class definition add the SalesInfo class definition with the required inheritance code. This code needs to be developed by the student. Use the UML provided above and the outline listed below.

class SalesInfo : public SalesPerson  { private: double rate; int qty; public: //Populate based on the SalesInfo UML diagram }; 

After the SalesInfo class definition, Copy-and-paste the following application code to use as the driver to test the inheritance.

//This function saves sales info to a file or displays to screen (cout) void outputSalesInfo(ostream& target, SalesInfo& salesInfoObj); int main() { cout << "P12 - your name "; //Open the file for output ofstream outFile; outFile.open("P12.txt"); if (outFile.fail()) { cout << "Error opening output file for sales information. " << "Exiting program "; return 0; } cout << " P12 Your Name "; //1001 Joe Smith 5.00, 25 - use default constructors and input functions SalesInfo salesInfoObj; //Save the sales data as a record to the file and display on the screen. outputSalesInfo(outFile, salesInfoObj); //1002 Larry Jones - use set methods to change values. salesInfoObj.setSalesPersonId(1002); salesInfoObj.setFirstName("Larry"); salesInfoObj.setLastName("Jones"); salesInfoObj.setRate(10.00); salesInfoObj.setQty(50); //Save the sales data as a record to the file and display on the screen.. outputSalesInfo(outFile, salesInfoObj); //1003 Paul Sailor - use overloaded constructors SalesInfo salesInfoObj2(1003, "Paul", "Sailor", 15.00, 150); //Save the sales data as a record to the file and display on the screen.. outputSalesInfo(outFile, salesInfoObj2); // Close the output file and exit program outFile.close(); return 0; }//end of main //save the order information to a file or display on screen void outputSalesInfo(ostream& target, SalesInfo& salesInfoObj) { //declare local variables int salesPersonId; string lastName, firstName; double rate; int qty; //set the precision for rate target.setf(ios::fixed); target.setf(ios::showpoint); target.precision(2); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //Have the class return the private values to the local variables. //Then store them in the file. salesPersonId = salesInfoObj.getSalesPersonId(); firstName = salesInfoObj.getFirstName(); lastName = salesInfoObj.getLastName(); rate = salesInfoObj.getRate(); qty = salesInfoObj.getQty(); cout << " SalesPerson's Information Saved! "; cout.setf(ios::left); cout << setw(6) << salesPersonId << setw(18) << firstName << setw(18) << lastName; cout.unsetf(ios::left); cout << setw(6) << rate; cout << setw(4) << qty; cout << endl; target.setf(ios::left); target << setw(6) << salesPersonId << setw(18) << firstName << setw(18) << lastName; target.unsetf(ios::left); target << setw(6) << rate; target << setw(4) << qty; target << endl; return; } //end of application code 

After the application code, Copy-and-paste the function definitions for SalesPerson from the bottom of your P11. The functions should include the following.

 SalesPerson::SalesPerson() SalesPerson::SalesPerson(int id, string fn, string ln) SalesPerson::~SalesPerson() void SalesPerson::setSalesPersonId(int id) void SalesPerson::setFirstName(string fn) void SalesPerson::setLastName(string ln) int SalesPerson::getSalesPersonId() string SalesPerson::getFirstName() string SalesPerson::getLastName() void SalesPerson::inputSalesPersonId() void SalesPerson::inputFirstName() void SalesPerson::inputLastName() 

Scroll down to the end of the file and copy-and-paste the following function definitions for SalesInfo. All of the function headers are provided below, but some of the function bodies need to be defined by the student.

//default constructor- the input functions should be called from here SalesInfo::SalesInfo() : SalesPerson()  //call constructor in base class  { inputRate(); inputQty(); } //overloaded constructor - arguments assigned to members //use base initialize list to call constructor in base class SalesInfo::SalesInfo(int id, string fn, string ln, double rt, int qt) : SalesPerson(id, fn, ln) { rate = rt; qty = qt; } //destructor SalesInfo::~SalesInfo() { cout << endl << "SalesInfo Object going out of scope. Id = " << salesPersonId; } //Student needs to complete the next 4 functions. //Accessors to set values in private variables void SalesInfo::setRate(double rt) { } void SalesInfo::setQty(int qt) { } //Accessors to return values in private variables double SalesInfo::getRate() { } int SalesInfo::getQty() { } void SalesInfo::inputRate() {//Normally set and input functions would include validation and would throw an exception if an error was found. do { cout << "Enter a bonus rate between $5.00 and $10: "; cin >> rate; if (rate < 5.0 || rate > 10.00) cout << "Error: The rate must be between $5.00 and $10.00. " << "Try again... "; } while (rate < 5.0 || rate > 10.00); return; } void SalesInfo::inputQty() { do { cout << "Enter a quantity between 0 and 200: "; cin >> qty; if (qty < 0 || qty > 200) cout << "Error: The quantity must be between 0 and 200. " << " Try again... "; }while (qty < 0 || qty > 200); return; } 

Compile and Build the project. Be sure to correct all errors reported by the compiler.

Execute the program to generate output. If correct, then proceed to the next step.

P12 Your Name Enter the SalesPerson ID (1000 - 9999): 1001 Enter First Name without spaces: Joe Enter Last Name without spaces: Smith Enter a bonus rate between $5.00 and $10: 5.00 Enter a quantity between 0 and 200: 25 SalesPerson's Information Saved! 1001 Joe Smith 5.00 25 SalesPerson's Information Saved! 1002 Larry Jones 10.00 50 SalesPerson's Information Saved! 1003 Paul Sailor 15.00 150 SalesInfo Object going out of scope. Id = 1003 SalesPerson Object going out of scope. Id = 1003 SalesInfo Object going out of scope. Id = 1002 SalesPerson Object going out of scope. Id = 1002 Press any key to continue

Double click on P12.txt to see the data saved to the file. A message may be displayed informing you that the file has been modified. Click on yes to have the updated file reloaded into memory.

1001 Joe Smith 5.00 25 1002 Larry Jones 10.00 50 1003 Paul Sailor 15.00 150

Study the two class definitions and the inheritance. Be sure you understand how main is able to use SalesInfo to access members defined in two separate classes (inheritance).

Submit P12.cpp and P12.txt.

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

Step: 3

blur-text-image

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

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

Myrna Talefiero is this organizations president elect.

Answered: 1 week ago

Question

How many owner operators are in the industry?

Answered: 1 week ago