MCC - Marquez - CIS162AB - C++ Level I
P11 SalesPerson Class Implementation - 20 points
(submit source code, sample output, and p11.txt)
   cpp.gif

The purpose of this C++ programming project is to familiarize the student with class definitions and usage, as well as with saving data to a file. A driver program to test the student's class interface and implementation is provided. The student must copy-and-paste the source code listed below and then complete the class definition and the functions to implement the class.

UML - Class Diagram:
Symbol definitions:
- private
+ public
# protected

SalesPerson
- int salesPersonId
- string firstName
- string lastName
+ 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( )



Create project P11
Add a text file named output.txt
Add a text file named P11.txt
Add a C++ Source file named P11.cpp
  Copy and paste the source code provided into P11.cpp.
  Complete the required class and function definitions.


 
Sample Output:

P11     Juan Marquez   

Enter Sales Person ID (1000 - 9999): 1001
Enter First Name without spaces:     Joe
Enter Last Name  without spaces:     Smith


Sales Person's Information Saved!
1001  Joe               Smith


Sales Person's Information Saved!
1002  Larry             Jones


Sales Person's Information Saved!
1003  Paul              Sailor

SalesPerson Object going out of scope. Id = 1003

SalesPerson Object going out of scope. Id = 1002

Press any key to continue


P11.txt: - created by program

1001  Joe               Smith             
1002  Larry             Jones             
1003  Paul              Sailor            


Source Code:

Copy-and-paste this source code into P11.cpp.

//P11 SalesPerson - Juan Marquez   
/*
   This program is a driver to test the SalesPerson class.
   It is used to create 2 objects, which test the constructors and accessors.
   The objects created are saved to p11.txt.
*/
#include <fstream>  // file processing
#include <iostream> // cin and cout
#include <iomanip>  // setw
#include <string>   // string class
using namespace std;


//Copy-and-paste your class definition from P10.cpp.
 
//Class definition -  the interface for the class

class SalesPerson
{
private:


public:


};


//Application Starts here (prototypes, main(), and defintions for application)

//saves info to a file or displays to screen (cout) through ostream& parameter
void outputSalesInfo(ostream& target, SalesPerson& salesPersonObj);

//main driver to test SalesPerson class
void main()
{
//Open the file for output; if there are any errors, we need to 
//display an error message.

    ofstream fileOut;
    fileOut.open("P11.txt");
    if (fileOut.fail())
    {
        cout << "Error opening output file for sales information.\n"
             << "Exiting program \n\n";
        return;
    }

//Change instructor's name to your name throughout 
    cout << "\nP11     Juan Marquez   \n\n";


//1001 Joe Smith - use default constructor and input functions
    SalesPerson salesPersonObj;

//Save the validated sales info data as a record to the file.
    outputSalesInfo(fileOut, salesPersonObj);
//display the record on the screen
    outputSalesInfo(cout, salesPersonObj);
  


//1002  Larry Jones - use set functions to change values.
    salesPersonObj.setSalesPersonId(1002);
    salesPersonObj.setFirstName("Larry");
    salesPersonObj.setLastName("Jones");

//Save the sales info data as a record to the file.
    outputSalesInfo(fileOut, salesPersonObj);
//display the record on the screen
    outputSalesInfo(cout, salesPersonObj);



//1003 Paul Sailor - use overloaded constructor 
    SalesPerson salesPersonObj2(1003, "Paul", "Sailor");

//Save the sales info data as a record to the file.
    outputSalesInfo(fileOut, salesPersonObj2);
//display the record on the screen
    outputSalesInfo(cout, salesPersonObj2);



// Close the output file and exit program
    fileOut.close();
    return;
}//end of main


//save the order information to a file or display to screen.
//target can be either cout or fileOut because ofstream inherits ostream.
//ofstream objects are also ostream objects.
//also, must be ostream because cout is already declared as an ostream.

void outputSalesInfo(ostream& target, SalesPerson& salesPersonObj)
{
//declare local variables
    int    salesPersonId;
    string lastName, firstName;

//Have the class return the private values to the local variables.
//Then store them in the file.
    salesPersonId = salesPersonObj.getSalesPersonId();
    firstName     = salesPersonObj.getFirstName();
    lastName      = salesPersonObj.getLastName();

    if(target == cout)
        target << "\n\nSales Person's Information Saved! \n";

    target.setf(ios::left);
    target  << setw(6)  << salesPersonId
            << setw(18) << firstName
            << setw(18) << lastName 
            << endl;
    target.unsetf(ios::left);

    return;
}



//Student must complete the 2 constructors, 3 set and 3 get accessors,
//and 3 input function definitions.  A total of 11 functions.

//default constructor
//The default constructor should call the 3 input functions.
 
//overloaded constructor

//Destructor function definition is provided below
SalesPerson::~SalesPerson()
{
    cout << "\nSalesPerson Object going out of scope. Id = " 
         << salesPersonId << endl;
    return;
}

//set accessors

//get accessors

//input accessors


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