Please help me with these C++ questions. If possible please screenshot the outpu
ID: 3853112 • Letter: P
Question
Please help me with these C++ questions. If possible please screenshot the outputs. thank you so much!
8. Enter employee.cpp, which can be found below. Notice that we can make an entire struct a friend of another struct. All the member functions of accountant have access to private elements of employee.
a) Create struct accountant in the file employee.cpp. Within the struct, declare a function payroll() which shows an employee object being passed by value into the payroll() function. Submit the code.
b) Define the payroll() function which accesses and prints the private information about employee. Simply select them as if they were public members of the struct. Convince yourself that you can compile the program. Submit the code.
c) To show this isn't normally possible, comment out the friend declaration in the employee struct. What happens when you compile? Submit the code.
d) Create a new struct called boss with a member function called seepay(employee). This should precede the employee declaration. To do this, you must first tell the compiler there is a struct somewhere called employee with a name declaration: struct employee; Submit the code.
e) A type name declaration has no body, so the compiler knows the name exists, but not how big it is. Now create a declaration inside struct employee which says "the seepay() function of struct boss has friend status, but only that function." Submit the code.
f) See if you can change the friend declaration in struct employee so it only gives access to accountant::payroll(), and not the entire struct accountant. Submit the code.
SOURCE CODES BELOW:
// employee.cpp entire structs can be friends
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
friend struct accountant;
};
int main() {
}
Explanation / Answer
a)
#include <iostream>
using namespace std;
// employee.cpp entire structs can be friends
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
friend struct accountant;
};
struct accountant
{
void payroll(employee e);
};
int main() {
}
=========
b)
#include <iostream>
using namespace std;
// employee.cpp entire structs can be friends
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
friend struct accountant;
};
struct accountant
{
void payroll(employee e)
{
cout << e.salary << " " << e.benefitLevel << endl;
}
};
int main() {
}
===========
c) Commenting out the friend declaration in employee struct generates error - private members salary and benefitLevel can not be accessed in accountant.
The code does not compile.
#include <iostream>
using namespace std;
// employee.cpp entire structs can be friends
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
//friend struct accountant;
};
struct accountant
{
void payroll(employee e)
{
cout << e.salary << " " << e.benefitLevel << endl;
}
};
int main() {
}
========
d)
#include <iostream>
using namespace std;
// employee.cpp entire structs can be friends
struct employee; //name declaration of employee
struct boss
{
void seepay(employee e);
};
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
friend struct accountant;
};
struct accountant
{
void payroll(employee e)
{
cout << e.salary << " " << e.benefitLevel << endl;
}
};
int main() {
}
======
e)
#include <iostream>
using namespace std;
// employee.cpp entire structs can be friends
struct employee; //name declaration of employee
struct boss
{
void seepay(employee e);
};
struct employee {
private:
int salary;
int benefitLevel;
public:
void initialize(
int startingPay);
void raise(float percent);
friend struct accountant;
friend void boss::seepay(employee e);
};
struct accountant
{
void payroll(employee e)
{
cout << e.salary << " " << e.benefitLevel << endl;
}
};
int main() {
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.