-2

For the following code, does anyone understand how and why the friend function is working? what is emp and why does emp.boss point to name?? The purpose was the create a class that adds employees to subteams.• Since employees can have only one boss, if we hire someone who already has a boss, we should report a failure.

#include<iostream>.
#include<string>
#include<vector>

class Employee{

friend ostream& operator<<(ostream& os, const Employee& emp){
    cout<< "Name: "<< emp.name << endl;
    cout<< "Boss: ";
    if (emp.boss != nullptr){
        cout<< emp.boss -> name << endl;
    }
    else
    {
        cout << "I am the boss." <<endl;
    }
    cout<< "Team ..." <<endl;
    for(fize_t i = 0; i < emp.myTeam.size(); i++) {
        cout << "\t" << emp.myTeam[i] -> name << endl;
    }
    return os;
}

private:
    string name;
    Employee* boss;
    vector<Employee*> myTeam;

public:
    Employee(const string& name, Employee* boss = nullptr): name(name),    boss(boss)
//
    {
    if(boss != nullptr){
        boss -> myTeam.push_back(this);
    }
}
//condition check





bool addToTeam(Employee& newGuy)
{
    if(newGuy.boss != nullptr)
    {
        return False;
    }
    newGuy.boss = this;
    myTeam.push_back(&newGuy);
    return true;
}
bool removeFromTeam(Employee& emp)
{
    for(size_t i=0; i <myTeam.size(); i++)
    {
        if(myTeam[i] == &emp) //the first item in the vector is the reference to &emp, so now the first item is now the address of the employee?        {
            emp.boss = nullptr;
            myTeam[i] = myTeam[myTeam.size() -1];
            myTeam.pop_back();
            return true;
        }
    }
    return false;
    }
};

I tried to get the correct code.

swindy
  • 1
  • 1
    `emp.boss` doesn't "point to" `name`. The `->` operator dereferences the `emp.boss` pointer and accesses its `name` member. It is syntactic sugar for `(*emp.boss).name`. – 0x5453 Mar 21 '22 at 18:39
  • 5
    It looks like you need [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The concepts of passing arguements to functions, references and operator overloading should be in any beginner level book. – Yksisarvinen Mar 21 '22 at 18:39
  • So if it accesses the name member, what does it look for?Is nullptr pointing to nothing, but then points to the name to see if the name is the boss?? I am very confused – swindy Mar 21 '22 at 18:44
  • I don't really understand what is your issue. Perhaps you should run your debugger, examine values of variables when the function is executed and it will clear up to you? – Yksisarvinen Mar 21 '22 at 18:51
  • This question may need more focus. From your comment, you may be misunderstanding what `!=` means. `if (emp.boss != nullptr)` is an if statement that is only entered if `emp.boss` is **not** `nullptr`. _"pointing to nothing, but then points to the name"_ does not describe what this code is saying. – Drew Dormann Mar 21 '22 at 20:03
  • the friend `operator<<` should write to `os` not to `cout` – pm100 Mar 22 '22 at 01:05

1 Answers1

0

Okay, let's look at the method:

friend ostream& operator<<(ostream& os, const Employee& emp){
    cout<< "Name: "<< emp.name << endl;
    cout<< "Boss: ";
    if (emp.boss != nullptr){
        cout<< emp.boss -> name << endl;
    }
    else
    {
        cout << "I am the boss." <<endl;
    }
    cout<< "Team ..." <<endl;
    for(fize_t i = 0; i < emp.myTeam.size(); i++) {
        cout << "\t" << emp.myTeam[i] -> name << endl;
    }
    return os;
}

First off, this is an operator<< method. You would use it like this:

Employee someone;
... populate someone with data, however you're doing that

std::cout << someone << std::endl;

Basically, this just makes it easy to print an Employee to any stream -- like cout or a file.

Most of it should make sense. I think you were wondering about this:

    cout<< emp.boss -> name << endl;

This is writing out this employee's boss's name. So if you did this:

Employee emp;
Employee boss;
boss.name = "Fred";

Then that section of code would write out Fred. You can read it left to right:

Start with emp
Then go to the boss field
Then because boss points somewhere (the code checks), look at the boss's name field.
pm100
  • 48,078
  • 23
  • 82
  • 145
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36