Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with these C++ questions. If possible please screenshot the outputs. thank you so much! 7. You can also control access by using

Please help me with these C++ questions. If possible please screenshot the outputs. thank you so much!

7. You can also control access by using a friend function. A friend is a function that, although it isn't a member of a struct, has access to private and protected members. A friend must be declared inside a struct (so you can tell by looking at the struct who has access to it). The declaration is shown in farray1.cpp. In farray1.cpp, an intArray is passed by value to print(), which can then access the elements of the private array a. This would not be possible if print() were not a friend function.

a) Enter farray1.cpp, add the set() function from the previous exercise and compile and execute the program. Submit the code and the output.

b) Remove the friend modifier and discuss what happens when you compile the program. Submit the code.

8. Enter employee.cpp, which can be found on the following pages. 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:

// array1.cpp

// note: this program will compile, but it won't link without the

// set() definition

#include

#define SIZE 10

using namespace std;

struct intArray {

public:

int a[SIZE];

void set(int index, int value);

void print();

};

void intArray::print() {

for(int i = 0; i < SIZE; i++)

cout << "a[" << i << "] = " << a[i] << endl;

}

int main() {

intArray num;

for(int i =

0; i < SIZE; i++)

num.set(i,i);

num.print();

}

// farray1.cpp friend function

// note: this program will compile, but it won't link without

// the set() definition

#include

#define SIZE 10

using namespace std;

struct

intArray {

private:

int a[SIZE];

public:

void set(int index, int value);

friend void print(intArray);

};

void print(intArray x) {

for(int i = 0; i < SIZE; i++)

cout << "a[" << i << "] = " << x.a[i] << endl;

}

int main()

{

intArray num;

for(int i = 0; i < SIZE; i++)

num.set(i,i);

print(num);

}

// 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() {

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

How are we going to work together?

Answered: 1 week ago