Erroneous access to protected variable fields



  • Please help solve this problem! There's an employee in the program with a field protected. In the zarplata function, I'm trying to change this field's variable data through the index (the employee class target that indicator k indicates has been created in advance), but the program is making a mistake in access to the field. See code below.

    class employee//сотрудник
    {
    protected:
        string name;
        string surname;
        string patronymic;
        double salary = 0;
    };
    

    void zarplata(employees *k)
    {
    if (k->position == "Бухгалтер") k->j.salary = standart * stavka_accountant;
    if (k->position == "Секретарь") k->j.salary = standart * stavka_secretary;
    if (k->position == "Сисадмин") k->j.salary = standart * stavka_administrator;
    if (k->position == "Директор") k->j.salary = standart * stavka_director;
    return;
    }

    Thank you.



  • What if you use your friends?

    class employee//сотрудник
    {
    protected:
        double salary = 0;
    
    friend void zarplata(employee* k);
    

    };

    void zarplata(employee* k)
    {
    k->salary = 20;
    }

    int main() {
    employee x;
    zarplata(&x);
    }


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2