using C Create two classes The first named Sale holds data f

using C++

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.

Solution

Code:

#include<iostream>
#include<string>
using namespace std;

class Salesperson;

class Sale
{
int day;
int month;
int year;
double amount;
int spID;

public:
   Sale(int d,int m,int y, double a, int id)
   {
       day=d;
       month=m;
       year=y;
       amount=a;
       spID=id;
   }

friend void display(Sale *[], Salesperson *[]);
};

class Salesperson
{
int spID;
string name;

public:
Salesperson(int id,char *n)
{
   spID=id;
   name.assign(n);
}

//friend void display(Sale **s, Salesperson **sp);
friend void display(Sale *[], Salesperson *[]);
};


//void display(Sale **s, Salesperson **sp)
void display(Sale *s[] , Salesperson *sp[])
{

for(int i=0;i<2;i++)
{
   string nn;
   for(int j=0;j<2;j++){
       if(sp[i]->spID==s[i]->spID){
           nn.assign(sp[i]->name);
           break;
       }
   }

   cout<<\"Sale #\"<<s[i]->spID<<\" on \"<<s[i]->day<<\"/\"<<s[i]->month<<\"/\"<<s[i]->year<<\" for $\"<<s[i]->amount<<\" sold by #\"<<s[i]->spID<<\" \"<<nn<<endl;

  
}
  
}

int main(void)
{

Sale * s[2];
Salesperson *sp[2];

s[0]=new Sale(25,12,2016, 559.95, 103);
s[1]=new Sale(15,11,2016,359.95,106);

sp[0]=new Salesperson(103,\"Woods\");
sp[1]=new Salesperson(106,\"Hansen\");

display(s,sp);
return 0;
}

I have created display() function with the arguments as the pointers to objects of Sale and Salesperson class assuming theree will be only two data members. If you want you can re-write the function as below

void display(Sale *s[] , Salesperson *sp[],int sSize, int spSize)
{

for(int i=0;i<sSize;i++)
{
   string nn;
   for(int j=0;j<spSize;j++){
       if(sp[i]->spID==s[i]->spID){
           nn.assign(sp[i]->name);
           break;
       }
   }

   cout<<\"Sale #\"<<s[i]->spID<<\" on \"<<s[i]->day<<\"/\"<<s[i]->month<<\"/\"<<s[i]->year<<\" for $\"<<s[i]->amount<<\" sold by #\"<<s[i]->spID<<\" \"<<nn<<endl;

  
}
  
}

Output:

Sale #103 on 25/12/2016 for $559.95 sold by #103 Woods
Sale #106 on 15/11/2016 for $359.95 sold by #106 Hansen

using C++ 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 sa
using C++ 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 sa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site