View the step-by-step solution to:

Question


Use website jdoodle c++ online complier and Copy/paste in the following code and compile it to

verify that the IDE is working.

You will need to make 3 separate files, Assignment10.cpp, Employee.cpp and Employee.h. Here are the

files.


Employee.h

#include <iostream>

#include <string>

using namespace std;

class Employee

{

public:

void setFullName( string );

string getFullName();

void setSalary ( int );

int getSalary ();

string fullName;

int salary;

};


Employee.cpp

#include "stdafx.h"

#include "Employee.h"

void Employee::setSalary( int salary )

{

this->salary = salary;

}

int Employee::getSalary( )

{

return salary;

}

void Employee::setFullName( string fullName )

{

this->fullName = fullName;

}

string Employee::getFullName( )

{

return fullName;

}


Assignment10.cpp

// 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);

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

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

system ("pause");

return 0;

}



When you execute the code, the output will look like this:


Capture111.PNG


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 to be done 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;


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:


Capture22222.PNG



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 documentsvisual studio

2010projectsassignment10assignment10assignment10.cpp(20): error C2248: 'Employee::salary'

: cannot access private member declared in class 'Employee'


Since this assignment requires 3 files, simply submit a ZIP file that includes all 3 files. This time you can

include the Assignment10.cpp, Employee.h and Employee.cpp files in a zip file with their proper

extensions. Send over the zip file and screenshots of the code and output.

Capture111.PNG

Capture22222.PNG

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes