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

How to create three sperate files Assignment10.cpp, Employes.cpp and Employee.h

ID: 3604023 • Letter: H

Question

How to create three sperate files Assignment10.cpp, Employes.cpp and Employee.h in visual studio 2017 pay attention to the instrucation and the problem as follow:

Here are the main points that I want you to learn in this assignment:

Modularize files so that you can reuse them and provide easier maintenance.

Understand what belongs in a header file (.h) and what belongs in a code file (.cpp)

Understand the proper use of getters and setters

Understand the proper use of access modifiers (public/protected/private)

Copy/paste in the following code and compile it to verify that the IDE is working correctly.

You will need to create 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;

}

The project will look like this:

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

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.

the first console as flow:

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'

Explanation / Answer

I have modified your code.

Note: use header files appropriately in case if the compiler throws any error, because it can vary from compiler to compiler.

//Employee.h file

#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 file

#include<string>

#include <iostream>

#include “Employee.h”

using namespace std;

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 file

#include “Employee.h”

#include<iostream>

#include<string>

Using namespace std;

int main( )

{

Employee robert;

robert.setFirstName("Robert");

robert.setLastName("Smith");

robert.setSalary(100);

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

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

system ("pause");

return 0;

}

//Output

Salary for Robert Smith is 100

Hit any key to continue

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote