0

The problem was to access the private variables for output from two different classes by adding a function that is a friend of both classes.

class Salesperson;

class Sale {
private:
    string date;
    double total;
    int SalespersonIdNum;
public:
    Sale(string, double, int);
    friend void display(Sale &, Salesperson &);
};
Sale::Sale(string d, double t, int S) {
    >>d = date;
    >>t = total;
    >>S = SalespersonIdNum;
}
//Salesperson class
class Salesperson {
private:
    int idNum;
    string lastName;
public:
    Salesperson(int, string);
    friend void display(Sale &, Salesperson &);
};
Salesperson::Salesperson(int i, string l) {
    >>i = idNum;
    >>l = lastName;
}

void display(Sale &s, Salesperson &sp) {
    //Output Sale info
    cout << " ID of person who completed sale: " << s.SalespersonIdNum << endl;
    cout << " Sale Total: " << s.total << endl;
    cout << " Sale Date: " << s.date << endl;
    //Output Saleperson info
    cout << " Sales Person ID: " << sp.idNum << endl;
    cout << " Sales Person Last Name: " << sp.lastName << endl << endl;
}

int main() {

    Sale s1("10/06/2008", 78.45, 123);
    Salesperson emp1(123, "Pelletier");

    display(s1, emp1);

    Sale s2("05/21/2010", 162.59, 321);
    Salesperson emp2(321, "Howel");

    display(s2, emp2);

    return 0;
}

The display function should ideally be able to access the variables defined by my object definitions, but when I go to run and display them the function displays this.

ID of person who completed sale: 124                                                    
 Sale Total: 2.07482e-317                                                                
 Sale Date:                                                                              
 Sales Person ID: 6299816                                                                
 Sales Person Last Name:  

 ID of person who completed sale: 1677231376                                             
 Sale Total: 2.07417e-317                                                                
 Sale Date:                                                                              
 Sales Person ID: 1                                                                      
 Sales Person Last Name:                                                                 


...Program finished with exit code 0                                                     
Press ENTER to exit console.

By my understanding the friend function should work like that but maybe the issue lies within the constructors, but those are also done correctly I think. As for the output maybe im outputting memory addresses or something, but I have no clue about that

Saucy Goat
  • 1,587
  • 1
  • 11
  • 32
  • 1
    Your constructors are "the wrong way round" - you are assigning the values of the class members (which are uninitialized) to the parameters you pass in (instead of actually assigning to the class members) – UnholySheep May 24 '18 at 20:24

1 Answers1

2

Your assignments are in the wrong direction in the constructor.

Sale::Sale(string d, double t, int S) {
    d = date;
    t = total;
    S = SalespersonIdNum;
}

They need to be:

Sale::Sale(string d, double t, int S) {
    date = d;
    total = t;
    SalespersonIdNum = S;
}

It will be better to use the list initialization syntax.

Sale::Sale(string d, double t, int S) :
    date(d),
    total(t),
    SalespersonIdNum(S) { }
R Sahu
  • 204,454
  • 14
  • 159
  • 270