MCC - CIS162AB - C++ Level I
P13 Separate Files - Extra Credit - 20 points
(turn in source code files, sample output, and P13.txt)
   cpp.gif

The purpose of this C++ programming project is to familiarize students with compiling projects with C++ code in multiple and separate files. In this project we take the class definitions and code in P12 Inheritance and break it up into separate files, which are call the interface, implemetation, and application. These are step-by-step instructions. Most of this assignment will be completed by copy-and-pasting code from the complete P12 assignment, and adding some additional commands to each file.

Each class definition is stored in a separate file (interface file), and the function definitions for each class are stored in another file (implementation file). The application that uses the classes is stored in another file (application file).

Be sure to review the assigned reading and lecture before beginning this project in order to better understand the procedures and preprocessor directives.

  1. Create Project P13. Three sources files and two header files will be added. In Visual C++ we will end up with the following stucture:
    P13 project
       Header files
          sales_info.h       (interface)
          sales_person.h     (interface)
       Source files
          sales.cpp          (application)
          sales_info.cpp     (implementation)
          sales_person.cpp   (implementation)
       output.txt
       P13.txt
    
  2. Use the menu option Project -> Add New Item to add the text file P13.txt to the project. Do not enter anything into this file. The program will be writing some sample output to it.

  3. Use the menu option Project -> Add New Item to add the text file output.txt to the project.

  4. Create the first interface file (Header: sales_person.h).
    The interface file only contains the class definition and is stored in a header file.
  5. //P13 SalesPerson  -  Juan Marquez   
    //Interface file - sales_person.h  - class definition
    
    #ifndef SALES_PERSON_H
    #define SALES_PERSON_H
    
    #include <string>  // string class
    using namespace std;
    
    
    class SalesPerson   //copy-and-paste your class definition over this outline.
    {
    protected:
    
    public:
    
    };                  //copy-and-paste your class definition over this outline.
    
    
    #endif   // End of SALES_PERSON_H
    

  6. Create the second interface file (Header: sales_info.h).
    The interface file only contains the class definition and is stored in a header file.
  7. //P13  SalesInfo -  Juan Marquez   TR 1:00pm
    //Interface file - sales_info.h  - function prototypes
    
    #ifndef SALES_INFO_H
    #define SALES_INFO_H
    
    #include "sales_person.h"   //SalesPerson definition required for inheritance
    
    
    class SalesInfo : public SalesPerson   //copy-and-paste your class definition over this outline.
    {
    private:
    
    public:
    
    };                    //copy-and-paste your class definition over this outline.
    
    
    #endif  // End of SALES_INFO_H  
    

  8. Create the application file (sales.cpp).
    This C++ source file contains main(), which uses the classes defined above.
  9. //P13 Separate Files -  Juan Marquez   
    /*
       This program is a driver to test SalesInfo, which inherits SalesPerson.
       It is used to create 2 objects, which test the constructors and accessors.
       The objects created are saved to p13.txt
    */
    #include <fstream>    // file processing
    #include <iostream>   // cout and cin
    #include <iomanip>    // setw
    #include <string>     // string class
    
    #include "sales_person.h"  // SalesPerson
    #include "sales_info.h"    // SalesInfo also includes SalesPerson,
                               // that is why #defines were added. 
    
    using namespace std;
    
    
    //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("P13.txt");
        if (fileOut.fail())
        {
            cout << "Error opening output file for sales information.\n"
                 << "Exiting program \n\n";
            return;
        }
    
        cout << "\nP13     Juan Marquez    \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 - sales.cpp
    
  10. Create the first implementation file (sales_person.cpp).
    This file contains the actual function defintions for the class and is stored in a C++ source file.
  11. //P13 SalesPerson  -  Juan Marquez  
    //Implementation file - sales_person.cpp - function defintions
    
    
    #include <iostream>        // cout and cin
    #include "sales_person.h"  // SalesPerson
    
    using namespace std;
    
    
    //Functions for class SalesPerson include:
    
    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()
    
    // End of implementation file  sales_person.cpp
    

  12. Create the second implementation file (sales_info.cpp).
    This file contains the actual function defintions for the class and is stored in a C++ source file.
  13. //P13 SalesInfo    Juan Marquez   
    //Implementation file - sales_info.cpp - function defintions
    
    #include <iostream>      // cout and cin
    #include "sales_info.h"  // SalesInfo
    
    using namespace std;
    
    
    //Functions for class SalesInfo include: 
    
    SalesInfo::SalesInfo() : SalesPerson() 
    SalesInfo::SalesInfo(int id, string fn, string ln, 
                  double rt, int qt) : SalesPerson(id, fn, ln) 
    SalesInfo::~SalesInfo()
    
    void SalesInfo::setRate(double rt)
    void SalesInfo::setQty(int qt)
    
    double SalesInfo::getRate()
    int SalesInfo::getQty()
    
    void SalesInfo::inputRate()
    void SalesInfo::inputQty()
    
    // End of implementation file  sales_info.cpp
    

  14. Select Build -> Build Solution to actually build the executable.
  15. Now the program can be executed
    From the menu bar, select Debug -> Start Without Debugging.
    P13     Juan Marquez   
    
    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
    
  16. Double click on P13.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 
    
  17. Submit the following files:.


Revised: 08/15/2006 - www.mc.maricopa.edu/~marquez/cis162ab/p13_separate_net.html