Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please pay attention to the second problem l did build the code and set Empoyee.

ID: 3604440 • Letter: P

Question

  please pay attention to the second problem l did build the code and set Empoyee.h to private member the problem as follow look at the last sentence of the problem lam not able how to fix if declare  privates :

The Problem

Now, as usual, the problem is simple but the solution may be a bit tough.

First, I want to make sure that you understand the concept of getters, setters and access modifiers. Right now the name is handled in one variable called fullName. This is not the way that most applications work. What I want you to do is separate the fullName variable into 2 separate variables called, firstName and lastName. Define the variables and then create the getters and setters for them. You can dispose of the methods for fullName – since these are now obsolete.

Second, as you may have noticed, there is a slight, but significant problem with the code. Take a look at the code below; I have inserted the following line:

robert.salary = 200;

This line is highlighted in red for a reason. This is one of the worst coding mistakes that can be made in object-oriented systems. The problem is that we are now directly accessing the code from an application without any security. Thus, anyone who uses the Employee class has direct access to the salary information. We obviously don’t want that. Notice that in the Employee.h file all of the attributes (variables) are defines as public. This is the problem.

// Assignment10.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

#include "Employee.h"

int main( )

{

Employee robert;

robert.setFullName("Robert Smith");

robert.setSalary(100);

robert.salary = 200;

cout << "Salary for " << robert.getFullName() << " is " << robert.getSalary() << endl;

cout << "Hit any key to continue" << endl;

system ("pause");

return 0;

}

When you run this code you get the following output:

You can see that the application changed the salary to 200 – not a wise thing. Most data should be protected – especially stuff like salaries.

The second part of this problem is simple (at least from my perspective). All you need to do is add one line to the Employee.h file and by doing so you will get this compilation message:

1>c:documents and settingsmweisfemy documents isual studio 2010projectsssignment10ssignment10ssignment10.cpp(20): error C2248: 'Employee::salary' : cannot access private member declared in class 'Employee'

This is a major concept in object-oriented programing and I want to see how you solve this problem.

pay attention how how solve this error code when it decare private the code l built as follow:

Employee.h

#ifndef Employee_h

#define Employee_h

#include <iostream>

#include <string>

using namespace std;

class Employee

{

private:

string firstName;

string lastName;

int salary;

public:

void setFirstName(string);

string getFirstName();

void setLastName(string);

string getLastName();

void setSalary(int);

int getSalary();

};

#endif

Employee.cpp

#include "Employee.h"

void Employee::setSalary(int salary)

{

this->salary = salary;

}

int Employee::getSalary()

{

return salary;

}

void Employee::setFirstName(string firstName)

{

this->firstName = firstName;

}

string Employee::getFirstName()

{

return firstName;

}

void Employee::setLastName(string lastName)

{

this->lastName = lastName;

}

string Employee::getLastName()

{

return lastName;

}

Assignment10.cpp

#include<iostream>

#include<string>

#include "Employee.h"

using namespace std;

int main()

{

Employee robert;

robert.setFirstName("Robert");

robert.setLastName("Smith");

robert.setSalary(100);

robert.salary = 200;

cout << "Salary for " << robert.getFirstName() << " " << robert.getLastName() << " is " << robert.getSalary() << endl;

cout << "Hit any key to continue" << endl;

system("pause");

return 0;

}

Explanation / Answer

The problem here is that, we need to hide or protect the salary variable of class from outside classes. So we cannot expose this variable directly. No class can directly read or wrte the value of salary in the Employee class object.

To read or right object we must use the publically defined getters and setters in the class

So we made it private and thus it started throwing the error.

Now to fix the error and make our program work , we need to make changes in Assignment10.cpp.

We need to remove the line where we are accessing a private variable salary. In place of it we need to use the setSalary() function. The updated code is as below ..

#include<iostream>
#include<string>
#include "Employee.h"
using namespace std;
int main()
{
Employee robert;
robert.setFirstName("Robert");
robert.setLastName("Smith");
robert.setSalary(100);
robert.setSalary(200);
cout << "Salary for " << robert.getFirstName() << " " << robert.getLastName() << " is " << robert.getSalary() << endl;
cout << "Hit any key to continue" << endl;
system("pause");
return 0;
}

Notice the line in bold. I have changed it from direct salary access to setSalary() function. The program now works fine. This is because setSalary() function is public and can be access outside the class and salary is private and this cannot be accessed anywhere except within the class. The process is called Abstraction and Encapsulation.