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