P12 Inheritance - 20 points (submit source code, sample output, and P12.txt) |
|
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.
| 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( ) |
//P12 Inheritance - Juan Marquez TR 1:00pm /* 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 <fstream> // file processing #include <iostream> // cout and cin #include <iomanip> // setw #include <string> // string class using namespace std;
class SalesPerson
{
protected:
public:
};
class SalesInfo : public SalesPerson
{
private:
double rate;
int qty;
public:
};
//This function saves sales info to a file or displays to screen (cout)
void outputSalesInfo(ostream& target, SalesInfo& salesInfoObj);
void main()
{
//Open the file for output
ofstream fileOut;
fileOut.open("P12.txt");
if (fileOut.fail())
{
cout << "Error opening output file for sales information.\n"
<< "Exiting program \n\n";
return;
}
cout << "\nP12 Juan Marquez TR 1:00pm \n\n";
//1001 Joe Smith 5.00, 25 - use default constructors and input functions
SalesInfo salesInfoObj;
//Save the validated sales info data as a record to the file.
outputSalesInfo(fileOut, salesInfoObj);
//display the record on the screen
outputSalesInfo(cout, 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 info data as a record to the file.
outputSalesInfo(fileOut, salesInfoObj);
//display the record on the screen
outputSalesInfo(cout, salesInfoObj);
//1003 Paul Sailor - use overloaded constructors
SalesInfo salesInfoObj2(1003, "Paul", "Sailor", 15.00, 150);
//Save the sales info data as a record to the file.
outputSalesInfo(fileOut, salesInfoObj2);
//display the record on the screen
outputSalesInfo(cout, salesInfoObj2);
// Close the output file and exit program
fileOut.close();
return;
}//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);
//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();
if(target == cout)
target << "\n\nSalesPerson's Information Saved! \n";
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
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()
//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 << "SalesInfo Object going out of scope. Id = "
<< salesPersonId << endl;
}
//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...\n\n";
} 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...\n\n";
}while (qty < 0 || qty > 200);
return;
}
P12 Juan Marquez TR 1:00pm 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
1001 Joe Smith 5.00 25 1002 Larry Jones 10.00 50 1003 Paul Sailor 15.00 150