Question #55472

Create two classes. The first, named Sale, holds data for a sales transaction. Its private data members include the day of the month, amount of the sale, and the salesperson’s ID number. The second class, named Salesperson holds data for a salesperson, and its private data members include each salesperson’s ID number and last name. Each class includes a constructor to which you can pass the field values. Create a friend function named display () that is a friend of both classes and displays the date of sale, the amount, and the salesperson ID and name. Write a short main () demonstration program to test your classes and friend function.
1

Expert's answer

2015-10-13T03:08:15-0400

Answer on Question #55472, Programming / C++

SalesPerson.h


#include <string>
using namespace std;
// forward declaration for friend function
class Sale;
class SalesPerson {
public:
    SalesPerson(int aSalesPersonId, string aLastName);
    friend void display(Sale sale, SalesPerson salesPerson);
private:
    int salesPersonId;
    string lastName;
};


SalesPerson.cpp


#include "SalesPerson.h"
SalesPerson::SalesPerson(int aSalesPersonId, string aLastName) {
    salesPersonId = aSalesPersonId;
    lastName = aLastName;
}


Sale.h


// forward declaration for friend function
class SalesPerson;
class Sale {
public:
    Sale(int aDayOfTheMonth, int aAmount, int aSalesPersonId);
    friend void display(Sale sale, SalesPerson salesPerson);
private:
    int dayOfTheMonth;
    int amount;
    int salesPersonId;
};


Sale.cpp


#include "Sale.h"
Sale::Sale(int aDayOfTheMonth, int aAmount, int aSalesPersonId) {
    dayOfTheMonth = aDayOfTheMonth;
    amount = aAmount;
    salesPersonId = aSalesPersonId;
}


Main.cpp


#include "Sale.h"
#include "SalesPerson.h"
#include <iostream>
using namespace std;
// definition of friend function shared by two classes
void display(Sale sale, SalesPerson salesPerson) {
    cout << sale.dayOfTheMonth << " " << sale.amount << " " << sale.salesPersonId
         << " " << salesPerson.lastName << endl;
}
int main() {
    Sale sale(5, 100, 1);
    SalesPerson salesPerson(1, "Cole");
    display(sale, salesPerson);
    system("pause");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS