MCC - Marquez - CIS162AB - C++ Level I
P12 Inheritance - 20 points
(submit source code, sample output, and P12.txt)
   cpp.gif

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.

  1. 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( )

  2. Create Project P12.
  3. Add the text file P12.txt to the project. Do not enter anything into this file.

  4. Add the text file output.txt to the project.

  5. Add the source file P12.cpp to the project.
  6. Copy-and-paste the following lines of code into P12.cpp.
  7. //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;
    
    
    
  8. Copy-and-paste the class definition for SalesPerson from the top of your P11.
    To allow for inheritance, change private: to protected:. The outline is listed below.
  9. 
    class SalesPerson
    {
    protected:
    
    
    public:
    
    
    };
    
    
    
  10. 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.
  11. class SalesInfo : public SalesPerson 
    {
    private:
        double rate;
        int    qty;
    
    public:
    
    
    };
    
    
  12. 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);
    
    
    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
    
    
  13. After the application code, Copy-and-paste the function definitions for SalesPerson from the bottom of your P11. The functions should include the following.
  14.      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()
    
    
    
  15. 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.
  16. 
    //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;
    }
    

  17. Compile and Build the project. Be sure to correct all errors reported by the compiler.
  18. Execute the program to generate output. If correct, copy-and-paste into output.txt.
    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
    
  19. 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
    
  20. 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).

  21. Submit P12.cpp, P12.txt, and output.txt.


Revised: 11/09/2009 - www.mc.maricopa.edu/~marquez/cis162ab/p12_inheritance.html