Question: 6. The file, array1.cpp, appears below. Enter this code and perform the following operations: a) How do public, private, and protected differ? Bring array1.cpp into

6. The file, array1.cpp, appears below. Enter this code and perform the following operations:

a) How do public, private, and protected differ? Bring array1.cpp into the editor. Note that you cannot link or execute this program because the definition for set() is not included. However, you should be able to compile this program successfully. Try it. Submit the code.

b) Make the array inside struct intArray private instead of public and try to access the array elements directly. What happens?

c) Make the array inside struct intArray protected instead of private and try to access the array elements directly. What happens?

d) Demonstrate how you access private members from public functions by writing the definition for intArray::set(). Make the function safer by checking the index to ensure it's in the proper range. Submit the code and the output.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!